ListActivity 中的 BaseAdapter 未显示正确的项目
我有一个 ListActivity,其中我试图在列表下显示一个 ImageView。我尝试通过创建自己的 BaseAdapter
并在 getView
方法中使用两种不同的方法来实现此目的,一种用于 List,一种用于 ImageView.我设法让
ImageView
按我想要的方式显示在列表下,但我遇到的问题是列表显示不正确。
由于某种原因,列表中的任何项目在用户向下滚动之前不会出现在屏幕上,都会填充错误的数据,如下所示:
----Top Screen------
| Item 1 |
| Item 2 |
| Item 3 |
| Item 4 |
----Bottom Screen---
| Item 1 | <--Items not on the screen show
| Item 1 | <--as item 1 once the user scrolls
| Image View |
它应该是这样的:
----Top Screen------
| Item 1 |
| Item 2 |
| Item 3 |
| Item 4 |
----Bottom Screen---
| Item 5 |
| Item 6 |
| Image View |
我的自定义 BaseAdapter
private class MyCustomAdapter extends BaseAdapter {
private static final int TYPE_ITEM = 0;
private static final int TYPE_SEPARATOR = 1;
private static final int TYPE_MAX_COUNT = TYPE_SEPARATOR + 1;
private ArrayList<HashMap<String,String>> mData = new ArrayList<HashMap<String,String>>();
private LayoutInflater mInflater;
private TreeSet mSeparatorsSet = new TreeSet();
public MyCustomAdapter() {
mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public void addItem(final String header, final String message) {
HashMap<String,String> item = new HashMap<String,String>();
item.put(LINE_1, header);
item.put(LINE_2, message);
mData.add(item);
notifyDataSetChanged();
}
public void addImageItem() {
HashMap<String,String> item = new HashMap<String,String>();
item.put(LINE_1, "");
item.put(LINE_2, "");
mData.add(item);
mSeparatorsSet.add(mData.size() - 1);
notifyDataSetChanged();
}
@Override
public int getItemViewType(int position) {
return mSeparatorsSet.contains(position) ? TYPE_SEPARATOR : TYPE_ITEM;
}
@Override
public int getViewTypeCount() {
return TYPE_MAX_COUNT;
}
public int getCount() {
return mData.size();
}
public Object getItem(int position) {
return mData.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder vh = null;
int type = getItemViewType(position);
System.out.println("getView " + position + " " + convertView + " type = " + type);
if (convertView == null) {
vh = new ViewHolder();
switch (type) {
case TYPE_ITEM:
convertView = mInflater.inflate(R.layout.instructions_two_row, null);
vh.header = (TextView) convertView.findViewById(R.id.instructions_header);
vh.message = (TextView) convertView.findViewById(R.id.instructions_message);
vh.header.setText((CharSequence) mData.get(position).get(LINE_1));
vh.message.setText((CharSequence) mData.get(position).get(LINE_2));
break;
case TYPE_SEPARATOR:
convertView = mInflater.inflate(R.layout.instructions_image, null);
vh.imageView = (ImageView) convertView.findViewById(R.id.instuct_image);
vh.imageView.setImageDrawable(getResources().getDrawable(R.drawable.instructions));
break;
}
convertView.setTag(vh);
}
else {
vh = (ViewHolder) convertView.getTag();
}
return convertView;
}
}
public static class ViewHolder {
public TextView header;
public TextView message;
public ImageView imageView;
}
感谢您的帮助帮助大家!
I have a ListActivity
in which I am trying to display an ImageView
under the List. I'm attempting to do this by creating my own BaseAdapter
and using two different methods inside the getView
method, one for the List and one for the ImageView
. I managed to get the ImageView
displayed under the List how I want it, but the problem I am having is the List isn't displayed correctly.
For some reason any item in the list that isn't on the screen until the user scrolls down gets populated with the wrong data like so:
----Top Screen------
| Item 1 |
| Item 2 |
| Item 3 |
| Item 4 |
----Bottom Screen---
| Item 1 | <--Items not on the screen show
| Item 1 | <--as item 1 once the user scrolls
| Image View |
What it should be is like so:
----Top Screen------
| Item 1 |
| Item 2 |
| Item 3 |
| Item 4 |
----Bottom Screen---
| Item 5 |
| Item 6 |
| Image View |
My custom BaseAdapter
private class MyCustomAdapter extends BaseAdapter {
private static final int TYPE_ITEM = 0;
private static final int TYPE_SEPARATOR = 1;
private static final int TYPE_MAX_COUNT = TYPE_SEPARATOR + 1;
private ArrayList<HashMap<String,String>> mData = new ArrayList<HashMap<String,String>>();
private LayoutInflater mInflater;
private TreeSet mSeparatorsSet = new TreeSet();
public MyCustomAdapter() {
mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public void addItem(final String header, final String message) {
HashMap<String,String> item = new HashMap<String,String>();
item.put(LINE_1, header);
item.put(LINE_2, message);
mData.add(item);
notifyDataSetChanged();
}
public void addImageItem() {
HashMap<String,String> item = new HashMap<String,String>();
item.put(LINE_1, "");
item.put(LINE_2, "");
mData.add(item);
mSeparatorsSet.add(mData.size() - 1);
notifyDataSetChanged();
}
@Override
public int getItemViewType(int position) {
return mSeparatorsSet.contains(position) ? TYPE_SEPARATOR : TYPE_ITEM;
}
@Override
public int getViewTypeCount() {
return TYPE_MAX_COUNT;
}
public int getCount() {
return mData.size();
}
public Object getItem(int position) {
return mData.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder vh = null;
int type = getItemViewType(position);
System.out.println("getView " + position + " " + convertView + " type = " + type);
if (convertView == null) {
vh = new ViewHolder();
switch (type) {
case TYPE_ITEM:
convertView = mInflater.inflate(R.layout.instructions_two_row, null);
vh.header = (TextView) convertView.findViewById(R.id.instructions_header);
vh.message = (TextView) convertView.findViewById(R.id.instructions_message);
vh.header.setText((CharSequence) mData.get(position).get(LINE_1));
vh.message.setText((CharSequence) mData.get(position).get(LINE_2));
break;
case TYPE_SEPARATOR:
convertView = mInflater.inflate(R.layout.instructions_image, null);
vh.imageView = (ImageView) convertView.findViewById(R.id.instuct_image);
vh.imageView.setImageDrawable(getResources().getDrawable(R.drawable.instructions));
break;
}
convertView.setTag(vh);
}
else {
vh = (ViewHolder) convertView.getTag();
}
return convertView;
}
}
public static class ViewHolder {
public TextView header;
public TextView message;
public ImageView imageView;
}
Thanks for the help all!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
getView
在滚动时回收视图。如果convertView不为null,您仍然需要检查它的类型是否正确并为其设置正确的值。getView
recycles views as you scroll. If convertView is not null, you still need to check that it is the correct type and set the correct values on it.