我无法获得Toast消息,因为onItemClick从未被调用。Log-cat没有显示任何错误。请检查我的代码和纠正我,如果我错了任何地方。我使用了数组适配器。
public class Open extends ListActivity {
DbAdapter mDb = new DbAdapter(this);
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
mDb.close();
super.onDestroy();
}
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.open);
ListView listContent = (ListView) findViewById(android.R.id.list);
mDb.open();
Cursor cursor =mDb.fetchAllPrograms();
startManagingCursor(cursor);
String[] from = new String[] { DbAdapter.KEY_TITLE };
int[] to = new int[] { R.id.textViewName };
SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this,
R.layout.row, cursor, from, to);
listContent.setAdapter(cursorAdapter);
//Onclick ListView setlistener
listContent.setTextFilterEnabled(true);
listContent=getListView();
listContent.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View
view,int position, long id) {
Toast.makeText(getApplicationContext(),
"Working!",
Toast.LENGTH_LONG).show();
}
});
}
}
Row.xml如下:
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:id="@+id/textViewName"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:textColor="#0000FF"
android:textIsSelectable="true"
/>
我遇到了一个类似的问题。不仅是我的onItemClick方法被忽略,而且我的列表选择器不工作(android:listSelector
.)
原来它是我的TextView行元素的android:textIsSelectable
属性。尝试设置为false
在我的情况下,问题是在android:longClickable="true"
。从xml中删除这一行后,单击侦听器又开始工作了。