onListItemClick is not getting called on ListActiv

2020-03-31 02:31发布

I have a small application, and I am trying to display list of youtube thumbnails using the ListActivity

public class ResultListActivity extends ListActivity {
.....

......
@Override
    protected void onCreate(Bundle savedInstanceState) {

loadData();


---
---

}


private void loadData(final String searchQuery) {
        AsyncTask<Object, Void, List<YouTubeVideos>> asyncTask = new AsyncTask<Object, Void, List<YouTubeVideos>>() {

.....

}

/** {@inheritDoc} */
    @Override  
    protected void onListItemClick(ListView l, View v, int pos, long id) {  
        super.onListItemClick(l, v, pos, id);

        Log.d("test","Inside onListItemClick");

}

The xml looks like this

<ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true" >
    </ListView>



   <ImageView
    android:id="@+id/imageView1" 
    android:layout_height="wrap_content"
    android:layout_width="100dp" 
    android:focusable="false"
    android:focusableInTouchMode="false"
    >
</ImageView>

Adapter:

public class ResultViewAdapter extends BaseAdapter {

@Override
    public View getView(int position, View convertView, ViewGroup parent) {

ImageView imageView = (ImageView) row.findViewById(R.id.imageView1);

        imageView.setImageDrawable(torvideo.getDrawable());





        return row;
}

...

}

I am not able to get the onListItemClick invoked. Could you please let me know if I am making any mistake?

Update 1/17

When I click nothing happens but I see these in logs

01-18 04:09:32.030: W/Trace(973): Unexpected value from nativeGetEnabledTags: 0

6条回答
不美不萌又怎样
2楼-- · 2020-03-31 02:44

your imageview xml of baseadapter add this line

android:clickable="false"
android:focusable="false"
查看更多
闹够了就滚
3楼-- · 2020-03-31 02:52
ListView lv = getListView();
 lv.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> arg0, View view, int arg2,
                        long arg3) {
                     Log.d("test","Inside onListItemClick");
                }
            });
查看更多
一夜七次
4楼-- · 2020-03-31 02:53

For ListActivity its simple to call setOnItemClickListener

The corresponding code is below

list.setOnItemClickListener(new OnItemClickListener() {

public void onItemClick(AdapterView<?> arg0,
                    View arg1, int position, long arg3)
{
   System.out.println("LIST ITEM POSITION "+position);
}
});
查看更多
爷的心禁止访问
5楼-- · 2020-03-31 02:54

You need to add row.setOnClickListener(new OnItemClickListener(position)); in your ResultViewAdapter for getView()

查看更多
Viruses.
6楼-- · 2020-03-31 03:00

In my ListView I have an clickable TextView and clickable ImageView. onListItemClick didn't work for me since I have more than one clickable items in the list row. So what I did is i implemented onClickListener in my getView() for the adapter. Here is my code:

public class SimpleArrayAdapter extends ArrayAdapter<String>{

private LayoutInflater mInflater;
private String[] mStrings;
private TypedArray mIcons;
private int mViewResourceId;

public SimpleArrayAdapter(Context context, int textViewResourceId, String[] names, TypedArray icons) {
    super(context, textViewResourceId, names);
    // TODO Auto-generated constructor stub

    mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    mStrings = names;
    mIcons = icons;
    mViewResourceId = textViewResourceId;
}


public int getCount(){
    return mStrings.length;
}

public View getView(final int position, View convertView, ViewGroup parent){
    convertView = mInflater.inflate(mViewResourceId, null);

    ImageView iv = (ImageView)convertView.findViewById(R.id.option_icon);
    iv.setImageDrawable(mIcons.getDrawable(position));
    iv.setTag("colorChooserClicked");

    iv.setOnClickListener( new OnClickListener() {
        int pos = position;
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Log.v("text", "Image clicked, row %d"+pos);

        }
    });
    TextView tv = (TextView)convertView.findViewById(R.id.textview01);
    tv.setText(mStrings[position]);
    tv.setTag("textViewClicked");
    tv.setOnClickListener(new OnClickListener() {
        int textpos = position;
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Log.v("text", "Title clicked  %s"+mStrings[textpos]);
        }
    });
    return convertView;

}

}

It happens since we have more than one clickable item and we are trying to use the ListActivity. Else the other option is to extend Activity class for your MainActivity and then do it.

查看更多
虎瘦雄心在
7楼-- · 2020-03-31 03:04

if you have a class called ListActivity make sure you are extending from the right listActivity because when extending, both your class ListActivity and the super class ListActivity will show up

查看更多
登录 后发表回答