在列表视图中创建动态复选框并在Android中编写点击事件有什么好的方法?
What is the good way to create dynamic check boxes in list view and write click event in Android?
这是我的场景。
我有一个扩展 ListActivity
class 的 class。在那个 class 中,我收到一个 JSON String
。使用它我使 HashMap
包含这些值。然后我将那些 HashMap
个对象添加到 ArrayList
。然后我制作我的列表视图。要创建 ListView
我扩展 ArrayAdapter
class.
认为 hashmap
对象只包含一个名称。当我创建视图时,我会在该名称中显示一个复选框。 hashmap
个对象的数量可以动态更改。
我的列表视图是这样的......
================================
name 1 []
================================
name 2 []
================================
name 3 []
================================
([] is my check box )
我可以制作这个列表 view.But 我如何将点击事件写入那些复选框??
我唯一能做的就是,在扩展 ListActivity
的 class 中为列表视图编写一个 onListItemClick(ListView l, View v, int position, long id)
方法。
单击复选框时我如何编写事件。
或者我想改变我制作列表视图的方式吗??
这是我的ArrayAdapter
Class.......
public class TestClass1 extends
ArrayAdapter<ArrayList<HashMap<String, String>>> {
private Activity context;
private ArrayList<HashMap<String, String>> myArrayList;
private LayoutInflater inflater;
private TextView mName;
private CheckBox mCheckBox;
public TestClass1(Activity context,
ArrayList<HashMap<String, String>> myArrayList) {
super(context, R.layout.list_view_category_type);
this.context = context;
this.myArrayList= myArrayList;
inflater = LayoutInflater.from(this.context);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null)
v = inflater.inflate(R.layout.list_view_category_type_save, null);
mName = (TextView) v.findViewById(R.id.my_name);
mCheckBox = (CheckBox) v.findViewById(R.id.my_check_box);
categoryName.setText(myArrayList.get(position).get("name"));
return v;
}
@Override
public int getCount() {
return myArrayList.size();
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public ArrayList<HashMap<String, String>> getItem(int position) {
return super.getItem(position);
}
}
这是我的场景。
我有一个扩展 ListActivity
class 的 class。在那个 class 中,我收到一个 JSON String
。使用它我使 HashMap
包含这些值。然后我将那些 HashMap
个对象添加到 ArrayList
。然后我制作我的列表视图。要创建 ListView
我扩展 ArrayAdapter
class.
认为 hashmap
对象只包含一个名称。当我创建视图时,我会在该名称中显示一个复选框。 hashmap
个对象的数量可以动态更改。
我的列表视图是这样的......
================================
name 1 []
================================
name 2 []
================================
name 3 []
================================
([] is my check box )
我可以制作这个列表 view.But 我如何将点击事件写入那些复选框??
我唯一能做的就是,在扩展 ListActivity
的 class 中为列表视图编写一个 onListItemClick(ListView l, View v, int position, long id)
方法。
单击复选框时我如何编写事件。
或者我想改变我制作列表视图的方式吗??
这是我的ArrayAdapter
Class.......
public class TestClass1 extends
ArrayAdapter<ArrayList<HashMap<String, String>>> {
private Activity context;
private ArrayList<HashMap<String, String>> myArrayList;
private LayoutInflater inflater;
private TextView mName;
private CheckBox mCheckBox;
public TestClass1(Activity context,
ArrayList<HashMap<String, String>> myArrayList) {
super(context, R.layout.list_view_category_type);
this.context = context;
this.myArrayList= myArrayList;
inflater = LayoutInflater.from(this.context);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null)
v = inflater.inflate(R.layout.list_view_category_type_save, null);
mName = (TextView) v.findViewById(R.id.my_name);
mCheckBox = (CheckBox) v.findViewById(R.id.my_check_box);
categoryName.setText(myArrayList.get(position).get("name"));
return v;
}
@Override
public int getCount() {
return myArrayList.size();
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public ArrayList<HashMap<String, String>> getItem(int position) {
return super.getItem(position);
}
}