本文介绍了ArrayAdapter无法更新内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我想更新我的ArrayAdapter的内容。我已经打过电话 notifyDataSetChanged()适配器上的方法和无效()在 ListView控件,但我没有看到适配器内的数据被更改。我花了两个小时通过每一个计算器岗位有关此主题的搜索,但没有答案的工作。下面是我延长了 ArrayAdapter 类 公共类ChannelAdapter扩展ArrayAdapter&LT; ChannelRow&GT; {上下文语境;INT layoutResourceId;ChannelRow []的数据;公共ChannelAdapter(上下文的背景下,INT layoutResourceId,ChannelRow []数据){ 超级(上下文,layoutResourceId,数据); this.layoutResourceId = layoutResourceId; this.context =背景; this.data =数据;}@覆盖公共查看getView(INT位置,查看convertView,ViewGroup中父){ 查看排= convertView; ChannelRowHolder支架=无效; 如果(行== NULL) { LayoutInflater充气=((活动)上下文).getLayoutInflater(); 行= inflater.inflate(layoutResourceId,父母,假); 持有人=新ChannelRowHolder(); holder.userName =(TextView中)row.findViewById(R.id.userNameTextView); holder.channelName =(TextView中)row.findViewById(R.id.channelNameTextView); row.setTag(保持器); } 其他 { 支架=(ChannelRowHolder)row.getTag(); } ChannelRow channelRow =数据[位置] holder.userName.setText(channelRow.getUserName()); holder.channelName.setText(channelRow.getChannelName()); 返回行;}静态类ChannelRowHolder{ TextView的用户名; TextView的channelName;}} 下面是我的活动这是我处理的适配器。 公共类ChannelNameActivity延伸活动{私人的ListView channelListView;私人ChannelRow [] channelData;私人ChannelAdapter适配器;@覆盖保护无效的onCreate(包savedInstanceState){ super.onCreate(savedInstanceState); ... this.setContentView(R.layout.activity_channelname); //创建频道的ListView grabSessions(); channelData =新ChannelRow [] {//默认数据 新ChannelRow(用户1,通道), 新ChannelRow(用户2,通道2) }; //附加的ListView适配器/人次 适配器=新ChannelAdapter(这一点,R.layout.channel_row,channelData); channelListView =(ListView控件)findViewById(R.id.channelListView); 。最后查看内容查看=(查看)getLayoutInflater()膨胀(R.layout.activity_channelname,NULL); channelListView.addHeaderView(内容查看); channelListView.setAdapter(适配器);....}私人无效grabSessions(){ ParseQuery&LT;的parseObject&GT;查询= ParseQuery.getQuery(会议); query.findInBackground(新FindCallback&LT;的parseObject&GT;(){ 公共无效完成(名单&LT;的parseObject&GT;的对象,ParseException的E){ 如果(E == NULL){ createSessionsList((ArrayList中&LT;的parseObject&GT;)对象); } 其他 { //与查询错误 } } });}/ ** *从grabSessions调用() *初始化channelData与查询的parseObject会议 * @参数对象的查询会话 * /私人无效createSessionsList(ArrayList中&LT;的parseObject&GT;对象){ ArrayList的&LT; ChannelRow&GT;通道=新的ArrayList&LT;&GT;(); ChannelRow NEWROW = NULL; 对于(的parseObject○:对象){ NEWROW =新ChannelRow((字符串)o.get(主机名),(串)o.get(chatTitle)); channels.add(NEWROW); } channelData = channels.toArray(新ChannelRow [channels.size()]); adapter.notifyDataSetChanged(); channelListView.invalidate();}} 解决方案 爵士; 检查了这一点私人ChannelRow [] channelData; 这是你的实例变量,你在你的的onCreate()这样 channelData =新ChannelRow [] {//默认数据 新ChannelRow(用户1,通道), 新ChannelRow(用户2,通道2)}; // channelData持有的参照对象与`new`关键字创建 因此,举例来说,如果你添加一个物体到 channelData 并打电话给你的 notifyDataSetChanged()将刷新但是在 createSessionsList(ArrayList中&LT;的parseObject&GT;对象)方法分配你的 channelData 到像这样的新对象 channelData = channels.toArray(新ChannelRow [channels.size()); 键,此引用不是什么的ListView 的适配器数据所指向的,所以你的 notifyDataSetChanged()不起作用,因为它有没有改变。你所要做的就是回忆实例线,这是完整的code 私人无效createSessionsList(ArrayList中&LT;的parseObject&GT;对象){ArrayList的&LT; ChannelRow&GT;通道=新的ArrayList&LT;&GT;();ChannelRow NEWROW = NULL;对于(的parseObject○:对象){ NEWROW =新ChannelRow((字符串)o.get(主机名),(串)o.get(chatTitle)); channels.add(NEWROW);}channelData = channels.toArray(新ChannelRow [channels.size()]);适配器=新ChannelAdapter(这一点,R.layout.channel_row,channelData);//编辑从这里开始//设置列表视图的适配器channelListView.setAdapter(适配器); //你设置你的清单,以新的适配器adapter.notifyDataSetChanged(); //你可以删除它,如果你喜欢} 修改1 如果你恨调用此行适配器=新ChannelAdapter(这一点,R.layout.channel_row,channelData)的理念; 所有的时间我会建议你使用的ArrayList 并使用 ArrayList.add(对象o)函数来更新你的项目,你都可以 notifyDataSetChanged() .. 希望它帮助。I am trying to update the contents of my ArrayAdapter. I have tried calling the method notifyDataSetChanged() on the adapter and invalidate() on the ListView but I do not see the data within the adapter being changed. I spent two hours searching through every StackOverflow post about this topic, but none of the answers worked.Here is the ArrayAdapter class which I extendedpublic class ChannelAdapter extends ArrayAdapter<ChannelRow> {Context context;int layoutResourceId;ChannelRow[] data;public ChannelAdapter(Context context, int layoutResourceId, ChannelRow[] data) { super(context, layoutResourceId, data); this.layoutResourceId = layoutResourceId; this.context = context; this.data = data;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) { View row = convertView; ChannelRowHolder holder = null; if(row == null) { LayoutInflater inflater = ((Activity)context).getLayoutInflater(); row = inflater.inflate(layoutResourceId, parent, false); holder = new ChannelRowHolder(); holder.userName = (TextView)row.findViewById(R.id.userNameTextView); holder.channelName = (TextView)row.findViewById(R.id.channelNameTextView); row.setTag(holder); } else { holder = (ChannelRowHolder)row.getTag(); } ChannelRow channelRow = data[position]; holder.userName.setText(channelRow.getUserName()); holder.channelName.setText(channelRow.getChannelName()); return row;}static class ChannelRowHolder{ TextView userName; TextView channelName;}}Below is my Activity which I deal with the adapter.public class ChannelNameActivity extends Activity {private ListView channelListView ;private ChannelRow[] channelData;private ChannelAdapter adapter;@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ... this.setContentView(R.layout.activity_channelname); // create ListView of channels grabSessions(); channelData = new ChannelRow[]{ // default data new ChannelRow("user1", "channel1"), new ChannelRow("user2", "channel2") }; // attach ListView adapters/views adapter = new ChannelAdapter(this, R.layout.channel_row, channelData); channelListView = (ListView) findViewById(R.id.channelListView); final View contentView = (View)getLayoutInflater().inflate(R.layout.activity_channelname, null); channelListView.addHeaderView(contentView); channelListView.setAdapter(adapter);....}private void grabSessions() { ParseQuery<ParseObject> query = ParseQuery.getQuery("Session"); query.findInBackground(new FindCallback<ParseObject>() { public void done(List<ParseObject> objects, ParseException e) { if (e == null) { createSessionsList((ArrayList<ParseObject>) objects); } else { // error with query } } });}/** * Called from grabSessions() * Initializes channelData with the queried ParseObject Sessions * @param objects the queried Sessions */private void createSessionsList(ArrayList<ParseObject> objects){ ArrayList<ChannelRow> channels = new ArrayList<>(); ChannelRow newRow = null; for (ParseObject o : objects){ newRow = new ChannelRow((String)o.get("hostName"), (String)o.get("chatTitle")); channels.add(newRow); } channelData = channels.toArray(new ChannelRow[channels.size()]); adapter.notifyDataSetChanged(); channelListView.invalidate();}} 解决方案 Sir;check this out private ChannelRow[] channelData; that's your instance variable, you instantiate it in your onCreate() this way channelData = new ChannelRow[]{ // default data new ChannelRow("user1", "channel1"), new ChannelRow("user2", "channel2")}; // channelData is holding is reference to the object being created with the `new` keywordso for example if you add one more object to channelData and call your notifyDataSetChanged() it will refresh but in your createSessionsList(ArrayList<ParseObject> objects) method you assign your channelData to a new object like this channelData = channels.toArray(new ChannelRow[channels.size()]); and this reference is not what the ListView's Adapter data is pointing to, so your notifyDataSetChanged() does not work because it has not changed. what you have to do is recall the instantiation line, this is the complete codeprivate void createSessionsList(ArrayList<ParseObject> objects){ArrayList<ChannelRow> channels = new ArrayList<>();ChannelRow newRow = null;for (ParseObject o : objects){ newRow = new ChannelRow((String)o.get("hostName"), (String)o.get("chatTitle")); channels.add(newRow);}channelData = channels.toArray(new ChannelRow[channels.size()]);adapter = new ChannelAdapter(this, R.layout.channel_row, channelData);//edit started here // set your Listview to the adapter channelListView.setAdapter(adapter); // you set your list to the new adapteradapter.notifyDataSetChanged();// you can remove it if you like}EDIT 1 if you hate the idea of calling this line adapter = new ChannelAdapter(this, R.layout.channel_row, channelData); all the time i'd suggest you use ArrayList and use the ArrayList.add(Object o) function to update your item then you can all notifyDataSetChanged() ..Hope it helps.. 这篇关于ArrayAdapter无法更新内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-22 12:03