2

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

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

}

我需要更改文本视图“taskStatus”,我尝试这样做

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

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

4

1 回答 1

1

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

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方法的作用。

于 2011-05-08T11:33:52.343 回答