单击按钮时从自定义适配器刷新 ListView

Refreshing ListView from Custom Adapter on Button Click

我正在实施一个带有赞成票和反对票的评论系统。我创建了 MainActivity,我在其中声明并填充了一个二维数组(ArrayList of Array)和 运行 我的自定义适配器与我的列表视图。

MainActivity

public static ArrayList<String[]> commentDb = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        String[] comments1 = {"Help on slide 21", "5", "12:43"};
        String[] comments2 = {"Speak louder", "14", "11:01"};
        String[] comments3 = {"Slow down", "1", "8:32"};
        String[] comments4 = {"Wear a microphone", "11", "18:11"};

        commentDb.add(comments1); //pushing comment to the 2d array
        commentDb.add(comments2);
        commentDb.add(comments3);
        commentDb.add(comments4);

        ListAdapter myAdapter = new CustomAdapter(this, commentDb); // instantiating custom adapter with comments
        ListView myListView = findViewById(R.id.listComments); //creating a list view
        myListView.setAdapter(myAdapter); //populating list view using custom adapter

    }

在我的 CustomAdapter 中,我编写了代码以根据适配器接收到的数组中的元素生成列表视图。到目前为止,一切正常,显示了 4 个不同的列表项,等等...

但是,我正在尝试实现更新投票值(数组中的 idx 1)的 clickListener。数组中的数字得到更新(注销增量)并且按钮改变颜色,但视图不刷新。当我显然无法再次 return 自定义视图时,我不确定如何在单击按钮时刷新视图。

CustomAdater

public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) {

        LayoutInflater loader = LayoutInflater.from(getContext());
        final View customView = loader.inflate(R.layout.custom_row, parent, false);

        final ArrayList<String[]> comments = MainActivity.commentDb; // getting arraylist of array from main activity

        TextView descriptionView = customView.findViewById(R.id.description); //gets the description ID in the UI
        TextView timeView = customView.findViewById(R.id.time); //
        final TextView votesView = customView.findViewById(R.id.votes); // vote textView
        final ImageButton upvote = customView.findViewById(R.id.upvote);
        final ImageButton downvote = customView.findViewById(R.id.downvote);

        String comment = comments.get(position)[0]; // comment strings are in 1st position of 2d array
        final String vote =  comments.get(position)[1]; //votes are in 2nd position
        String time = comments.get(position)[2]; // time is 3rd position

        descriptionView.setText(comment);
        votesView.setText(vote);
        timeView.setText(time);
        upvote.setImageResource(R.drawable.up_arrow_smol);
        downvote.setImageResource(R.drawable.down_arrow_smol);

        upvote.setOnClickListener( //in here
            new View.OnClickListener(){
                public void onClick(View v){
                    comments.get(position)[1] = Integer.toString(Integer.parseInt(comments.get(position)[1]) + 1); //updating vote value
                    upvote.setColorFilter(Color.argb(230, 255, 150, 0)); //changing button color
                    Log.d("DEBUGGGGGG!!!!!!", comments.get(position)[1]); //debug log, works
                    // refreshing the view so that the arrays with update values are reloaded???
                }
            }
        );
        return customView;
    }

更新数据后,只需在点击事件中调用 notifyDataSetChanged()

upvote.setOnClickListener( //in here
            new View.OnClickListener(){
                public void onClick(View v){
                    comments.get(position)[1] = Integer.toString(Integer.parseInt(comments.get(position)[1]) + 1); //updating vote value
                    upvote.setColorFilter(Color.argb(230, 255, 150, 0)); //changing button color
                    Log.d("DEBUGGGGGG!!!!!!", comments.get(position)[1]); //debug log, works
                    // refreshing the view so that the arrays with update values are reloaded???
                    notifyDataSetChanged();
                }
            }
        );