khwileh
khwileh

Reputation: 39

Android custom listview

hello all i am working on project to display tasks for employee , and these tasks needs to sets the tasks status by employees i handle this by menus to make update stats this is the array adapter

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

}

i need to change the textview "taskStatus" , i try to do this

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

but it dosnt work any one can help me plz

Upvotes: 2

Views: 425

Answers (1)

rekaszeru
rekaszeru

Reputation: 19220

You should remove the following lines from your code:

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

and instead determine the selected Task instance: task, and

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

This way you change the underlying data, and let the adapter show the correct view (update the appropriate TextView correctly, by notifying it about this change; this is what the notifyDataSetChanged method does.

Upvotes: 1

Related Questions