原问题来自于CSDN问答频道,更多解决方案见:http://ask.csdn.net/questions/1173
问题描述
程序里的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 textViewResourceId,String[] 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
@Override
public int getCount() {
return groups.size();
}
@Override
public Group getItem(int position) {
return groups.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public 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;
}
}