我正在使用upvotes和downvotes实现评论系统。我创建了MainActivity,我只是在其中声明并填充一个二维数组(Array的ArrayList)并使用我的listview运行我的customadapter。
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中,我编写了代码,用于根据适配器接收的数组中的元素生成listview。到目前为止,一切运行良好,4个不同的列表项目显示,等等...
但是,我正在尝试实现一个更新投票值的clickListener(数组中的idx 1)。数组中的数字会更新(注销增量),按钮会更改颜色,但视图不会刷新。当我显然无法再次返回自定义视图时,我不确定如何在点击按钮时刷新视图。
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;
}
答案 0 :(得分:3)
更新数据后,只需在点击事件中调用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();
}
}
);