Listview getChildAt () Returns null
I am working on an android project and got into a problem. I searched for it but could not find an answer. My project has a snippet named viewsurahfragment
and contains a list whose id lv_showquran
.
I want to highlight a list view at a specified index. I am using listview.getchildat () but it returns a value null
.
Here is the code for the viewurahfragment. Irrelevant functions are emitted.
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 change if I used the following line of code in either in onactivitycreated
or in onviewcreated
, it returned null when I called the listview.getchildat () function in highlightayah
.
this.listView = (ListView) getActivity().findViewById(R.id.lv_showQuran);
I also tried to do the following, but that 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 what is interesting to me is that getchildviewcount() return 0
in both solutions I have used, but the items appear in the list.
Can anyone tell me where I am going wrong.
Thanks in advance for your help.
source to share
This raises null:
TextView textView = (TextView) listView.getChildAt(0);
Log.i("item", textView.getText().toString());
And it is not:
TextView textView = (TextView) listView.getAdapter().getView(0, null, listView);
Log.i("item", textView.getText().toString());
This example is used android.R.layout.simple_list_item_1
, so keep in mind that the TextView is the root view in simple_list_item_1.
source to share
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 simplest way for a hightlight list item.
Hope this helps you.
source to share
So when you call getChildCount()
- your listView is empty. It looks like you are calling this before you post content inside it using the method displayAyas()
.
Check where is called displayAyas()
. Because of the elements being displayed - I'm guessing it's called somewhere in the action. Place it in front getChildCount()
.
source to share