0

我正在尝试通过使用顶部的按钮在否定列表和肯定列表之间切换来更改 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;

    }
}
4

1 回答 1

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

于 2013-06-04T04:28:08.237 回答