MasterGberry
MasterGberry

Reputation: 2860

Update ListAdapter Dynamically During execution

I am trying to change the ListView data mid-program by using a button at the top that switches between negative and positive lists.

private ArrayList<LifeTask> positiveListData = new ArrayList<LifeTask>();
private ArrayList<LifeTask> negativeListData = new ArrayList<LifeTask>();
private ArrayList<LifeTask> currentListData = positiveListData;
private ListAdapter adapter;
private ListView gameList;

By default I have currentListData pointing to the positive list. In the onCreate() function of the app I have the following code:

adapter = new ListAdapter(this, R.layout.list_item, currentListData);
gameList = (ListView) findViewById(R.id.main_list_view);
gameList.setAdapter(adapter);

When the negative button is pressed I try doing the following:

currentListData = negativeListData;
adapter.notifyDataSetChanged();

but nothing changes. What am i doing wrong?

EDIT: Adapter code added as per requested

package cs.ucsb.cs185.dimberman.lifeRPG;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class ListAdapter extends ArrayAdapter<LifeTask>{
    Context context; 
    int layoutResourceId;    
    ArrayList<LifeTask> data = null;

    public ListAdapter(Context context, int layoutResourceId, ArrayList<LifeTask> List_Data) {
        super(context, layoutResourceId, List_Data);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.data = List_Data;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        ItemHolder holder = null;
        if(row == null)
        {
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);
            holder = new ItemHolder();
            holder.name = (TextView)row.findViewById(R.id.ItemName);
            row.setTag(holder);
        }
        else
        {
            holder = (ItemHolder)row.getTag();
        }

        LifeTask s = data.get(position);
        holder.name.setText(s.getName());
        return row;

    }

    static class ItemHolder{

        TextView name;

    }
}

Upvotes: 0

Views: 2869

Answers (1)

sandrstar
sandrstar

Reputation: 12643

I would suggest to change Your adapter the following way:

public class ListAdapter extends ArrayAdapter<LifeTask> {
    int layoutResourceId;

    public ListAdapter(Context context, int layoutResourceId, ArrayList<LifeTask> List_Data) {
        super(context, layoutResourceId, List_Data);
        this.layoutResourceId = layoutResourceId;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        ItemHolder holder = null;

        if(row == null) {
            LayoutInflater inflater = parent.getContext().getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);
            holder = new ItemHolder();
            holder.name = (TextView)row.findViewById(R.id.ItemName);
            row.setTag(holder);
        } else {
            holder = (ItemHolder)row.getTag();
        }

        final LifeTask s = getItem(position);
        holder.name.setText(s.getName());
        return row;
    }

    static class ItemHolder{
        TextView name;
    }
}

The issue with Your implementation was in having own ArrayList<LifeTask> data. However it's not needed, because parent class ArrayAdapter<LifeTask> already has container for items.

And update the list using code like that:

adapter.clear();
adapter.addAll(negativeListData);

Methods clear() and addAll() will call notifyDataSetChanged().

Upvotes: 4

Related Questions