以下是我的代码:
public class TestActivity extends ListActivity {
private Cursor cursor;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
fillData();
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
// Get the item that was clicked
Object o = this.getListAdapter().getItem(position);
String keyword = o.toString();
Toast.makeText(this, "You selected: " + keyword, Toast.LENGTH_LONG)
.show();
}
private void fillData() {
TermsData_DataHelper termsDataHelper = new TermsData_DataHelper(
TestActivity.this);
cursor = termsDataHelper.fetchAllCategories();
startManagingCursor(cursor);
String[] names = new String[] { "name" };
int[] to = new int[] { R.id.label };
// Now create an array adapter and set it to display using our row
SimpleCursorAdapter categories = new SimpleCursorAdapter(this,
R.layout.terms_row, cursor, names, to);
setListAdapter(categories);
}
}
当我点击列表视图中的一行时,我看不到任何Toast。任何人都可以告诉我我在哪里犯了错误。
提前致谢。
答案 0 :(得分:4)
我遇到了类似的问题,发现从我的列表项中删除android:clickable="true"
后,onListItemClick()被触发就好了。我希望这会有所帮助。
答案 1 :(得分:2)
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
String mString = parent.getItemAtPosition(position).toString();
//will give you the text of current line or
int i = parent.getItemAtPosition(position);
//if you want just position number
}
答案 2 :(得分:2)
答案 3 :(得分:1)
我也有这个问题。我通过在布局XML中从TextView中删除它来解决它。
android:textIsSelectable="true"