我有一个小应用程序,我正在尝试使用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
答案 0 :(得分: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
课程,然后再进行。{/ p>
答案 1 :(得分: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);
}
});
答案 2 :(得分:1)
你的imageview xml of baseadapter添加这一行
android:clickable="false"
android:focusable="false"
答案 3 :(得分: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");
}
});
答案 4 :(得分:0)
您需要在ResultViewAdapter中为getView()
添加row.setOnClickListener(new OnItemClickListener(position));
答案 5 :(得分:0)
如果你有一个名为ListActivity的类,请确保从右侧listActivity扩展,因为在扩展时,类ListActivity和超类ListActivity都将显示