listview getChildAt()返回null [英] listview getChildAt() Return null
问题描述
我一直在从事android项目,并遇到了问题.我用谷歌搜索,但没有找到答案.在我的项目中,有一个名为 viewsurahfragment
的片段,它包含一个ID为 lv_showquran
的listview.
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
.
我想突出显示指定索引处的listview的视图.我正在使用listview.getchildat(),但它返回 null
值.
这是viewsurahfragment的代码.发出不相关的功能.
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");
}
}
无论是在 onactivitycreated
中还是在 onviewcreated
中使用了以下代码行,它在调用 highlightayah中的listview.getchildat()时都返回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");
但是对我来说有趣的是,在我使用的两种解决方案中, getchildviewcount()返回0
,但项目显示在列表视图中.
有人可以告诉我我在哪里做错事.
预先感谢您的帮助.
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.
推荐答案
这将产生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中的根视图.
This example used android.R.layout.simple_list_item_1
so keep in mind that TextView is root view in simple_list_item_1.
这篇关于listview getChildAt()返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!