我必须在我的listview行中捕获Button或RelativeLayot上的单击(是一个惰性列表)。 我可以使用此代码执行此操作。
public class LazyAdapter extends BaseAdapter {
private Activity _thisActivity;
private Context _thisContext;
private static LayoutInflater inflater=null;
public ImageLoader imageLoader;
ArrayList<ApplicationObject> apps;
private int pos=0;
public LazyAdapter(Activity activity,Context context, ArrayList<ApplicationObject> app)
{
_thisActivity = activity;
_thisContext = context;
inflater = (LayoutInflater)_thisActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader=new ImageLoader(_thisActivity.getApplicationContext());
apps=app;
}
public int getCount() {
return apps.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent)
{
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.home_listview_line4, null);
pos=position;
TextView title=(TextView)vi.findViewById(R.id.textView1);
title.setText(apps.get(position).getTitle());
ImageView image=(ImageView)vi.findViewById(R.id.imageView1);
imageLoader.DisplayImage(apps.get(position).getImageSource(), image);
TextView description=(TextView)vi.findViewById(R.id.textView2);
description.setText(shortDescription);
RelativeLayout rl = (RelativeLayout)vi.findViewById(R.id.oggettoRL);
rl.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent i = new Intent(_thisContext,AppCompleteDescription.class);
i.putExtra("app", apps.get(pos));
_thisContext.startActivity(i);
}
});
Button bugReport = (Button)vi.findViewById(R.id.button1);
bugReport.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent i = new Intent(_thisContext,BugReportActivity.class);
i.putExtra("id", apps.get(pos).getId());
_thisContext.startActivity(i);
}
});
return vi;
}
}
但是它没有采用正确的行号。如果在列表中插入最后一个值的情况下设置“position”是怎么回事。如何捕获点击的确切项目?
我以这种方式调用适配器:
final ListView list = (ListView)findViewById(R.id.list);
LazyAdapter lazy = new LazyAdapter(_thisActivity,_thisContext,listaApplicationObject);
list.setAdapter(lazy);
答案 0 :(得分:1)
在onListemClick
-
@Override
protected void onListItemClick(ListView l, View v, final int position, long id) {
super.onListItemClick(l, v, position, id);
Log.i("the Item clicked is :: ", position);
}