6

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.

Khizar Iqbal
  • 325
  • 1
  • 6
  • 16
  • [Capitalization](https://en.wikipedia.org/wiki/Capitalization_in_English) on every word is very hard to read ... – 2Dee Jul 03 '15 at 12:19
  • Sorry about that, I have a bad habbit of doing this. I hope one day i'll get rid of this problem. I am editing my post and try to remove capatilization. thanks for point out. – Khizar Iqbal Jul 03 '15 at 12:23
  • No problem, mate, English is not everyone's native language, in fact it's not mine either ;) Anyway, thanks for the edit ! About your issue, why are you looking for the ListView in the Activity's layout ? Where are you calling public void highlightAyah(int ayahNum) ? How do you make sure you are calling that method with a number that is =< than the list's content ? – 2Dee Jul 03 '15 at 12:31
  • Thanks for your patience. Anyway, let's come to the poiny. As we know, a Fragment is hosted by an activity; So to access any of its view we need reference of activity. As I mentioned above, irrelevent code is emited to show only code which of worth noticing for this problem. but `public void highlightAyah(int ayahNum)` is called by the activity hosted this fragment and it (activity) perform the validation. – Khizar Iqbal Jul 03 '15 at 12:38
  • What is the value of `ayahNum`? I wouldn't be surprised if `ayahNum` was 0, and returns `null` because the input for `.getChildAt()` would really be -1 (since you substract 1 of the input). – Edwin Lambregts Jul 03 '15 at 13:01
  • By doing this.listView = (ListView) getActivity().findViewById(R.id.lv_showQuran);, you're looking for the ListView in the Activity's layout. My point is that if the ListView is part of the Activity, it should be handled there, and if it's in the Fragment's layout, you need to inflate it in onCreateView and get a reference to the listview by doing view.findViewById where view is the inflated layout you return in onCreateView – 2Dee Jul 03 '15 at 13:01
  • By default, ayahNum will have the value of 1. after that its value has been read from a textview and if it is zero, ayahNum again become 1 – Khizar Iqbal Jul 03 '15 at 13:28
  • Code where value of ayahNum has been read and passed to viewSurahFragment. ` EditText editText = (EditText) getActivity().findViewById(R.id.et_ayahNo); int ayahNum = 1; if (editText.getText().toString().length() > 0) ayahNum = Integer.parseInt(editText.getText().toString()); if(ayahNum <= 0) ayahNum = 1; masterFragmentMessenger.viewSurah(surahNamesList.getSelectedItemPosition() + 1); masterFragmentMessenger.highlightAyah(ayahNum); ` – Khizar Iqbal Jul 03 '15 at 13:29
  • @2Dee I did as you said. I initialize the `listview` in the `onCreateView` but after that calling the `getViewCount()` in the `highlightAyah` also return 0. here is code for `onCreateView` `View view = inflater.inflate(R.layout.fragment_view_surah, container, false);` `this.listView = (ListView) view.findViewById(R.id.lv_showQuran);` `return view;` – Khizar Iqbal Jul 03 '15 at 13:56

4 Answers4

13

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.

JuliusScript
  • 3,823
  • 1
  • 15
  • 17
  • Thanks. this one works. here is working code. TextView view = (TextView) this.listView.getAdapter().getView(ayahNum - 1, null, this.listView); view.setBackgroundColor(Color.WHITE); Log.i("ViewSurahFragment", "List View Child Counts Are: " + this.listView.getAdapter().getCount()); if(view == null) Log.i("ViewSurahFragment", "view is null"); But now I fell into another problem. view background color is not changing to white. :-( – Khizar Iqbal Jul 03 '15 at 13:15
  • Well only thing I can suggest right now is defining selection in adapter it self. That way you would set textview to have white background from within getView() method (in there you could check whether item is selected or not). – JuliusScript Jul 03 '15 at 13:46
1
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.

Amrut Bidri
  • 5,898
  • 6
  • 31
  • 76
0

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().

Ydjeen
  • 3
  • 7
  • `displayAyas()` always get called before `highlightAyah` from the activity. If you want I can share complete code here. – Khizar Iqbal Jul 03 '15 at 13:12
  • @RedPrince007 try this approach to get view: http://stackoverflow.com/a/24864536/2448244 – Ydjeen Jul 03 '15 at 14:19
  • I got the solution of the problem. solution is posted by @Julius and I marked it as the answer. anyway thanks for reply Ydjeen – Khizar Iqbal Jul 03 '15 at 15:08
0

use getView method on adapter instead of getChildAt on listview

listview.getAdapter().getView(i, null, null)
Sai Gopi Me
  • 9,800
  • 1
  • 65
  • 43