hello all我正在为员工显示任务的项目,这些任务需要由员工设置任务状态,我通过菜单处理以更新统计信息这是阵列适配器
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;
}
}
我需要更改文本视图"任务状态",我尝试这样做
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();
这样,您可以更改基础数据,并通过通知适配器有关此更改的信息,让适配器显示正确的视图(正确更新相应的TextView
;这就是 notifyDataSetChanged
方法的作用。