listview getChildAt()返回null(listview getChildAt() Return null)

编程入门 行业动态 更新时间:2024-10-12 01:29:19
listview getChildAt()返回null(listview getChildAt() Return null)

我一直在研究一个android项目并陷入了一个问题。 我GOOGLE了它,但没有找到答案。 在我的项目中,有一个名为viewsurahfragment的片段,它包含一个listview,其ID是lv_showquran 。

我想突出显示指定索引处的列表视图。 我使用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我是否在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() return 0在我使用的解决方案中getchildviewcount() return 0 ,但项目显示在列表视图中。 任何人都可以告诉我我犯了什么错误。 预先感谢您的帮助。

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.

最满意答案

这产生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 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.

更多推荐

本文发布于:2023-07-28 03:53:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1299885.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:getChildAt   listview   Return   null

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!