列表视图显示以前的选择[ANDROID]
问题描述:
在我的应用程序中,我显示列表视图中的一些项目,每行有一个名称和一个刻度图像。勾选图像只显示当选择项目(选中),选择工作正常,但问题是,当一个新的项目被选中时,先前选择的项目的勾号不会不可见。这可以如何解决?任何帮助将这里了解 是我的代码列表视图显示以前的选择[ANDROID]
适配器
public class ListAdapter extends BaseAdapter {
private LayoutInflater inflater;
Context context;
private ArrayList<String> responseList;
private LinearLayout parent_choice_list_container;
private TextView item_name;
private ImageView iv_choice_list_item_selected;
public ListAdapter (Context context, ArrayList<String> responseList) {
this.context = context;
this.responseList = responseList;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public void setSelectedIndex(int ind) {
selectedIndex = ind;
notifyDataSetChanged();
}
@Override
public int getCount() {
return responseList.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
view = inflater.inflate(R.layout.menu_list_item, null);
}
parent_choice_list_container = (LinearLayout) view.findViewById(R.id.parent_choice_list_container);
item_name = (TextView) view.findViewById(R.id.item_name);
iv_choice_list_item_selected = (ImageView) view.findViewById(R.id.iv_choice_list_item_selected);
iv_logo = (ImageView) view.findViewById(R.id.iv_blog_category_logo);
if (responseList.get(position).isSelected()) {
iv_choice_list_item_selected.setVisibility(View.VISIBLE);
} else {
iv_choice_list_item_selected.setVisibility(View.GONE);
} list_item_name.setText(responseList.get(position).getName());
return view;
}
}
FragmentPart(项目点击监听器)
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
response.get(position).setSelected(true);
ListAdapter.notifyDataSetChanged();
}
答
应设置标签为每个图像以跟踪其状态的(0 =未选择/不可见,1 =选择/可见),例如:
if (responseList.get(position).isSelected()) {
iv_choice_list_item_selected.setTag(1);
}
并可以添加这样的:
if (iv_choice_list_item_selected.getTag() == 1) {
iv_choice_list_item_selected.setVisibility(View.VISIBLE);
} else {
iv_choice_list_item_selected.setVisibility(View.GONE);
}
+0
没有它的不工作,它反而显示在任意位置的勾号图像,甚至没有被选中 – User
选中此链接(http://www.androhub.com/android-listview-radiobutton/)。 –