Listview.onitemclick Not Working
My ListView in Activity: ListView listView1 = (ListView) menu.findViewById(R.id.menuList); String menuItems[] = new String[] { 'My Wants', 'Profile', 'Notifications',
Solution 1:
Its because in my ListView item layout i added
android:clickable="true"
for both the TextViews. So when i click on the ListView item it indeed is a click on these TextView whose onClick is not implemented. Removing the clickable attribute from the TextViews solved my problem.
Thanks everyone
Solution 2:
Try making the RelativeLayout
clickable and focusable.
Solution 3:
Layout File
<ListView android:id="@id/android:list"android:layout_width="fill_parent"android:layout_height="fill_parent"android:background="#000fff"android:layout_weight="2"android:drawSelectorOnTop="false">
</ListView>
Main Activity
publicclassMyListViewextendsListActivity {
/** Called when the activity is first created. */@OverridepublicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(newArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, PENS));
getListView().setTextFilterEnabled(true);
}
static final String[] PENS = newString[]{
"MONT Blanc",
"Gucci",
"Parker",
"Sailor",
"Porsche Design",
"Rotring",
"Sheaffer",
"Waterman"
};
protectedvoidonListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Object o = this.getListAdapter().getItem(position);
String pen = o.toString();
Toast.makeText(this, "You have chosen the pen: " + " " + pen, Toast.LENGTH_LONG).show();
}
}
Solution 4:
try this one
http://www.mkyong.com/android/android-listview-example/
res/layout/list_fruit.xml
<?xml version="1.0" encoding="utf-8"?><TextViewxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:padding="10dp"android:textSize="20sp" ></TextView>
ListView
publicclassListFruitActivityextendsListActivity {
staticfinal String[] FRUITS = newString[] { "Apple", "Avocado", "Banana",
"Blueberry", "Coconut", "Durian", "Guava", "Kiwifruit",
"Jackfruit", "Mango", "Olive", "Pear", "Sugar-apple" };
@OverridepublicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// no more this// setContentView(R.layout.list_fruit);
setListAdapter(newArrayAdapter<String>(this, R.layout.list_fruit,FRUITS));
ListViewlistView= getListView();
listView.setTextFilterEnabled(true);
listView.setOnItemClickListener(newOnItemClickListener() {
publicvoidonItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(),
((TextView) view).getText(), Toast.LENGTH_SHORT).show();
}
});
}
}
Post a Comment for "Listview.onitemclick Not Working"