Skip to content

Instantly share code, notes, and snippets.

@basilbeltran
Last active August 29, 2015 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save basilbeltran/86c8f7b1c5c3a0479b5b to your computer and use it in GitHub Desktop.
Save basilbeltran/86c8f7b1c5c3a0479b5b to your computer and use it in GitHub Desktop.
A ListFragment and its ArrayAdapter
public class MListFragment extends ListFragment { private static final String TAG = "MListFragment";
private ArrayList<DataObject> mArrayList;
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
mArrayList = DataStoreSingleton.get(getActivity()).getObjects();
MAdapter adapter = new MAdapter(mArrayList);
setListAdapter(adapter); } // ListFragment METHOD
private class MAdapter extends ArrayAdapter<DataObject> {
public MAdapter(ArrayList<DataObject> mArrayList) { super(getActivity(), 0, mArrayList); }
public View getView(int position, View convertView, ViewGroup parent){
if (convertView == null) { convertView = getActivity().getLayoutInflater().inflate(R.layout.list_item_data, null); }
DataObject o = getItem(position);
TextView titleTextView = (TextView)convertView.findViewById(R.id.data_list_item_titleTextView);
titleTextView.setText(o.getTitle());
.... see "listeners" for more inflate / populate / listen. ListFragment's is below...
return convertView;
}
}
public void onListItemClick(ListView l, View v, int position, long id){ // ListFragment METHOD
Object o = ((MAdapter)getListAdapter()).getItem(position);
Intent i = new Intent(getActivity(), MPagerActivity.class); // this template starts a pager holding detail fragments
i.putExtra(MFragment.EXTRA_M_ID, o.getId());
startActivity(i);
}
public void onResume() {
super.onResume();
((MAdapter)getListAdapter()).notifyDataSetChanged();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment