问题描述
- 在 list view 中如何执行 action?
- 程序里的listview中有3个image buttons:play、detail、buy。
每一个 image button都有各自的action。那么在 listview 中对每一个 image button 怎样执行 onclick action?
我使用的代码:public class AndroidThumbnailList extends ListActivity{ .......... public class MyThumbnaildapter extends ArrayAdapter<String>{ public MyThumbnaildapter(Context context int textViewResourceIdString[] objects) { super(context textViewResourceId objects); // TODO Auto-generated constructor stub } public View getView(int position View convertView ViewGroup parent) { ......... } } public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); _contentUri = MEDIA_EXTERNAL_CONTENT_URI; initVideosId(); setListAdapter(new MyThumbnaildapter(AndroidThumbnailList.this R.layout.row _videosId)); }}
如何在 list view 创建 action ?
解决方案
你需要创建自己的 Adapter, 然后 inflate 你想用的视图。然后给每个图像分配一个 OnClick 监听。
给你一个我之前使用的例子
public class GroupListAdapter extends BaseAdapter {private List<Group> groups;// ... constructors here@Overridepublic int getCount() { return groups.size();}@Overridepublic Group getItem(int position) { return groups.get(position);}@Overridepublic long getItemId(int position) { return position;}@Overridepublic View getView(final int position View convertView final ViewGroup parent) { final Group group = getItem(position); final View view; if (convertView == null) view = LayoutInflater.from(parent.getContext()).inflate(R.layout.group null); else view = convertView; view.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // do stuff when the rest of the view is clicked } }); TextView tv = (TextView) view.findViewById(R.id.group_name); tv.setText(group.getName()); final CheckBox check = (CheckBox) view.findViewById(R.id.group_checkbox); check.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // do stuff when clicked } }); return view;}}
解决方案二:
在Adapter中,查找所用子项布局中的imagebutton,然后每个添加listen监听,就Ok了!好久没做安卓了!
时间: 2024-11-18 00:47:21