3 回答

TA貢獻1871條經(jīng)驗 獲得超13個贊
除了創(chuàng)建一個覆蓋newView / bindView或getView的自定義適配器之外,我不確定您將如何執(zhí)行此操作,具體取決于您覆蓋的內容(ResourceCursorAdapter是個不錯的選擇)。
好的,這是一個例子。我沒有測試是否可以編譯,因為我正在工作,但這絕對可以為您指明正確的方向:
public class MyActivity extends ListActivity {
MyAdapter mListAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Cursor myCur = null;
myCur = do_stuff_here_to_obtain_a_cursor_of_query_results();
mListAdapter = new MyAdapter(MyActivity.this, myCur);
setListAdapter(mListAdapter);
}
private class MyAdapter extends ResourceCursorAdapter {
public MyAdapter(Context context, Cursor cur) {
super(context, R.layout.mylist, cur);
}
@Override
public View newView(Context context, Cursor cur, ViewGroup parent) {
LayoutInflater li = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
return li.inflate(R.layout.mylist, parent, false);
}
@Override
public void bindView(View view, Context context, Cursor cur) {
TextView tvListText = (TextView)view.findViewById(R.id.list_text);
CheckBox cbListCheck = (CheckBox)view.findViewById(R.id.list_checkbox);
tvListText.setText(cur.getString(cur.getColumnIndex(Datenbank.DB_NAME)));
cbListCheck.setChecked((cur.getInt(cur.getColumnIndex(Datenbank.DB_STATE))==0? false:true))));
}
}
}

TA貢獻1860條經(jīng)驗 獲得超8個贊
您可以設置一個自定義SimpleCursorAdapter.ViewBinder:
SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(/* ur stuff */);
cursorAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
if(columnIndex == 1) {
CheckBox cb = (CheckBox) view;
cb.setChecked(cursor.getInt(1) > 0);
return true;
}
return false;
}
});
setViewValue在SimpleCursorAdapter構造函數(shù)中為您指定的每個列都調用該方法,并為您提供了一個操作某些(或全部)視圖的好地方。

TA貢獻2037條經(jīng)驗 獲得超6個贊
您可以通過創(chuàng)建自定義CheckBox小部件來解決該問題,如下所示:
package com.example.CustomCheckBox;
public class CustomCheckBox extends CheckBox {
public CustomCheckBox(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public CustomCheckBox(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomCheckBox(Context context) {
super(context);
}
protected void onTextChanged(CharSequence text, int start, int before, int after) {
if (text.toString().compareTo("") != 0) {
setChecked(text.toString().compareTo("1") == 0 ? true : false);
setText("");
}
}
}
當ListView將數(shù)據(jù)綁定到CheckBox時(即添加“ 0”或“ 1”),將調用onTextChanged函數(shù)。這將捕獲該更改并添加您的布爾處理。需要第一個“ if”語句,以免產(chǎn)生無限遞歸。
然后像這樣在布局文件中提供您的自定義類:
<com.example.CustomCheckBox
android:id="@+id/rowCheckBox"
android:layout_height="fill_parent"
android:layout_width="wrap_content" />
那應該做!
- 3 回答
- 0 關注
- 519 瀏覽
添加回答
舉報