ArrayAdapter无法更新内容

时间:2015-02-28 23:55:44

标签: android android-arrayadapter

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

这是我扩展的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;
}

}

以下是我处理适配器的Activity

public class ChannelNameActivity extends Activity {

private ListView channelListView ;
private ChannelRow[] channelData;
private ChannelAdapter adapter;

@Override
protected 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();
}

}

1 个答案:

答案 0 :(得分:2)

爵士;

查看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(),则会在createSessionsList(ArrayList<ParseObject> objects)方法中刷新,并为您分配channelData channelData = channels.toArray(new ChannelRow[channels.size()]); 1}}添加到此ListView这样的新对象,此引用不是Adapter&#39; 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 } 不会工作,因为它没有改变。你要做的就是回忆实例化行,这是完整的代码

adapter = new ChannelAdapter(this, R.layout.channel_row, channelData);

编辑1

如果你不想一直打电话给ArrayList,我建议你使用ArrayList.add(Object o)并使用notifyDataSetChanged()功能来更新你的项目然后你就可以了{{1}} ..

希望有所帮助......