突出显示项

时间:2016-06-20 12:32:19

标签: android listview android-arrayadapter

我在其布局中有一个DialogFragmentListView,其目的是作为单项选项列表。我使用selectedItemId参数对其进行实例化,然后将其设置为列表的ArrayAdapter,以便我可以在getView()方法中突出显示最新选择的项目,如下所示:

    public ItemSelectionListAdapter(Context context, List<Item> items, String selectedItemId) {
        super(context, RESOURCE);
        this.items = items;
        this.selectedItemId = selectedItemId;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = View.inflate(getContext(), RESOURCE, null);
        }

        Item item = items.get(position);

        TextView textView = (TextView) convertView;
        textView.setText(item.getName());

        String itemId = item.getId();

        Log.d(TAG, "Current item id: " + itemId);
        Log.d(TAG, "Selected item id: " + selectedItemId);

        if (itemId.equals(selectedItemId)) {
            textView.setTextColor(ContextCompat.getColor(getContext(), R.color.primaryColor));
            textView.setTypeface(textView.getTypeface(), Typeface.BOLD);
        }

        return convertView;
    }

默认项目(当尚未选择任何项目时)位于0位置,因此该图像显示预期视图:

enter image description here

但是,当我选择其他项时,两者都会突出显示:

enter image description here

代码非常简单,但位置:0 的项目始终会突出显示。我已经检查过selectedId是否已更新,是的,它始终是最新选择的项目ID。我还检查了所有项目都有唯一ID,是的,他们有:

这是对话框第一次显示的日志:

06-20 14:26:37.677 7860-7860/xxx D/ADAPTER: getCount() -> 6
06-20 14:26:37.677 7860-7860/xxx D/ADAPTER: Current item id: -2
06-20 14:26:37.678 7860-7860/xxx D/ADAPTER: Selected item id: -2
06-20 14:26:37.678 7860-7860/xxx D/ADAPTER: Current item id: 3
06-20 14:26:37.678 7860-7860/xxx D/ADAPTER: Selected item id: -2
06-20 14:26:37.678 7860-7860/xxx D/ADAPTER: Current item id: 4
06-20 14:26:37.679 7860-7860/xxx D/ADAPTER: Selected item id: -2
06-20 14:26:37.679 7860-7860/xxx D/ADAPTER: Current item id: 5
06-20 14:26:37.679 7860-7860/xxx D/ADAPTER: Selected item id: -2
06-20 14:26:37.680 7860-7860/xxx D/ADAPTER: Current item id: 6
06-20 14:26:37.680 7860-7860/xxx D/ADAPTER: Selected item id: -2
06-20 14:26:37.680 7860-7860/xxx D/ADAPTER: Current item id: 7
06-20 14:26:37.680 7860-7860/xxx D/ADAPTER: Selected item id: -2

这是点击某个项目后的另一次:

06-20 14:28:24.759 7860-7860/xxx D/ADAPTER: getCount() -> 6
06-20 14:28:24.760 7860-7860/xxx D/ADAPTER: Current item id: -2
06-20 14:28:24.760 7860-7860/xxx D/ADAPTER: Selected item id: 4
06-20 14:28:24.760 7860-7860/xxx D/ADAPTER: Current item id: 3
06-20 14:28:24.761 7860-7860/xxx D/ADAPTER: Selected item id: 4
06-20 14:28:24.761 7860-7860/xxx D/ADAPTER: Current item id: 4
06-20 14:28:24.761 7860-7860/xxx D/ADAPTER: Selected item id: 4
06-20 14:28:24.762 7860-7860/xxx D/ADAPTER: Current item id: 5
06-20 14:28:24.762 7860-7860/xxx D/ADAPTER: Selected item id: 4
06-20 14:28:24.762 7860-7860/xxx D/ADAPTER: Current item id: 6
06-20 14:28:24.763 7860-7860/xxx D/ADAPTER: Selected item id: 4
06-20 14:28:24.763 7860-7860/xxx D/ADAPTER: Current item id: 7
06-20 14:28:24.763 7860-7860/xxx D/ADAPTER: Selected item id: 4

我做错了什么?

修改 单击一个项目后,其id将存储在活动中(它将是下一个selectedItemId)并且对话框将被销毁。因此适配器每次都会创建一个项目。

编辑2: 这是对话框的重要代码,每次创建对话框时,我都会实例化适配器并将其设置为ListView

    @Bind(R.id.item_list)
    protected ListView list;

    private String selectedItemId;
    private List<Item> items;

    public static ItemSelectionDialog newInstance(String selectedItemId) {
        ItemSelectionDialog dialog = new ItemSelectionDialog();

        Bundle args = new Bundle();
        args.putString(ARG_ITEM_ID, selectedItemId);
        dialog.setArguments(args);

        return dialog;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        selectedItemId = getArguments().getString(ARG_ITEM_ID);
        items = getItemsFromDataBase();
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        View view = View.inflate(getContext(), R.layout.dialog_item_selection, null);
        ButterKnife.bind(this, view);

        // Here I instantiate the adapter with a different selectedItemId each time
        list.setAdapter(new ItemSelectionListAdapter(getContext(), items, selectedItemId));

        return new AlertDialog.Builder(getActivity())
            .setView(view)
            .create();
    }

    // This is a normal OnItemClickListener attached to the ListView
    @OnItemClick(R.id.item_list)
    public void onItemClick(int position) {
        // Here I store the ID in the host activity, which implements a custom "OnItemSelectedListener"
        listener.onItemSelected(items.get(position));
        dismiss();
    }

2 个答案:

答案 0 :(得分:1)

您有选择的代码,但不能删除该选择。还有什么部分?第一次第0个位置项目将突出显示,下次选择其中一个,但您没有从之前的第0个位置删除选择

if (itemId.equals(selectedItemId)) {
        textView.setTextColor(ContextCompat.getColor(getContext(), R.color.primaryColor));
        textView.setTypeface(textView.getTypeface(), Typeface.BOLD);
    }else{
 textView.setTextColor(ContextCompat.getColor(getContext(),R.color.black));
        textView.setTypeface(textView.getTypeface(), Typeface.NORMAL);
 }

答案 1 :(得分:0)

如果每次都在重新创建对话框的listView 向Item类添加另一个布尔值,默认情况下它将为false 你应该将onItemClick添加到listView并更新你的项目数据结构 根据onItemClick回调的位置,确保在更新项目数据结构中的选定项目之前将所有选中的项目字段初始化为false。 你需要做的最后一件事只是添加@ Pr38y所评论的内容,但稍加改动而不是使用

if(itemId.equals(selectedItemId))

使用这个:

if(item.isSelected())