2

EDIT I must tell you that my ListView is populate by an AsyncTask.

The code below works fine when I do in onPostExecute method :

synchronized (mListView) {
            if(mFeeds==null || mFeeds.size()==0){
                Utils.Log("mFeeds empty");
                _this.setListShown(false);
            }else{
                Utils.Log("mFeeds Full");
                _this.setListShown(true);
                mListView.setAdapter(new ListFeedsAdapter(mActivity,mFeeds));
                mListView.notifyAll();

                NewsFeedsDetailViewPagerFragment fragment = (NewsFeedsDetailViewPagerFragment) getFragmentManager()
                        .findFragmentById(R.id.feeddetailViewPagerFragment);

                if(fragment!=null){
                    mListView.performItemClick(null, 0, mListView.getFirstVisiblePosition());

                }
            }

        }

The item is clicked and my detail view is populate...

I try to change my Fragment to a ListFragment but nothing changed...

EDIT END

For my application, I created an UI (for tablet) with ListView at left screen and a detail view at the right. I would like to automatically select the first item after loading datas, and view the detail.

I am able to do this by calling mListView.performItemClick(null, 0, mListView.getFirstVisiblePosition()); and

mListView.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
            long arg3) {
    // TODO Auto-generated method stub

    NewsFeedsDetailViewPagerFragment fragment = (NewsFeedsDetailViewPagerFragment)
            getFragmentManager().findFragmentById(R.id.feeddetailViewPagerFragment);
    if(fragment!=null){
        fragment.setFeedDetail(mFeeds, arg2);
    }
});

Now, what I want to do is to highlight the first item like Gmail application for tablet. I use a selector background on each row like :

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/selected_item_background" android:state_activated="true"/>
    <item android:drawable="@drawable/pressed_item_background" android:state_activated="false" android:state_pressed="true"/>
    <item android:drawable="@drawable/pressed_item_background" android:state_focused="true"/>
    <item android:drawable="@drawable/unselected_item_background"/>
</selector>

When I use mListView.getChildAt(arg2).setActivated(true); in the onItemClick listener, I have a NullPointerException because mListView.getChildAt(arg2) is null, only if I keep the perforItemClick. If I just comment this line, and click on a row, this row is highlight as in Gmail application.

Can you help me and tell me what I'm doing wrong ?

Thank in advance

Alexandre B.
  • 495
  • 1
  • 4
  • 16
  • When I log arg2, all is fine I've got '0'. But if I log arg1 (the view passed to method), I've got null. I imagine that at the moment where the perforItemClick is done, all the views are not instanciate yet and that's why my view is null but what is the best way to do this ? – Alexandre B. Apr 24 '12 at 11:59
  • try this: [https://stackoverflow.com/questions/26248849/how-to-highlight-android-listview-first-item-after-load-items-to-listview#tab-top](https://stackoverflow.com/questions/26248849/how-to-highlight-android-listview-first-item-after-load-items-to-listview#tab-top) – Nadun Priyankarage Jul 13 '17 at 05:08

4 Answers4

3

I found a solution to my problem and I post here the solution if there is a developer that is in the same situation.

I created a method in my adapter that flag wich position must be activated and in my getView method, I just compare the current position to the flag and activate or not the row :

((ListFeedsAdapter) mListView.getAdapter()).setItemSelected(mOldPosition);

And in the adapter :

private int mItemSelected = -1 ;
public void setItemSelected(int position){
mItemSelected=position;
}

public View getView(int position, View convertView, ViewGroup parent){
       //some code ....
    if(mItemSelected==position){
        convertView.setActivated(true);
    }else{
        convertView.setActivated(false);
    }
}

Thank you !

Alexandre B.
  • 495
  • 1
  • 4
  • 16
2

I think it's because you passed null in the performItemClick() method:

mListView.performItemClick(null, 0, mListView.getFirstVisiblePosition());

Try something like:

mListView.performItemClick(mListView.getChildAt(0), 0, mListView.getFirstVisiblePosition());
Hong Duan
  • 4,234
  • 2
  • 27
  • 50
  • 1
    Thanks for your advice, but I tried your solution without success. I found the sample called HoneycombGallery that did exactly what I want but I don't understand how it works. They use `mListView.setItemChecked(position,true)`. When I try it, nothing happened. I guess it's because in the sample, each list item has `android:background="?android:attr/activatedBackgroundIndicator"` – Alexandre B. Apr 24 '12 at 13:28
  • Sorry I didn't see the sample you said, but I think there must be something wrong with the 3rd param of `performItemClick`, which need a row id but you use `mListView.getFirstVisiblePosition()`. – Hong Duan Apr 24 '12 at 14:05
  • I try to replace with `mListView.performItemClick(null, 0, mListView.getItemIdAtPosition(0));` I appreciate your efforts for helping me but this has no effect ... – Alexandre B. Apr 24 '12 at 14:07
0

did you tried setSelection()?

mListView.setSelection(0)
timoschloesser
  • 2,790
  • 4
  • 25
  • 33
0

I know answer is accepted, but if this helps someone.

If you want to select/play the very first view of list view automatically as list loads, then you should visit this link. I was looking for the same thing and found post() method which waits till your list is filled.

Here is my question link :-Accessing list's first item when list view is loaded in android

Here is its solution link :-smoothScrollToPosition after notifyDataSetChanged not working in android

However writing any code in getView() will cost you as getView is called repeatedly. Thanks.

Community
  • 1
  • 1
Roshan Jha
  • 2,091
  • 1
  • 21
  • 30