我正在定制ListView
包含 a 的行数CheckBox
and a TextView
。在我使用自定义之前ListViews
使用 SimpleCursorAdapter,我的onListItemClick()
工作得很好。
我读过我必须添加一个onClickListener
to my TextViews
但是哪里?为什么?
我还在延长ListActivity
并通过一个Adapter
to setListAdapter(listedPuzzleAdapter);
,我不是吗?
public class PuzzleListActivity extends ListActivity {
private PuzzlesDbAdapter mDbHelper;
private Cursor puzzlesCursor;
private ArrayList<ListedPuzzle> listedPuzzles = null;
private ListedPuzzleAdapter listedPuzzleAdapter;
private class ListedPuzzleAdapter extends ArrayAdapter<ListedPuzzle> {
private ArrayList<ListedPuzzle> items;
public ListedPuzzleAdapter(Context context, int textViewResourceId,
ArrayList<ListedPuzzle> items) {
super(context, textViewResourceId, items);
this.items = items;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.puzzles_row, null);
}
ListedPuzzle lp = items.get(position);
if (lp != null) {
TextView title = (TextView) v.findViewById(R.id.listTitles);
title.setText(lp.getTitle());
CheckBox star = (CheckBox) v.findViewById(R.id.star_listed);
star.setChecked(lp.isStarred());
}
return v;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.puzzles_list);
// Create database helper to open connection
mDbHelper = new PuzzlesDbAdapter(this);
mDbHelper.open();
fetchData();
}
private void fetchData() {
puzzlesCursor = mDbHelper.fetchAllPuzzles();
startManagingCursor(puzzlesCursor);
listedPuzzles = new ArrayList<ListedPuzzle>();
ListedPuzzle lp;
puzzlesCursor.moveToFirst();
while (!puzzlesCursor.isAfterLast()) {
lp = new ListedPuzzle();
lp.setTitle(puzzlesCursor.getString(puzzlesCursor
.getColumnIndex(PuzzlesDbAdapter.KEY_TITLE)));
lp.setStarred(puzzlesCursor.getInt(puzzlesCursor
.getColumnIndex(PuzzlesDbAdapter.KEY_STARRED)) > 0);
listedPuzzles.add(lp);
puzzlesCursor.moveToNext();
}
listedPuzzleAdapter = new ListedPuzzleAdapter(this,
R.layout.puzzles_row, listedPuzzles);
setListAdapter(listedPuzzleAdapter);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Intent i = new Intent(this, PuzzleQuestionActivity.class);
i.putExtra(PuzzlesDbAdapter.KEY_ROWID, id);
startActivity(i);
}
编辑:我的问题是制作整个定制项目ListView
可点击,所以我发现最好的答案是@Luksprog 给出的答案。这onListItemClick
从我的ListActivity
就足够了。我只需要设置android:focusable='false'
使其发挥作用。
现在CheckBox
的每一项上ListView
应该“星号”该项目,这意味着访问数据库。
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.puzzles_row, null);
}
ListedPuzzle lp = items.get(position);
if (lp != null) {
TextView title = (TextView) v.findViewById(R.id.listTitles);
title.setText(lp.getTitle());
CheckBox star = (CheckBox) v.findViewById(R.id.star_listed);
star.setChecked(lp.isStarred());
star.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
Integer realPosition = (Integer) v.getTag();
ListedPuzzle obj = items.get(realPosition);
obj.getId();
}
});
}
return v;
}
But the v.getTag()
指的是一个非最终变量,如果我改变它v = vi.inflate(R.layout.puzzles_row, null)
无法分配。
解决这个问题的最佳方法是什么?我从来没有真正理解整个最终协议。