listview getChildAt()返回null

Khi*_*bal 5 android listview android-listview

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

我想在指定的索引处突出显示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");
    }
}
Run Code Online (Sandbox Code Playgroud)

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

this.listView = (ListView) getActivity().findViewById(R.id.lv_showQuran);
Run Code Online (Sandbox Code Playgroud)

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

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");
Run Code Online (Sandbox Code Playgroud)

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

Jul*_*ipt 12

这会产生null:

 TextView textView = (TextView) listView.getChildAt(0);
 Log.i("item", textView.getText().toString());
Run Code Online (Sandbox Code Playgroud)

而这不是:

    TextView textView = (TextView) listView.getAdapter().getView(0, null, listView);
    Log.i("item", textView.getText().toString());
Run Code Online (Sandbox Code Playgroud)

使用此示例时android.R.layout.simple_list_item_1请记住,TextView是simple_list_item_1中的根视图.