gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 04:20:15 24 4
gpt4 key购买 nike

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

主 Activity

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 中,我编写了代码以根据适配器接收到的数组中的元素生成 ListView 。到目前为止,一切正常,显示了 4 个不同的列表项,等等......

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

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

关于java - 单击按钮时从自定义适配器刷新 ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49799817/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com