简体   繁体   中英

Refreshing ListView from Custom Adapter on Button Click

I'm implementing a comment system with upvotes and downvotes. I created the MainActivity, in which I just declare and populate a 2d array (ArrayList of Array) and run my customadapter with my listview.

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

    }

In my CustomAdapter, I wrote code to generate the listview based on elements from the array the adapter receives. Up to here, everything works well, the 4 distinct list items get displayed, etc...

However, I am trying to implement a clickListener that updates the vote value (idx 1 in the array). The number in the array gets updated (logs out the increments) and the button changes color, but the view doesn't refresh. I'm not sure how to refresh the view when clicking on the button when I obviously can't return custom view once again.

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

Just call notifyDataSetChanged() inside your click event after updating the data.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM