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

しかし、それは機能しません。誰かが私を助けてくれます

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 に答える