动态更新ListAdapter执行期间

时间:2013-06-04 03:47:42

标签: android

我正在尝试使用顶部的按钮在正面和正面列表之间切换,从而在程序中更改ListView数据。

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;

默认情况下,我currentListData指向肯定列表。在应用的onCreate()功能中,我有以下代码:

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

按下否定按钮时,我尝试执行以下操作:

currentListData = negativeListData;
adapter.notifyDataSetChanged();

但没有任何改变。我做错了什么?

编辑:根据请求添加适配器代码

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;

    }
}

1 个答案:

答案 0 :(得分:4)

我建议您通过以下方式更改您的适配器:

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

您的实施问题在于拥有自己的ArrayList<LifeTask> data。但是,它不是必需的,因为父类ArrayAdapter<LifeTask>已经有项目容器。

使用以下代码更新列表:

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

方法clear()addAll()会调用notifyDataSetChanged()