listview getChildAt() Return null

好久不见. 提交于 2021-02-07 06:56:19

问题


I have been working on an android project and stuck in a problem. I googled it but found no answer. In my project, there is a fragment named viewsurahfragment and it contains a listview whose id is lv_showquran.

I want to highlight the view of the listview at specified index. I am using listview.getchildat() but it return null value.
Here is the code for viewsurahfragment. Irrelevant functions are emited.

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");
    }
}

Don't metter whether i used following line of code either in onactivitycreated or in onviewcreated, it returned null on calling listview.getchildat() in highlightayah function.

this.listView = (ListView) getActivity().findViewById(R.id.lv_showQuran);

I also tried to do following, but it also didn't work for me.

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");

But the interesting thing for me is that getchildviewcount() return 0 in either solutions i used, but items are displayed in the listview.
Can anybody please tell me where i'm doing mistake.
Thanks in advance for helping.


回答1:


This produces null:

 TextView textView = (TextView) listView.getChildAt(0);
 Log.i("item", textView.getText().toString());

And this does not:

    TextView textView = (TextView) listView.getAdapter().getView(0, null, listView);
    Log.i("item", textView.getText().toString());

This example used android.R.layout.simple_list_item_1 so keep in mind that TextView is root view in simple_list_item_1.




回答2:


private void highlightListView(int position)
{

    try
    {
        int i = 0;
        int count2 = alAudio.size();
        for (i = 0; i < count2; i++)
        {

            View view = lstvwSongs.getChildAt(i - lstvwSongs.getFirstVisiblePosition());
            ViewHolder viewHolder = (ViewHolder) lstvwSongs.getAdapter().getView(i, view, lstvwSongs).getTag();

                if (i == position)
                {
                    viewHolder.txtvwTitle.setTextColor(color_item_highlight);
                }
                else
                {
                    viewHolder.txtvwTitle.setTextColor(color_item_normal);
                }
        }           
    }
    catch (Exception e)
    {           
        e.printStackTrace();
    }
}

This is the easiest way to hightlight listview item.

I hope this might help you.




回答3:


So, when you call getChildCount() - your listView is empty. Looks like you call it before putting content inside it with your displayAyas() method.

Check where is displayAyas() is called. Because of items are displayed - i suppose it is called somewhere in activity. Put it before getChildCount().




回答4:


use getView method on adapter instead of getChildAt on listview

listview.getAdapter().getView(i, null, null)


来源:https://stackoverflow.com/questions/31206674/listview-getchildat-return-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!