Android ArrayAdapter无法更新内容

Android ArrayAdapter无法更新内容,android,android-arrayadapter,Android,Android Arrayadapter,我正在尝试更新ArrayAdapter的内容。我已尝试在适配器上调用方法notifyDataSetChanged(),并在ListView上调用方法invalidate(),但我没有看到适配器中的数据被更改。我花了两个小时搜索了每一篇关于这个话题的帖子,但没有一个答案有效 下面是我扩展的ArrayAdapter类 public class ChannelAdapter extends ArrayAdapter<ChannelRow> { Context context; int l

我正在尝试更新ArrayAdapter的内容。我已尝试在适配器上调用方法
notifyDataSetChanged()
,并在
ListView
上调用方法
invalidate()
,但我没有看到适配器中的数据被更改。我花了两个小时搜索了每一篇关于这个话题的帖子,但没有一个答案有效

下面是我扩展的
ArrayAdapter

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

@Override
public 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;
}

}
先生

查看此
private ChannelRow[]channelData
这是您的实例变量,您可以用
onCreate()
这样实例化它

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` keyword
因此,例如,如果您向
channelData
添加一个对象,并调用
notifyDataSetChanged()
它将刷新,但在
CreateSessionList(ArrayList对象)
方法中,您将
channelData
分配给一个新对象,如
channelData=channels.toArray(new ChannelRow[channels.size()];
此引用不是
列表视图
适配器
数据所指向的,因此您的
notifyDataSetChanged()
无法工作,因为它没有更改。您需要做的是调用实例化行,这是完整的代码

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 = 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 adapter
adapter.notifyDataSetChanged();// you can remove it if you like
}
private void CreateSessionList(ArrayList对象){
ArrayList通道=新的ArrayList();
ChannelRow newRow=null;
for(解析对象o:objects){
newRow=newchannelrow((字符串)o.get(“主机名”),(字符串)o.get(“聊天标题”);
频道。添加(新行);
}
channelData=channels.toArray(新的ChannelRow[channels.size()]);
adapter=新的ChannelAdapter(此,R.layout.channel_行,channelData);
//编辑从这里开始
//将Listview设置为适配器
channelListView.setAdapter(适配器);//将列表设置为新适配器
adapter.notifyDataSetChanged();//如果愿意,可以将其删除
}
编辑1

如果您不喜欢调用此行
adapter=newchanneladapter(this,R.layout.channel_row,channelData);
我建议您一直使用
ArrayList
并使用
ArrayList.add(Object o)
函数来更新您的项目,然后您可以使用
notifydatasetchange()


希望它能有所帮助。

看不出代码中有任何错误。但有三点:您确定您的数据确实发生了更改吗?其次,listView上的
invalidate
完全没有用处,甚至可能更糟(我不认为这是您的问题,但我会将其删除)。第三,尝试调用
notifyDataSetInvalidated()
而不是
notifydataSetChanged
。我会更深入地看一看。@LaurentMeyer我认为你的第一点就解决了问题。我将调试器设置为在notifydataSetChanged行上停止,并检查了适配器数据。结果发现其中的数据没有变化,这没有意义,因为前一行更改了数据nelData。我创建了一个新的适配器对象,如您所述,但它仍然显示默认数据。您是否将适配器设置为您的listview?
channelListView.setAdapter(适配器);
检查我正在添加的编辑,使其完全完整。@clever\u
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 = 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 adapter
adapter.notifyDataSetChanged();// you can remove it if you like
}