本文介绍了即使视图存在,convertView也将作为null传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开发了一个Android应用程序,其中在网格视图中显示图像和文本,并且当用户向下滚动时,接下来显示十个项目(图像和文本)。在adapter.notifyDataSetChanged()调用之后调用适配器的getView方法时,会出现问题。适配器回收数据,但位置重新排列并在网格视图中重复。在添加条件以检查convertView是否为null之前,我没有遇到过这个问题。



活动类:

 公共类DynamicListViewActivity扩展Activity实现
OnScrollListener {

int visibleElements;
int scrollState;
int计数;
TextAdapter adapter = new TextAdapter();
int total = 200; //网格视图中的项目总数


@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate (savedInstanceState);

setContentView(R.layout.grid);
count = 10;
GridView网格=(GridView)findViewById(R.id.gridview);
grid.setAdapter(adapter);
grid.setOnScrollListener(this);

}

public void onScroll(AbsListView view,int firstVisible,int visibleCount,
int totalCount){
visibleElements = visibleCount;
}

public void onScrollStateChanged(AbsListView v,int s){
Log.d( ScrollState,s +);
scrollState = s;
isScrollStateComplete();
}

public void isScrollStateComplete(){
if(visibleElements> 0&& scrollState == SCROLL_STATE_IDLE&& total> count){
int diff =总计数;
count + =(diff> = 10)?10:diff; //将计数更新到下一个十项
adapter.notifyDataSetChanged();
}
}


类TextAdapter扩展了BaseAdapter {
public int getCount(){
返回计数;
}

public Object getItem(int pos){
return pos;
}

public long getItemId(int pos){
return pos;
}

public View getView(int pos,View convertView,ViewGroup p){

View v = convertView;
System.out.println( pos: + pos +布尔值 +(v == null)); //登录以检查位置并进行转换View
if(v == null){
v =(((LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE))。inflate(R.layout.textlayout,null);

ImageView iv =(ImageView)v.findViewById(R.id.album_thumbnail);
iv.setImageDrawable(getResources()。getDrawable(R.drawable.icon));

TextView tvAlbumName =(TextView)v.findViewById(R.id.album_name);
tvAlbumName.setText( postion + pos);

TextView tvAlbumDesc =(TextView)v.findViewById(R.id.album_description);
tvAlbumDesc.setText();

}

return v;
}
}
}

在滚动网格视图正确显示之前。日志:

  08-05 14:24:34.440:INFO / ActivityManager(58):开始活动:目的{act = android .intent.action.MAIN cat = [android.intent.category.LAUNCHER] flg = 0x10200000 cmp = com.mis.list.demo / .DynamicListViewActivity} 
08-05 14:24:34.642:信息/系统。 out(685):pos:0布尔值true
08-05 14:24:34.710:INFO / System.out(685):pos:布尔值false
08-05 14:24:34.710: INFO / System.out(685):pos:1布尔值true
08-05 14:24:34.730:INFO / System.out(685):pos:2 boolean值
08-05 14: 24:34.800:INFO / System.out(685):pos:3布尔值
08-05 14:24:34.860:INFO / System.out(685):pos:4布尔值
08 -05 14:24:34.880:INFO / System.out(685):pos:5布尔值true
08-05 14:24:34.910:INFO / System.out(685):pos:6 boolean值$
08-05 14:24:34.920:INFO / System.out(685):pos:7布尔值true
08-05 14:24:34.960:INFO / System.out(685):pos: 0布尔值true
08-05 14:24:35.030:INFO / ActivityManager(58):显示的活动com.mis .list.demo / .DynamicListViewActivity:520毫秒(总计520毫秒)

滚动的第一端之后

  08-05 14:26:15.740:调试/ ScrollState(685):1 
08-05 14:26 :15.830:DEBUG / dalvikvm(685):GC_EXTERNAL_ALLOC在71ms
中释放了3624个对象/ 257464字节
08-05 14:26:16.210:INFO / System.out(685):pos:8布尔值false
08-05 14:26:16.210:INFO / System.out(685):pos:9布尔值true
08-05 14:26:16.250:DEBUG / ScrollState(685):0
08- 05 14:26:16.260:INFO / System.out(685):pos:0布尔值true
08-05 14:26:16.271:INFO / System.out(685):pos:0布尔值false
08-05 14:26:16.271:INFO / System.out(685):pos:1布尔值false
08-05 14:26:16.271:INFO / System.out(685):pos:2布尔假
08-05 14:26:16.271:INFO / System.out(685):pos:3布尔假
08-05 14:26:16.271:INFO / System.out(685) :pos:4布尔false
08-05 14:26:16.271:INFO / System.out(685):pos:5布尔false
08-05 14:26:16.280:INFO / Sy stem.out(685):pos:6布尔值false
08-05 14:26:16.280:INFO / System.out(685):pos:7布尔值
08-05 14:26: 16.280:INFO / System.out(685):pos:8布尔假
08-05 14:26:16.280:INFO / System.out(685):pos:9布尔假
08-05 14:26:16.280:INFO / System.out(685):pos:10布尔值false
08-05 14:26:16.280:INFO / System.out(685):pos:11布尔值true
08-05 14:26:16.371:DEBUG / dalvikvm(685):GC_EXTERNAL_ALLOC释放644个对象/ 33224字节(41ms)
08-05 14:26:45.270:WARN / KeyCharacterMap(685):无ID键盘0
08-05 14:26:45.270:WARN / KeyCharacterMap(685):使用默认键盘映射:/system/usr/keychars/qwerty.kcm.bin
08-05 14:26:45.341: INFO / System.out(685):pos:12布尔值真
08-05 14:26:45.351:INFO / System.out(685):pos:13布尔值真
08-05 14: 26:45.371:INFO / System.out(685):pos:14布尔值
08-05 14:26:45.380:INFO / System.out(685):pos:15布尔值
08 -05 14:26:45.450:INFO / System.out(685):位置:1 6布尔假
08-05 14:26:45.450:INFO / System.out(685):pos:17布尔假
08-05 14:26:45.460:INFO / System.out(685 ):pos:18 boolean false
08-05 14:26:45.460:INFO / System.out(685):pos:19 boolean false

但是网格显示的是重复的值,而不是顺序。
对不起,我是新用户,所以我不能发布图像。



在日志中,我看到位置0 convertView == null为true。 / p>

如何设置此权限,因为我打算在当前使用的图像视图的android图标位置下载图像。



请帮助。

解决方案

您对convertView的解释不正确。

  public View getView(int position,@Nullable View convertView,ViewGroup parent){

View v = convertView;

//如果convertView为null则创建一个新视图,否则使用转换视图
if(v == null)
v =((LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE)) .inflate(R.layout.textlayout,null);

ImageView iv =(ImageView)v.findViewById(R.id.album_thumbnail);
iv.setImageDrawable(getResources()。getDrawable(R.drawable.icon));

TextView tvAlbumName =(TextView)v.findViewById(R.id.album_name);
tvAlbumName.setText( postion + pos);

TextView tvAlbumDesc =(TextView)v.findViewById(R.id.album_description);
tvAlbumDesc.setText();

返回v;
}

是您应该做的。



由于您的操作,在 convertView 不为空的情况下,您只是返回了convertView而不更新您的数据,因此图像被重复。仅在 convertView 为空的情况下,才设置数据。



适配器和列表视图系统之间的合同:如果传递的 convertView 为空,则创建一个新的,否则重新使用它。


I have developed an android application where image and text are displayed in a grid view and when the user scrolls down next ten items(image and text) are displayed. The problem arises when the getView method of adapter is called after adapter.notifyDataSetChanged() call. The adapter recycles the data but positions are rearranged and repeated in grid view. I hadn't faced this problem till I added the condition to check if convertView is null.

Activity class :

public class DynamicListViewActivity extends Activity implements
    OnScrollListener {

int visibleElements;
int scrollState;
int count;
TextAdapter adapter = new TextAdapter();
int total=200;// total items limit in grid view


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.grid);
    count=10;
    GridView grid = (GridView) findViewById(R.id.gridview);
    grid.setAdapter(adapter);
    grid.setOnScrollListener(this);

}

public void onScroll(AbsListView view, int firstVisible, int visibleCount,
        int totalCount) {
    visibleElements= visibleCount;
}

public void onScrollStateChanged(AbsListView v, int s) {
    Log.d("ScrollState", s+"");
    scrollState=s;
    isScrollStateComplete();
}

public void isScrollStateComplete(){
if(visibleElements>0 && scrollState==SCROLL_STATE_IDLE && total>count){
        int diff=total-count;
        count+=(diff>=10)?10:diff;//update count to next ten items
        adapter.notifyDataSetChanged();
    }
}


class TextAdapter extends BaseAdapter {
    public int getCount() {
        return count;
    }

    public Object getItem(int pos) {
        return pos;
    }

    public long getItemId(int pos) {
        return pos;
    }

    public View getView(int pos, View convertView, ViewGroup p) {

        View v = convertView;
        System.out.println("pos : "+pos+" boolean "+(v==null));// log to check position and convertView
                  if(v==null){
            v =((LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.textlayout,null);

            ImageView iv = (ImageView) v.findViewById(R.id.album_thumbnail);
            iv.setImageDrawable(getResources().getDrawable(R.drawable.icon));

            TextView tvAlbumName = (TextView)v.findViewById(R.id.album_name);
            tvAlbumName.setText("postion "+pos);

            TextView tvAlbumDesc = (TextView)v.findViewById(R.id.album_description);
            tvAlbumDesc.setText("");

           }

          return v;
    }
}
}

Before scrolling grid view displays properly. Log :

08-05 14:24:34.440: INFO/ActivityManager(58): Starting activity: Intent {  act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.mis.list.demo/.DynamicListViewActivity }
08-05 14:24:34.642: INFO/System.out(685): pos : 0 boolean true
08-05 14:24:34.710: INFO/System.out(685): pos : 0 boolean false
08-05 14:24:34.710: INFO/System.out(685): pos : 1 boolean true
08-05 14:24:34.730: INFO/System.out(685): pos : 2 boolean true
08-05 14:24:34.800: INFO/System.out(685): pos : 3 boolean true
08-05 14:24:34.860: INFO/System.out(685): pos : 4 boolean true
08-05 14:24:34.880: INFO/System.out(685): pos : 5 boolean true
08-05 14:24:34.910: INFO/System.out(685): pos : 6 boolean true
08-05 14:24:34.920: INFO/System.out(685): pos : 7 boolean true
08-05 14:24:34.960: INFO/System.out(685): pos : 0 boolean true
08-05 14:24:35.030: INFO/ActivityManager(58): Displayed activity com.mis.list.demo/.DynamicListViewActivity: 520 ms (total 520 ms)

After first end of scroll

08-05 14:26:15.740: DEBUG/ScrollState(685): 1
08-05 14:26:15.830: DEBUG/dalvikvm(685): GC_EXTERNAL_ALLOC freed 3624 objects / 257464 bytes in 71ms
08-05 14:26:16.210: INFO/System.out(685): pos : 8 boolean false
08-05 14:26:16.210: INFO/System.out(685): pos : 9 boolean true
08-05 14:26:16.250: DEBUG/ScrollState(685): 0
08-05 14:26:16.260: INFO/System.out(685): pos : 0 boolean true
08-05 14:26:16.271: INFO/System.out(685): pos : 0 boolean false
08-05 14:26:16.271: INFO/System.out(685): pos : 1 boolean false
08-05 14:26:16.271: INFO/System.out(685): pos : 2 boolean false
08-05 14:26:16.271: INFO/System.out(685): pos : 3 boolean false
08-05 14:26:16.271: INFO/System.out(685): pos : 4 boolean false
08-05 14:26:16.271: INFO/System.out(685): pos : 5 boolean false
08-05 14:26:16.280: INFO/System.out(685): pos : 6 boolean false
08-05 14:26:16.280: INFO/System.out(685): pos : 7 boolean false
08-05 14:26:16.280: INFO/System.out(685): pos : 8 boolean false
08-05 14:26:16.280: INFO/System.out(685): pos : 9 boolean false
08-05 14:26:16.280: INFO/System.out(685): pos : 10 boolean false
08-05 14:26:16.280: INFO/System.out(685): pos : 11 boolean true
08-05 14:26:16.371: DEBUG/dalvikvm(685): GC_EXTERNAL_ALLOC freed 644 objects / 33224 bytes in 41ms
08-05 14:26:45.270: WARN/KeyCharacterMap(685): No keyboard for id 0
08-05 14:26:45.270: WARN/KeyCharacterMap(685): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
08-05 14:26:45.341: INFO/System.out(685): pos : 12 boolean true
08-05 14:26:45.351: INFO/System.out(685): pos : 13 boolean true
08-05 14:26:45.371: INFO/System.out(685): pos : 14 boolean true
08-05 14:26:45.380: INFO/System.out(685): pos : 15 boolean true
08-05 14:26:45.450: INFO/System.out(685): pos : 16 boolean false
08-05 14:26:45.450: INFO/System.out(685): pos : 17 boolean false
08-05 14:26:45.460: INFO/System.out(685): pos : 18 boolean false
08-05 14:26:45.460: INFO/System.out(685): pos : 19 boolean false

But the grid displays with repeated values and not in the order it should.Sorry i am not allowed to post images as i am new user.

In the log i see that for position 0 convertView==null is true.

how to set this right as i plan to download images in the place of android icon for image view currently used.

Please help.

解决方案

Your interpretation of convertView is incorrect.

public View getView(int position, @Nullable View convertView, ViewGroup parent) {

    View v = convertView;

    //If convertView is null create a new view, else use convert view
    if (v == null)
        v = ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.textlayout, null);

    ImageView iv = (ImageView) v.findViewById(R.id.album_thumbnail);
    iv.setImageDrawable(getResources().getDrawable(R.drawable.icon));

    TextView tvAlbumName = (TextView)v.findViewById(R.id.album_name);
    tvAlbumName.setText("postion "+pos);

    TextView tvAlbumDesc = (TextView)v.findViewById(R.id.album_description);
    tvAlbumDesc.setText("");

    return v;
}

is what you should be doing.

As a result of what you were doing, in cases where convertView was not null, you were just returning convertView without updating your data, and hence your images were being repeated. Only in cases where convertView was null were you setting your data.

Contract between the adapter and the list view system: If the passed convertView is null, create a new one, else re-use it.

这篇关于即使视图存在,convertView也将作为null传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 19:15