TheAptKid
TheAptKid

Reputation: 1571

Setting a listadapter in a fragment

I have a fragment Tab1Fragment which extends the Fragment class (so I can't extend the ListActivity, which is required for calling the setListAdapter method). In this fragment, I want to display a listview populated with custom objects.

Is there a way, I could set the adapter without changing the extended class to ListFragment? The problem is with the setListAdapter methods (marked with # HERE).

Here is the fragment code:

public class Tab1Fragment extends Fragment {

    private ArrayList<MobileNETDistinctChatInfo> m_parts = new ArrayList<MobileNETDistinctChatInfo>();
    private Runnable viewParts;
    private MobileNETDistinctChatInfoAdapter m_adapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        MobileNETDBHandler db = new MobileNETDBHandler(getActivity());

        // instantiate Adapter class
        m_adapter = new MobileNETDistinctChatInfoAdapter(getActivity(), R.layout.chatlist_list_item, m_parts);

        setListAdapter(m_adapter); # HERE

        // Thread, which fetches data in the background
        viewParts = new Runnable(){
        public void run(){
            handler.sendEmptyMessage(0);
        }
    };

    Thread thread =  new Thread(null, viewParts, "MagentoBackground");
    thread.start();
    return (LinearLayout) inflater.inflate(R.layout.tab1, container, false);
}


private Handler handler = new Handler()
 {
    public void handleMessage(Message msg)
    {
        m_parts.add(new MobileNETDistinctChatInfo("[email protected]","Message1", "2013-01-01 11:11:11"));
        m_parts.add(new MobileNETDistinctChatInfo("[email protected]","Message2", "2013-01-01 11:11:11"));
        m_parts.add(new MobileNETDistinctChatInfo("[email protected]","Message3", "2013-01-01 11:11:11"));

        m_adapter = new MobileNETDistinctChatInfoAdapter(getActivity(), R.layout.chatlist_list_item, m_parts);

        // display the list.
        setListAdapter(m_adapter); # HERE
    }
};

}

I can't use the ListFragment, because of a method in another class, which returns tabs (they are named Tab1Fragment, Ta2Fragment,Tab3Fragment and extend the Fragment class), which is of the type Fragment, so I shouldn't extend ListFragment.

The method:

@Override  
public Fragment getItem(int position) {  

    Fragment fragment = new Fragment();  
    switch (position) {  
        case 0:  
            return fragment =  new Tab1Fragment();  
        case 1:  
            return fragment =  new Tab2Fragment();  
        case 2:  
            return fragment =  new Tab3Fragment();   
        default:  
            break;  
    }

    return fragment;
}

Upvotes: 0

Views: 4758

Answers (5)

sromku
sromku

Reputation: 4683

  1. Add ListView to layout tab1.xml and give id to this list. for example: my_list
  2. update onCreateView() method in Tab1Fragment to be:

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    
        LinearLayout view = (LinearLayout) inflater.inflate(R.layout.tab1, container, false);
        ListView list = (ListView)view.findViewById(R.id.my_list);
    
        MobileNETDBHandler db = new MobileNETDBHandler(getActivity());
    
        // instantiate Adapter class
        m_adapter = new MobileNETDistinctChatInfoAdapter(getActivity(), R.layout.chatlist_list_item, m_parts);
    
        list.setListAdapter(m_adapter); # HERE
    
        // Thread, which fetches data in the background    
        viewParts = new Runnable(){
            public void run(){
                handler.sendEmptyMessage(0);
            }
        };
    
        Thread thread =  new Thread(null, viewParts, "MagentoBackground");
        thread.start();
        return view;
    }
    

Upvotes: 1

Biraj Zalavadia
Biraj Zalavadia

Reputation: 28484

Use like

public class Tab1Fragment extends ListFragment {

}

Upvotes: 0

Vipul Purohit
Vipul Purohit

Reputation: 9827

You can extend your activity to ListFragment and this will work same as ListActivity.

Or you have to set your list adapter like this :

ListView mListView = (ListView)findViewById(R.Id.your_list_view);

To set list adapter

mListView.setAdapter(YOUR_LIST_ADAPTER);

Upvotes: 0

Newts
Newts

Reputation: 1372

You can create your custom list view class by extending BaseAdapter without changing any extended class in android. Here is sample code for this:

public class Simple_Fragment extends Fragment 
{


   class MyAdapter extends BaseAdapter {
  // here you adpter methods and impelement what do you want

        }

}

Upvotes: 0

Antoine Marques
Antoine Marques

Reputation: 1389

You need to get a reference to the ListView object to be populated.
Having a look at your code, i believe it is defined in the LinearLayout you're inflating, so just do

LinearLayout l = (LinearLayout) inflater.inflate(R.layout.tab1, container, false);
ListView list = (ListView) l.findViewById(R.id.my_listview_id);
list.setAdapter(m_adapter);

You can store the list view in a field if you'd like to make modifications later.

Upvotes: 2

Related Questions