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

乔恩·高

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

主要活动

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 个不同的列表项,等等......

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

自定义适配器

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何为随机自定义ListView适配器设置按钮单击事件?

来自分类Dev

ListView的自定义适配器

来自分类Dev

自定义ListView的适配器

来自分类Dev

使用自定义 ListView 适配器刷新 Fragment ListView

来自分类Dev

如何在自定义适配器中刷新ListView?

来自分类Dev

从自定义适配器更改数据后刷新listview

来自分类Dev

使用扩展BaseAdapter的自定义适配器刷新ListView

来自分类Dev

获取一个ListView刷新?YouTube API-自定义适配器

来自分类Dev

如何使用自定义基本适配器和sqlite数据库刷新Listview?

来自分类Dev

将按钮添加到 xml 文件时,使用自定义适配器的 ListView 会被覆盖

来自分类Dev

ListView /自定义数组适配器-调用getView时适配器设置为null

来自分类Dev

无法使用自定义适配器单击ListView Android

来自分类Dev

自定义适配器,然后单击ListView中的项目

来自分类Dev

无法使用自定义适配器单击ListView Android

来自分类Dev

ListView自定义适配器丢失imageView

来自分类Dev

自定义适配器显示RuntimeException-ListView

来自分类Dev

使用自定义适配器过滤ListView

来自分类Dev

自定义listview适配器上的OnclickListner

来自分类Dev

ListView自定义适配器重复项

来自分类Dev

自定义基础适配器到ListView

来自分类Dev

使用自定义适配器从listview搜索项目

来自分类Dev

Android ListView onClickListener自定义适配器

来自分类Dev

使用自定义适配器未显示ListView

来自分类Dev

自定义ListView适配器中的NullPointerException

来自分类Dev

ListView自定义适配器丢失imageView

来自分类Dev

Android自定义ListView /适配器

来自分类Dev

在自定义适配器中遍历ListView

来自分类Dev

Android ListView onItemclick以及自定义适配器

来自分类Dev

在自定义ListView适配器中按项目时突出显示效果

Related 相关文章

  1. 1

    如何为随机自定义ListView适配器设置按钮单击事件?

  2. 2

    ListView的自定义适配器

  3. 3

    自定义ListView的适配器

  4. 4

    使用自定义 ListView 适配器刷新 Fragment ListView

  5. 5

    如何在自定义适配器中刷新ListView?

  6. 6

    从自定义适配器更改数据后刷新listview

  7. 7

    使用扩展BaseAdapter的自定义适配器刷新ListView

  8. 8

    获取一个ListView刷新?YouTube API-自定义适配器

  9. 9

    如何使用自定义基本适配器和sqlite数据库刷新Listview?

  10. 10

    将按钮添加到 xml 文件时,使用自定义适配器的 ListView 会被覆盖

  11. 11

    ListView /自定义数组适配器-调用getView时适配器设置为null

  12. 12

    无法使用自定义适配器单击ListView Android

  13. 13

    自定义适配器,然后单击ListView中的项目

  14. 14

    无法使用自定义适配器单击ListView Android

  15. 15

    ListView自定义适配器丢失imageView

  16. 16

    自定义适配器显示RuntimeException-ListView

  17. 17

    使用自定义适配器过滤ListView

  18. 18

    自定义listview适配器上的OnclickListner

  19. 19

    ListView自定义适配器重复项

  20. 20

    自定义基础适配器到ListView

  21. 21

    使用自定义适配器从listview搜索项目

  22. 22

    Android ListView onClickListener自定义适配器

  23. 23

    使用自定义适配器未显示ListView

  24. 24

    自定义ListView适配器中的NullPointerException

  25. 25

    ListView自定义适配器丢失imageView

  26. 26

    Android自定义ListView /适配器

  27. 27

    在自定义适配器中遍历ListView

  28. 28

    Android ListView onItemclick以及自定义适配器

  29. 29

    在自定义ListView适配器中按项目时突出显示效果

热门标签

归档