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