Reputation: 12582
following is my ListActivity, onCreate code
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
fakeTripBuilder();
setContentView(R.layout.cutom_list_view);
SimpleAdapter adapter = new SimpleAdapter(this, list, R.layout.custom_row_view,
new String[] {"stop","distance","time","icon","mode"},
new int[] {R.id.text_stop,R.id.text_distance, R.id.text_time, R.id.icon, R.id.text_mode});
populateList();
setListAdapter(adapter);
}
inside the the same class i have the following method
@Override
protected void onListItemClick(ListView l, View v, int position, long id){
super.onListItemClick(l, v, position, id);
Log.d("Travel","You choosed!");
Object o = this.getListAdapter().getItem(position);
Toast.makeText(this, "You Choosed"+o.toString(), Toast.LENGTH_LONG).show();
}
and here is the "custom_list_view.xml"
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:clickable="false"
android:gravity="center_horizontal|center_vertical"
android:orientation="vertical" >
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000fff"
android:layout_weight="2"
android:drawSelectorOnTop="false">
</ListView>
<TextView
android:id="@id/android:empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFff00"
android:text="No data"
/>
</LinearLayout>
Problem is, onListItemClick method do not call when i click on it. Help !!
Upvotes: 2
Views: 4705
Reputation: 1666
To get a reference to the ListView in a ListActivity you should call:
ListView lv = getListView();
You do not need to set a click listener, it already has one. Simply override:
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
Object item = lv.getAdapter().getItem(position);
}
And in your xml file, the ListView should have the id:
android:id="@android:id/list"
Edit
You need to remove
android:clickable="false"
from the layout to allow any child views to be clicked
Upvotes: 6
Reputation: 27237
A lot has changed since this question was asked in 2012. This is an update to the accepted answer
To use onItemClickListener
in your ListActivity
, just override the onListItemClick(...)
method like this:
public void onListItemClick(ListView listView, View view, int position, long id) {
ListView myListView = getListView();
Object itemClicked = myListView.getAdapter().getItem(position);
Toast.makeText(getApplicationContext(), "Item Clicked: " + item + " \nPosition: " + id,
Toast.LENGTH_SHORT).show();
}
Notice the getItem()
method instead of getItemAtPosition()
in the accepted answer
Remember: for this to work, you need to have a listview with id list
, else your app will crash.
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/list"
/>
Upvotes: 1
Reputation: 3812
ListView lv = (ListView) findViewById(R.layout.cutom_list_view);
You should use R.id.listview_id instead, that should be defined in R.layout.cutom_list_view
like so:
<ListView
android:id="@+id/listview_id"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
EDIT:
R.layout.cutom_list_view is id of your layout file, but you should find listview by its own id, defined like android:id="@+id/listview_id", so the id will be R.id.listview_id
Upvotes: 0
Reputation: 1738
Try overriding the onListItemClick method if you want to catch click events on your list.
Upvotes: 0