Android: Log or Toast the selected Item in an ListView

in case you have the need to log.i the selected item of an listview this snippet could help:

 

[java]

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
CheckedTextView checkedTextView = (CheckedTextView) view;

String itemClicked = (String) listView.getAdapter().getItem(i);

if (checkedTextView.isChecked()) {
Log.i("Info :", itemClicked + " is checked");
Toast.makeText(UsersActivity.this, itemClicked + " is checked", Toast.LENGTH_SHORT).show();
} else {
Log.i("Info :", itemClicked + " is NOT Checked");
Toast.makeText(UsersActivity.this, itemClicked + " is NOT checked", Toast.LENGTH_SHORT).show();
}
}
[/java]

 

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.