从 Android ListActivity 自定义适配器获取与 CheckedTextView 对应的项目
我有以下自定义适配器
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
}
}
});
和布局,
<?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(item)
方法。 (请参阅适配器类中的注释)
I 've the following custom adapter
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
}
}
});
and the layout
<?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>
Im' able to capture the setOnClickListener
for the CheckedTextView
, Inside the listener, I need to get the item correspond to clicked CheckedTextView
to call Adapters's setCheckedItem(item)
method. (Please see the comments in the adapter class)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该设置checkedTextView的标签。
就像 ctView.setTag(items.get(position)); 然后在
onClick()
中执行v.getTag()
会给你项目组。You should set the tag of the checkedTextView.
Like
ctView.setTag(items.get(position));
then in theonClick()
doingv.getTag()
will give you the item group.