我有一个用我的自定义 ArrayAdapter 填充的自定义 ListView。我从 URL 读取和解析 json 数据,并用它填充 ListView。当我阅读第一个文件时,它工作得很好。不过,现在我希望能够向我的 ListView 添加更多数据(较旧的帖子)。我添加了一个“加载更多”按钮作为 ListView 的页脚,但当我加载新数据并尝试滚动浏览它时,我收到此错误:
java.lang.IndexOutOfBoundsException:索引 25 无效,大小为 25
起初我完全不明白为什么会这样,现在我意识到这是我的 ListView 不可扩展。当我在谷歌搜索可扩展的 ListViews 时,自定义适配器扩展了 BaseExpandableListAdapter,但我不确定如何在我的应用程序中实现它。我的自定义 arrayadapter 定义如下:
private class StreamAdapter extends ArrayAdapter<Message>
消息是我的课程之一。
这就是我显示数据的方式:
private void DisplayData() {
// If the request was successfull then notify the adapter to display the data
if(success) {
if(dataAdapter.isEmpty())
{
dataAdapter.addAll(dataList);
dataAdapter.notifyDataSetChanged();
}
// If the listview isn't empty we want to erase what's there and then add the older posts to display
else
{
/** Add currently displayed messages to a temporary arraylist, clear the listview, then add both arraylists in the correct order **/
int size = messageList.getTop();
ArrayList<Message> temp = new ArrayList<Message>();
for(int i = 0; i < size; i++)
{
temp.add(dataAdapter.getItem(i));
}
dataAdapter.clear();
temp.addAll(dataList);
dataAdapter.addAll(temp);
dataAdapter.notifyDataSetChanged();
}
}
}
整个 temp-ArrayList 是我能想出的在列表视图底部而不是顶部获取新加载的数据(但最旧的帖子)的唯一方法。
有什么方法可以轻松地使我的 ListView 可扩展,还是我必须重写我的整个 StreamAdapter 类?
克里斯蒂娜
编辑:这是我的 StreamAdapter 类的代码,虽然我很确定它不相关并且只是添加到一堆代码中,但我省略了部分内容。
私有类 StreamAdapter 扩展 ArrayAdapter {
public StreamAdapter(Context context, int textViewResourceId, ArrayList<Message> dataList)
{
super(context,textViewResourceId, dataList);
}
private class ViewHolder {
ImageView thumbnail;
TextView display_name;
TextView username;
TextView created_at;
TextView text;
ImageView comments;
TextView nr_comments;
@SuppressWarnings("unused")
ImageView like;
TextView nr_likes;
@SuppressWarnings("unused")
ImageView starred;
TextView nr_starred;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if(convertView == null)
{
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.data_row, null);
holder = new ViewHolder();
holder.thumbnail = (ImageView) convertView.findViewById(R.id.thumbnail);
holder.display_name = (TextView) convertView.findViewById(R.id.display_name);
holder.username = (TextView) convertView.findViewById(R.id.username);
holder.created_at = (TextView) convertView.findViewById(R.id.created_at);
holder.text = (TextView) convertView.findViewById(R.id.text);
holder.comments = (ImageView) convertView.findViewById(R.id.comments);
holder.nr_comments = (TextView) convertView.findViewById(R.id.nr_comments);
holder.like = (ImageView) convertView.findViewById(R.id.like);
holder.nr_likes = (TextView) convertView.findViewById(R.id.nr_likes);
holder.starred = (ImageView) convertView.findViewById(R.id.starred);
holder.nr_starred = (TextView) convertView.findViewById(R.id.nr_starred);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
final Message data = dataList.get(position);
holder.thumbnail.setImageDrawable(getResources().getDrawable(R.drawable.owl_thumbnail));
holder.display_name.setText(data.created_by.getDisplay_name());
holder.username.setText("@"+data.created_by.getUsername());
holder.created_at.setText(data.getCreated_at());
holder.nr_comments.setText(Integer.toString(data.getComments().length));
holder.nr_likes.setText(Integer.toString(data.getLiked_by().length));
holder.nr_starred.setText(Integer.toString(data.getStarred_by().length));
// Code to make certain text in the text-textview clickable
// Clickhandler for the username (display_name)
holder.display_name.setOnClickListener(new OnClickListener() {
// Code
});
// Clickhandler for comments
holder.comments.setOnClickListener(new OnClickListener() {
//Code
});
return convertView;
}
}