gpt4 book ai didi

Android自定义 ListView

转载 作者:行者123 更新时间:2023-11-29 02:13:22 25 4
gpt4 key购买 nike

大家好我正在处理为员工显示任务的项目,这些任务需要由员工设置任务状态我通过菜单处理这个以更新统计信息这是数组适配器

public class MyArrayAdapter extends ArrayAdapter<Task> {
private static int viewCount = 0;

public MyArrayAdapter(Context context) {
super(context, R.layout.listview_items, R.id.taskTitle);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
boolean created = false;
if (convertView == null) {

created = true;
viewCount++;
}

View view = super.getView(position, convertView, parent);

Task task = getItem(position);
if (task != null) {
TextView taskTitle = (TextView) view.findViewById(R.id.taskTitle);
ImageView imageView = (ImageView) view.findViewById(R.id.taskImage);
TextView taskStatus = (TextView) view.findViewById(R.id.taskStatus);
TextView taskDate = (TextView) view.findViewById(R.id.taskDate);


if (created && taskTitle != null) {
taskTitle.setText(task.getTaskTitle());
}
if (imageView != null && task.image != null) {
imageView.setImageDrawable(task.image);
}
if (taskStatus != null && task.taskStatus != null) {
taskStatus.setText(task.getTaskStatus());
}
if (taskDate != null && task.taskDate != null) {
taskDate.setText(task.getTaskDate());
}
}
return view;
}

我需要更改 TextView “taskStatus”,我尝试这样做

        View v = adapter
.getView(listView.getSelectedItemPosition(),null , null);
TextView textView = (TextView) v.findViewById(R.id.taskStatus);
textView.setText("Started");
adapter.notifyDataSetChanged();

但是它不起作用任何人都可以帮助我

最佳答案

您应该从代码中删除以下行:

View v = adapter.getView(listView.getSelectedItemPosition(),null , null);
TextView textView = (TextView) v.findViewById(R.id.taskStatus);
textView.setText("Started");

并改为确定选定的 Task 实例:task,和

task.setTaskStatus("Started");
adapter.notifyDataSetChanged();

通过这种方式,您可以更改基础数据,并让适配器显示正确的 View (正确更新适当的 TextView,通过将此更改通知它;这就是 notifyDataSetChanged 方法。

关于Android自定义 ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5926926/

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