我正在尝试更新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();
}
}
最佳答案
先生;
检查这个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()]);
,而该引用不是ListView
的Adapter
数据指向,因此您的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
}
编辑1
如果您不喜欢一直使用此行
adapter = new ChannelAdapter(this, R.layout.channel_row, channelData);
的想法,我建议您使用ArrayList
并使用ArrayList.add(Object o)
函数更新您的项目,那么您可以全部notifyDataSetChanged()
..希望能帮助到你..
关于android - ArrayAdapter无法更新内容,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28788421/