从Android ListActivity自定义适配器获取对应于CheckedTextView的项
我有以下自定义适配器从Android ListActivity自定义适配器获取对应于CheckedTextView的项,android,android-listview,adapter,Android,Android Listview,Adapter,我有以下自定义适配器 public class GroupAdapter extends ArrayAdapter<Group> { private ArrayList<Group> items; public HashMap<String, String> checkedItems = new HashMap<String, String>(); ... public void setCheckedItem(int
public class GroupAdapter extends ArrayAdapter<Group> {
private ArrayList<Group> items;
public HashMap<String, String> checkedItems = new HashMap<String, String>();
...
public void setCheckedItem(int item) {
}
public HashMap<String, String> getCheckedItems() {
return checkedItems;
}
...
@Override
public View getView(int position, View convertView, ViewGroup parent) {
...
View v = convertView;
LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.list_groups, null);
Group bs = items.get(position);
CheckedTextView ctView = (CheckedTextView) v.findViewById(R.id.listCheckboxview);
ctView.setText(bs.getGroupName());
// ctView.setId(bs.getId());
ctView.setId(position);
ctView.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
((CheckedTextView) v).toggle();
if(((CheckedTextView) v).isChecked()) {
Log.e(MY_DEBUG_TAG, "Checked");
// I need to get the item currespond to this view to call Adapters's setCheckedItem(item) method
}
}
});
公共类GroupAdapter扩展了ArrayAdapter{
私有ArrayList项;
public HashMap checkedItems=new HashMap();
...
公共无效setCheckedItem(整数项){
}
公共HashMap getCheckedItems(){
返回支票;
}
...
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
...
视图v=转换视图;
LayoutInflater vi=(LayoutInflater)getContext().getSystemService(Context.LAYOUT\u INFLATER\u SERVICE);
v=vi.充气(R.布局.列表组,空);
组bs=项目。获取(位置);
CheckedTextView ctView=(CheckedTextView)v.findViewById(R.id.listCheckboxview);
ctView.setText(bs.getGroupName());
//ctView.setId(bs.getId());
ctView.setId(位置);
ctView.setOnClickListener(新视图.OnClickListener(){
公共void onClick(视图v){
((CheckedTextView)v).toggle();
如果((CheckedTextView)v).isChecked()){
Log.e(我的调试标签“Checked”);
//我需要获取该视图对应的项currespond来调用适配器的setCheckedItem(项)方法
}
}
});
布局呢
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="?android:attr/listPreferredItemHeight"
android:paddingLeft="5dip" android:paddingRight="5dip"
android:paddingTop="10dip" android:paddingBottom="10dip"
android:fastScrollEnabled="true"
>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/listCheckboxview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" android:gravity="center|left"
android:textColor="#0075AB" android:textStyle="bold" android:textSize="14dip"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
android:paddingLeft="6dip"
android:paddingRight="6dip"
android:clickable="true"
android:focusable="true"
android:text=""
/>
</LinearLayout>
我能够为
CheckedTextView
捕获setOnClickListener
,在监听器内部,我需要获取与单击的CheckedTextView
对应的项,以调用适配器的setCheckedItem(项)
方法。(请查看适配器类中的注释)您应该设置checkedTextView的标记。像
ctView.setTag(items.get(position));
然后在onClick()
中执行v.getTag()
将为您提供项目组。您说项目是什么意思…?您这里只有一个checkedTextView..@ntc这是您考虑过使用列表视图的单选/多选模式吗?