Search code examples
androidlistviewgmailtablet

Android/ListView : Select first item after loading datas


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


Solution

  • 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 !