我创建了一个自定义列表视图,并在product_name
和code
上设置了文字。
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View listViewItem = inflater.inflate(R.layout.product_list_item, null, true);
TextView txtProduct = (TextView) listViewItem.findViewById(R.id.textProduct);
TextView txtCode = (TextView) listViewItem.findViewById(R.id.textCode);
txtProduct.setText(product_name[position]);
txtCode.setText(code[position]);
return listViewItem;
}
如何设置OnItemClickListener
以从列表视图中获取每个数据并传递给另一个活动。
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
}
});
答案 0 :(得分:0)
Data data=listView.getAdapter().getPosition(arg2);
Intent intent=new Intent(this,newActivity.class);
intent.putExtra("Key",data);
startActivity(intent);
答案 1 :(得分:0)
private final List<Object> mList;
//public constructor, random name
public MyCustomListViewAdapter(Context c, List<Object> list) {
context = c;
this.mList = list;
}
@Override
public int getCount() {
return mList.size();
}
@Override
public Object getItem(int position) {
return mList.get(position);
}
@Override
public long getItemId(int position) {
return mList.get(position).getId();
}
从列表中检索数据时,只需调用
即可listView.getAdapter().getItem(position);
并且if将在您发送的位置返回自定义对象。