android–listview getChildAt()返回null

android–listview getChildAt()返回null,第1张

概述我一直在研究一个Android项目并遇到问题.我用Google搜索但没有找到答案.在我的项目中,有一个名为viewsurahfragment的片段,它包含一个id为lv_showquran的listview.我想在指定的索引处突出显示listview的视图.我使用listview.getchildat()但它返回null值.这是viewsurahfragment的

我一直在研究一个Android项目并遇到问题.我用Google搜索但没有找到答案.
在我的项目中,有一个名为vIEwsurahfragment的片段,它包含一个ID为lv_showquran的ListvIEw.

我想在指定的索引处突出显示ListvIEw的视图.我使用ListvIEw.getchildat()但它返回null值.
这是vIEwsurahfragment的代码.不相关的功能被激活.

public class VIEwSurahFragment extends Fragment{    private ListVIEw ListVIEw;    private int currentSurah;    private String surahname;    private DatabaseHelper databaseHelper;    private ArrayAdapter<String> arrayAdapter;    @OverrIDe    public voID onVIEwCreated(VIEw vIEw, Bundle savedInstanceState)    {        super.onVIEwCreated(vIEw, savedInstanceState);        //        this.ListVIEw = (ListVIEw) getActivity().findVIEwByID(R.ID.lv_showQuran);    }    @OverrIDe    public voID onActivityCreated(Bundle savedInstanceState)    {        super.onActivityCreated(savedInstanceState);        this.ListVIEw = (ListVIEw) getActivity().findVIEwByID(R.ID.lv_showQuran);    }    private voID displayAyas()    {        String[] values = this.getSurahAyas();        this.ListVIEw.setItemsCanFocus(true);        this.arrayAdapter = new ArrayAdapter<>(this.getActivity().getApplicationContext(), R.layout.layout_surah_ayah, values);        this.ListVIEw.setAdapter(this.arrayAdapter);        this.ListVIEw.setonItemClickListener(new AdapterVIEw.OnItemClickListener()        {            @OverrIDe            public voID onItemClick(AdapterVIEw<?> parent, VIEw vIEw, int position, long ID)            {            }        });        registerForContextMenu(this.ListVIEw);    }    public voID highlightAyah(int ayahNum)    {        TextVIEw vIEw = (TextVIEw) this.ListVIEw.getChildAt(ayahNum - 1);        Log.i("VIEwSurahFragment", "List VIEw Child Counts Are: " + this.ListVIEw.getChildCount());        if(vIEw == null)            Log.i("VIEwSurahFragment", "vIEw is null");    }}

不管是否在onactivitycreated或onvIEwcreated中使用了以下代码行,它在highlightayah函数中调用ListvIEw.getchildat()时返回null.

this.ListVIEw = (ListVIEw) getActivity().findVIEwByID(R.ID.lv_showQuran);

我也试着做以下,但它也不适合我.

ListVIEw vIEw = (ListVIEw) getActivity().findVIEwByID(R.ID.lv_showQuran);TextVIEw v = (TextVIEw) vIEw.getChildAt(ayahNum);Log.i("VIEwSurahFragment", "List VIEw Child Counts Are: " + vIEw.getChildCount());if(v == null)    Log.i("VIEwSurahFragment", "vIEw is null");

但对我来说有趣的是getchildvIEwcount()在我使用的任何一个解决方案中返回0,但是项目显示在列表视图中.
任何人都可以告诉我我在哪里做错了.
在此先感谢您的帮助.

解决方法:

这会产生null:

 TextVIEw textVIEw = (TextVIEw) ListVIEw.getChildAt(0); Log.i("item", textVIEw.getText().toString());

而这不是:

    TextVIEw textVIEw = (TextVIEw) ListVIEw.getAdapter().getVIEw(0, null, ListVIEw);    Log.i("item", textVIEw.getText().toString());

此示例使用了androID.R.layout.simple_List_item_1,因此请记住,TextVIEw是simple_List_item_1中的根视图.

总结

以上是内存溢出为你收集整理的android – listview getChildAt()返回null全部内容,希望文章能够帮你解决android – listview getChildAt()返回null所遇到的程序开发问题。

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

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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存