2011-05-08 50 views
2

您好所有 我就项目合作,以显示员工的任务,而这些任务需要设置员工我通过菜单处理这使更新统计这个任务的状态是阵列适配器的Android定制的ListView

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

但它dosnt工作,任何一个能帮助我PLZ

+1

不直接更改视图 - 更改数组中的值。 – jkhouw1 2011-05-08 11:05:18

回答

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

+0

感谢您的回答,我通过'\t获得了任务对象int taskPo = listView.getSelectedItemPosition(); \t \t任务任务= adapter.getItem(taskPo);'这是正确的吗? – khwileh 2011-05-08 12:26:53

+0

应该是。并且在适配器中使用的List上调用get方法并将其作为参数的参数也应该可以工作! – rekaszeru 2011-05-08 12:35:40

+0

非常感谢:) – khwileh 2011-05-08 12:48:56