1

我有一个小应用程序,我正在尝试使用 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

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

01-18 04:09:32.030:W/Trace(973):来自 nativeGetEnabledTags 的意外值:0

4

6 回答 6

3

在我的ListView我有一个可点击TextView和可点击的ImageView. onListItemClick对我不起作用,因为我在列表行中有多个可点击项目。所以我所做的是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,然后再做。

于 2013-01-18T06:59:57.963 回答
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);
}
});
于 2013-01-17T06:56:32.387 回答
1

您的 baseadapter 的 imageview xml 添加此行

android:clickable="false"
android:focusable="false"
于 2013-01-17T06:56:51.843 回答
0
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");
                }
            });
于 2013-01-17T06:51:07.320 回答
0

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

于 2013-01-17T07:00:53.727 回答
0

如果您有一个名为 ListActivity 的类,请确保您从正确的 listActivity 扩展,因为在扩展时,您的 ListActivity 类和超类 ListActivity 都会出现

于 2017-05-18T16:20:36.107 回答