android-将ConvertView作为仍在屏幕上的视图传递

android-将ConvertView作为仍在屏幕上的视图传递,第1张

概述在我的自定义ListAdapter中,第一次调用GetView()时,将convertView作为NULL传入,但是第二次将其作为第一次创建的视图传入.我的ListView有4行,并且所有4行同时显示在屏幕上.从文档看来,convertView应该是已经创建的视图,现在已经滚动到屏幕之外.我期望convertView会全部为4,因此它将

在我的自定义listadapter中,第一次调用GetVIEw()时,将convertVIEw作为NulL传入,但是第二次将其作为第一次创建的视图传入.我的ListVIEw有4行,并且所有4行同时显示在屏幕上.从文档看来,convertVIEw应该是已经创建的视图,现在已经滚动到屏幕之外.我期望convertVIEw会全部为4,因此它将创建/填充4个独立的视图.我应该在第一次调用getVIEw之后拥有一个convertVIEw吗?谢谢.

在OnCreate()中:

    Cursor questions = db.loadQuestions(b.getLong("categoryID"), inputLanguage.getLanguageID(), outputLanguage.getLanguageID());    startManagingCursor(questions);    listadapter adapter = new Questionslistadapter(this, questions);    ListVIEw List = (ListVIEw)findVIEwByID(R.ID.List1);    setlistadapter(adapter);

转接器类别

private class Questionslistadapter extends BaseAdapter implements  listadapter{    private Cursor c;    private Context context;    public Questionslistadapter(Context context, Cursor c) {        this.c = c;        this.context = context;    }    public Object getItem(int position) {        c.movetoposition(position);        return new Question(c);    }    public long getItemID(int position) {        c.movetoposition(position);        return new Question(c).get_ID();    }    @OverrIDe    public int getItemVIEwType(int position) {        Question currentQuestion = (Question)this.getItem(position);        if (currentQuestion.getType().equalsIgnoreCase("text"))            return 0;        else if (currentQuestion.getType().equalsIgnoreCase("range"))            return 0;        else if (currentQuestion.getType().equalsIgnoreCase("yesNo"))            return 2;        else if (currentQuestion.getType().equalsIgnoreCase("picker"))            return 0;        else if (currentQuestion.getType().equalsIgnoreCase("command"))            return 0;        else if (currentQuestion.getType().equalsIgnoreCase("datePicker"))            return 0;        else if (currentQuestion.getType().equalsIgnoreCase("diagram"))            return 0;        else            return -1;    }    @OverrIDe    public int getVIEwTypeCount() {        return 7;    }    public int getCount() {        return c.getCount();    }    public VIEw getVIEw(int position, VIEw convertVIEw, VIEwGroup vIEwGroup) {        Question currentQuestion = (Question)this.getItem(position);        if (convertVIEw == null) {            LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);            convertVIEw = inflater.inflate(R.layout.question_row_text, null);        }        //setup cell        return convertVIEw;    } }

解决方法:

如果我没记错的话,对于要显示的每一行,GetVIEw将被调用两次.我认为第一组调用是出于布局目的而完成的,第二组调用将返回将实际显示的视图.第二组调用应返回与第一组调用相同的视图,这是有道理的.

无论如何,您的代码都不必关心它是被布局还是显示.在几乎所有情况下,如果convertVIEw不为null,则通常应返回convertVIEw;否则,返回true.否则,您应该返回一个新的视图.

总结

以上是内存溢出为你收集整理的android-将ConvertView作为仍在屏幕上的视图传递全部内容,希望文章能够帮你解决android-将ConvertView作为仍在屏幕上的视图传递所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1070820.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存