1

My aim is to push the list item to the end of the list when the check box is enabled. My list item is an object "Task".

ListView lv=this.getListView();
int size=lv.getCount();
Myarrayadapter adapter = new Myarrayadapter(this,getTasks());   
lv.setAdapter(adapter);
lv.invalidateViews();


private List<Task> getTasks() {
List<Task> list = new ArrayList<Task>();
for(int k=0;k<taskname.length;k++)//taskname is an array of tasknames
{
list.add(new Task(taskname[k],status[k]));
    return list;
}

Myarrayadapter class is shown below.my aim is to check whether checkbox is enabled.If yes, make some changes(text Colour and button text) and move this changed view to the last position.

public class Myarrayadapter extends ArrayAdapter<Task> {
private final Activity context;
private final List<Task> list;

public Myarrayadapter(Activity context, List<Task> list) {
super(context, R.layout.list_item, list);
this.context = context;
this.list=list;
}

public View getView(final int position, View convertView, ViewGroup parent) {

LayoutInflater inflater = context.getLayoutInflater();
View rowView = inflater.inflate(R.layout.list_item, null, true);
final TextView text = (TextView) rowView.findViewById(R.id.label);
CheckBox checkbox = (CheckBox) rowView.findViewById(R.id.cb);
final Button btnChild = (Button) rowView.findViewById(R.id.directions);
text.setText(list.get(position).getName());
checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            // TODO Auto-generated method stub
                if(isChecked)
                {
                    text.setTextColor(Color.parseColor("#468765"));
                    btnChild.setText("Revert");
                    Task taskItem= list.get(position);
                    list.remove(position);
                    list.add(taskItem);

                }
        }});

    return rowView;
}

Its not moving the exact listitem I wanted.

Thanks in Advance!!!!

3 Answers 3

1

You need to call notifyDataSetChanged() to update the listview:

list.remove(position);
list.add(taskItem);
notifyDataSetChanged();   // add this line after adding task again at last position
Sign up to request clarification or add additional context in comments.

3 Comments

@siraj: since notifyDataSetChanged(); belongs to Adapter, then you have to call just notifyDataSetChanged(); (without variable or Myarrayadapter.this.notifyDataSetChanged();)
@AdilSoomro yes adil you may right, as he need to make a call of notifyDataSetChanged() in adapter itself so i think he just need to call notifyDataSetChanged()
Thanks it works.The item is pushed to the end of the list.But the changes i have made in colour and button is not maintained.pls help me.
1

Try this (didn't write your whole adapter but you see the parts you need to replace):

        View v = convertView;

        if(v == null){
            LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.list_item, null);
        }

        final Task task = getItem(position);
        if(isChecked)
        {
              text.setTextColor(Color.parseColor("#468765"));
              btnChild.setText("Revert");
              list.remove(task);
              list.add(task);
              notifyDataSetChanged();
        }
       return v;

To get the new changes of the button and color you'll need to need to setColor and setText (create your own setColor() and setText() in Task.class) on the Task after removing it, then add it and make if else statements in adapter (by using getColor() and getText()), to check which color the task has and which text.

Comments

0

You should Override some method in your ArrayAdapter

@Override
public int getCount() { 
    return list.size();
}

@Override
public Object getItem(int position) {
    return list.get(position);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.