本文介绍了更新ListAdapter动态执行过程中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我想通过那个消极和积极列表之间切换的顶部用一个按钮来改变的ListView数据中期计划。 私人的ArrayList&LT; LifeTask&GT; positiveListData =新的ArrayList&LT; LifeTask&GT;();私人的ArrayList&LT; LifeTask&GT; negativeListData =新的ArrayList&LT; LifeTask&GT;();私人的ArrayList&LT; LifeTask&GT; currentListData = positiveListData;私人ListAdapter适配器;私人的ListView gameList; 在默认情况下我有 currentListData 指向肯定列表。在应用程序中,我有以下code的的onCreate()功能: 适配器=新ListAdapter(这一点,R.layout.list_item,currentListData);gameList =(ListView控件)findViewById(R.id.main_list_view);gameList.setAdapter(适配器); 在负按钮pressed我尝试以下操作: currentListData = negativeListData;adapter.notifyDataSetChanged(); 但没有任何变化。我究竟做错了什么?编辑:适配器code加按要求 包cs.ucsb.cs185.dimberman.lifeRPG;进口的java.util.ArrayList;进口android.app.Activity;进口android.content.Context;进口android.view.LayoutInflater;进口android.view.View;进口android.view.ViewGroup;进口android.widget.ArrayAdapter;进口android.widget.TextView;公共类ListAdapter扩展ArrayAdapter&LT; LifeTask&GT; { 上下文语境; INT layoutResourceId; ArrayList的&LT; LifeTask&GT;数据= NULL; 公共ListAdapter(上下文的背景下,诠释layoutResourceId,ArrayList的&LT; LifeTask&GT; List_Data){ 超(背景下,layoutResourceId,List_Data); this.layoutResourceId = layoutResourceId; this.context =背景; this.data = List_Data; } @覆盖 公共查看getView(INT位置,查看convertView,父母的ViewGroup){ 查看排= convertView; ItemHolder支架=无效; 如果(行== NULL) { LayoutInflater充气=((活动)上下文).getLayoutInflater(); 行= inflater.inflate(layoutResourceId,父母,假); 持有人=新ItemHolder(); holder.name =(TextView中)row.findViewById(R.id.ItemName); row.setTag(保持器); } 其他 { 支架=(ItemHolder)row.getTag(); } LifeTask S = data.get(位置); holder.name.setText(s.getName()); 返回行; } 静态类ItemHolder { TextView的名称; }} 解决方案 我建议改变你的适配器方式如下: 公共类ListAdapter扩展ArrayAdapter&LT; LifeTask&GT; { INT layoutResourceId; 公共ListAdapter(上下文的背景下,诠释layoutResourceId,ArrayList的&LT; LifeTask&GT; List_Data){ 超(背景下,layoutResourceId,List_Data); this.layoutResourceId = layoutResourceId; } @覆盖 公共查看getView(INT位置,查看convertView,父母的ViewGroup){ 查看排= convertView; ItemHolder支架=无效; 如果(行== NULL){ LayoutInflater吹气= parent.getContext()getLayoutInflater()。 行= inflater.inflate(layoutResourceId,父母,假); 持有人=新ItemHolder(); holder.name =(TextView中)row.findViewById(R.id.ItemName); row.setTag(保持器); }其他{ 支架=(ItemHolder)row.getTag(); } 最后LifeTask S =的getItem(位置); holder.name.setText(s.getName()); 返回行; } 静态类ItemHolder { TextView的名称; }} 您的实施问题是有自己的的ArrayList&LT; LifeTask&GT;数据。然而,它是没有必要的,因为父类 ArrayAdapter&LT; LifeTask方式&gt; 已经为项目的容器和使用code这样的更新列表: adapter.clear();adapter.addAll(negativeListData); 明确的()和的addAll()将调用 notifyDataSetChanged()。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 requestedpackage 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; }} 解决方案 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(). 这篇关于更新ListAdapter动态执行过程中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 09-02 18:26