【问题标题】:Listview showing previous selection [ANDROID]显示先前选择的列表视图 [ANDROID]
【发布时间】:2017-07-14 12:57:15
【问题描述】:

在我的应用程序中,我在列表视图中显示了一些项目,每一行都有一个名称和一个刻度图像。 Tick image only shows up when item is selected(checked), The selection is working fine but the problem is when a new item is selected tick of previously selected item doesn't goes invisible.如何解决这个问题?任何帮助将不胜感激 这是我的代码

适配器

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(); 
}

【问题讨论】:

标签: android listview android-adapter android-adapterview


【解决方案1】:

听起来您使用的是CheckBox

您是否尝试过根据项目 isChecked(和 不是 isSelected)有条件地更新 Views 的可见性?

【讨论】:

    【解决方案2】:

    您应该为每个图像设置标签以跟踪其状态(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);
        }            
    

    【讨论】:

    • 不,它不起作用,而是在随机位置显示刻度图像,甚至没有被选中
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多