If you want to add a special action when you click a TextView
or / and CheckBox
from any of the lines in the ListView
, then add an OnCLickListener
for these Views
to the getView
your custom Adapter
:
@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);
If you want to do an action when a row is clicked (regardless of what View
clicked from that row (if it was clicked)) just use OnItemClickListener
on the ListView
(or onListItemClick
in case of a ListActivity
).
In addition, I hope that you set android:focusable="false"
for CheckBox
(in R.layout.puzzles_row
), because I do not think that onListItemClick
will work differently.
Edit:
You start a new Activity
in onListItemClick
(in the case of a ListActivity
) if you want to start a new action no matter where the user clicks on the line :
@Override protected void onListItemClick(ListView l, View v, int position, long id) { Intent i = new Intent(this, PuzzleQuestionActivity.class); i.putExtra(PuzzlesDbAdapter.KEY_ROWID, id); startActivity(i); }
If for some reason you want to start a new Activity
when the user clicks only (for example) TextView
on the ListView
line, then run the new action in the onClick
method from my code above:
//... title.setOnclickListener(new OnCLickListener(){ @Override public void onClick(View v) { Integer realPosition = (Integer) v.getTag(); ListedPuzzle obj = items.get(realPosition); Intent i = new Intent(this, PuzzleQuestionActivity.class); i.putExtra(PuzzlesDbAdapter.KEY_ROWID, obj.getTheId());//see below startActivity(i); } //...
To do this, you will need to modify the ListedPuzzle
to add the PuzzlesDbAdapter.KEY_ROWID
column from the puzzlesCursor
cursor in the fetchData()
method:
//... 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); lp.setTheId(puzzlesCursor.getLong(puzzlesCursor .getColumnIndex(PuzzlesDbAdapter.KEY_ROWID))); listedPuzzles.add(lp); //...