onListItemClick是没有得到所谓的ListActivity(onListItemClic

2019-07-18 08:05发布

我有一个小应用程序,我想使用ListActivity显示YouTube的缩略图列表

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");

}

该XML看起来是这样的

<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>

适配器:

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;
}

...

}

我不能够得到onListItemClick调用。 能否请你让我知道如果我作出任何错误?

1/17更新

当我点击没有任何反应,但我看到这些日志中

4月1日至18日:09:32.030:W /跟踪(973):意外从nativeGetEnabledTags值:0

Answer 1:

在我ListView我有一个可点击TextView和点击ImageViewonListItemClick没有为我工作,因为我有列表行中不止一个可点击的项目。 所以我所做的就是我实现onClickListener在我getView()的适配器。 这里是我的代码:

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;

}

}

它发生,因为我们有一个以上的可点击的项目,我们正在尝试使用ListActivity 。 否则的另一种选择是延长Activity类的MainActivity然后再去做。



Answer 2:

对于ListActivity其简单的调用setOnItemClickListener

对应的代码是下面

list.setOnItemClickListener(new OnItemClickListener() {

public void onItemClick(AdapterView<?> arg0,
                    View arg1, int position, long arg3)
{
   System.out.println("LIST ITEM POSITION "+position);
}
});


Answer 3:

baseadapter你imageview的XML添加此行

android:clickable="false"
android:focusable="false"


Answer 4:

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");
                }
            });


Answer 5:

您需要添加row.setOnClickListener(new OnItemClickListener(position)); 在ResultViewAdapter的getView()



Answer 6:

如果你有一个名为ListActivity类确保你从右边listActivity延长,因为延长的时候,无论你的类ListActivity和超类ListActivity将显示



文章来源: onListItemClick is not getting called on ListActivity