我必须在我的列表视图行(是一个惰性列表)中捕捉一个按钮或一个 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;
}
}
但它没有采用正确的行号..如果“位置”设置为插入列表中的最后一个值。我怎样才能捕捉到点击的确切项目?
我以这种方式调用适配器:
final ListView list = (ListView)findViewById(R.id.list);
LazyAdapter lazy = new LazyAdapter(_thisActivity,_thisContext,listaApplicationObject);
list.setAdapter(lazy);