问题描述
||
以下是我的代码:
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。谁能告诉我我在哪里犯错。
提前致谢。
解决方法
我遇到了类似的问题,发现从列表项中删除
android:clickable=\"true\"
之后,onListItemClick()被很好地触发了。我希望这有帮助。
,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
}
,解决方案是重写受保护的void onListItemClick(ListView l,View v,int位置,长id)方法。
,我也有这个问题。我通过从布局XML中的TextView中删除此问题来解决它。
android:textIsSelectable=\"true\"