简体   繁体   中英

Click on item in android ListActivity not working

I have ListActivity with custom ArrayAdapter and following layout for list items:

<?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" 
android:paddingTop="5dip"
android:paddingBottom="5dip" >

<ImageView
    android:id="@+id/image"
    android:layout_width="48dip"
    android:layout_height="48dip" />

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/text1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="@color/white"
        android:textSize="24.5sp" />

    <TextView
        android:id="@+id/text2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="@color/white" 
        android:textSize="12sp"/>
</LinearLayout>

My problem is that, I am unable to click any of the list items. What I've tried so far is setting focusable , clickable to false (which I found might help) to my ImageView but, unfortunately, problem remains. Please help, this has been bugging me all day. Thank you.

EDIT: OK, so William's answer helped my out with this one, only problem now is that click on item doesn't highlight it, when I change background colour to black (by setting new theme in manifest for this list activity). This is my custom style for activity theme <style name="ContentsListTheme"> <item name="android:background">@color/black</item> </style> from res/values/styles/ folder.

To make it clear, once again, default ListActivity background colour is white. I want it black. When I apply the style from above, list activity item highlighting when clicked is disabled. How can I enable it again, to the default highlighting colour?

In your oncreate try this

ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
          public void onItemClick(AdapterView<?> parent, View view,
    int position, long id) {
});

OR you can write this function which will handle the clicking on the list items

protected void onListItemClick(ListView l, View v, int position, long id) {}

I have this working code, and I think in your Java code is the problem.

public class NewsListActivity extends Activity implements OnItemClickListener

...

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list);
        listView = (ListView) findViewById(R.id.listView);
        listView.setAdapter(new NewsDataArrayAdapter(NewsListActivity.this, headlines));
        listView.setOnItemClickListener(this);

...

public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
        if (v == null) {
            return;
        }

try this

ListView lv=(ListView)findViewById(yourlistview);
Lv.setonItemClickListener(this);

public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    paret.getItemAtPositon(position);
    //this returns you an object cast it into the same object you passed to your adapter cast to string if values passed were string
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM