Note that there are some explanatory texts on larger screens.

plurals
  1. POSetting a listadapter in a fragment
    text
    copied!<p>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. </p> <p>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).</p> <p>Here is the fragment code:</p> <pre><code>public class Tab1Fragment extends Fragment { private ArrayList&lt;MobileNETDistinctChatInfo&gt; m_parts = new ArrayList&lt;MobileNETDistinctChatInfo&gt;(); 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("someone@gmail.com","Message1", "2013-01-01 11:11:11")); m_parts.add(new MobileNETDistinctChatInfo("someonelse@gmail.com","Message2", "2013-01-01 11:11:11")); m_parts.add(new MobileNETDistinctChatInfo("someonelselse@gmail.com","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 } }; } </code></pre> <p>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.</p> <p>The method:</p> <pre><code>@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; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload