I was trying out a ListActivity example from the net. I am having problem using a custom list layout. Here is my code:
public class Test1Activity extends ListActivity {
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
"Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
"Linux", "OS/2" };
//ArrayAdapter<String> standard_adapter = new ArrayAdapter<String> (this,android.R.layout.simple_list_item_1, values);
ArrayAdapter<String> my_adapter = new ArrayAdapter<String>(this, R.layout.mylistlayout, R.id.label,values);
setListAdapter(my_adapter);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
String item = (String) getListAdapter().getItem(position);
Toast.makeText(this, item + " selected", Toast.LENGTH_LONG).show();
}}
onListItemClick is working fine when I use the standard adapter. But it doesn’t work when use my adapter. In fact I don’t even see any highlighting when I click on the list items. Why is this happening. Am I missing some code?
mylistlayout is a simple LinearLayout with a TextView(id label) and a Button(id button1).
<?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="fill_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/label"
android:layout_width="287dp"
android:layout_height="wrap_content"
android:text="items" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="32dp"
android:padding="0dp"
android:text="X" />
Try to add:
to your
Buttonin the xml layout.