Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You're using the <code>ListView</code> but not using any of it's callbacks? Here, this is my code that I use for my <code>ListView</code>. I'm putting activities in my array, but you could put anything. Modifying the <code>R.layout.mfd_view</code> allows you to put whatever you want for each list item. A <code>Button</code> if that's what you need. Hope this helps. I'm still learning myself.</p> <pre><code>import android.app.Activity; import android.app.ListFragment; import android.content.Context; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.ListAdapter; import android.widget.ListView; public class MyListFragment extends ListFragment { String fragmentBackStack; MyMapHandler handler; OnViewSelectedListener mListener; /** * An array of POJOs used to hold the info about the fragments we'll be * swapping between This should be inserted into an array adapter of some * sort before being passed onto ListAdapter */ private static final ViewDetails[] ACTIVITY_DETAILS = { new ViewDetails(R.string.action_largeTach, R.string.largeTach_description, LargeTachActivity.class), new ViewDetails(R.string.action_map, R.string.map_description, MyMapHandler.class), new ViewDetails(R.string.action_navigation, R.string.navigation_description, NavigationActivity.class), new ViewDetails(R.string.action_raceMode, R.string.raceMode_description, RaceModeActivity.class), new ViewDetails(R.string.action_settings, R.string.settings_description, SettingsFragment.class), new ViewDetails(R.string.action_extraInfo, R.string.extraInfo_description, ExtraInfoActivity.class) }; /** * @author PyleC1 * * A POJO that holds a class object and it's resource info */ public static class ViewDetails { private final Class&lt;? extends Activity&gt; viewActivity; private int titleId; private int descriptionId; /** * @param titleId * The resource ID of the string for the title * @param descriptionId * The resource ID of the string for the description * @param activityClass * The fragment's class associated with this list position */ ViewDetails(int titleId, int descriptionId, Class&lt;? extends Activity&gt; viewActivity) { super(); this.titleId = titleId; this.descriptionId = descriptionId; this.viewActivity = viewActivity; } public Class&lt;? extends Activity&gt; getViewActivity() { return viewActivity; } } /** * @author PyleC1 * * Extends the ArrayAdapter class to support our custom array that * we'll insert into the ListAdapter so the user can pick between * MFD screens at boot time. */ private static class CustomArrayAdapter extends ArrayAdapter&lt;ViewDetails&gt; { public CustomArrayAdapter(Context context, ViewDetails[] activities) { super(context, R.layout.mfd_view, R.id.mfdTitle, activities); } @Override public View getView(int position, View convertView, ViewGroup parent) { MFDView mfdView; if (convertView instanceof MFDView) { mfdView = (MFDView) convertView; } else { mfdView = new MFDView(getContext()); } ViewDetails details = getItem(position); mfdView.setTitleId(details.titleId); mfdView.setDescriptionId(details.descriptionId); return mfdView; } } public void onAttach(Activity activity) { super.onAttach(activity); ListAdapter listAdapter = new CustomArrayAdapter(getActivity(), ACTIVITY_DETAILS); setListAdapter(listAdapter); try { mListener = (OnViewSelectedListener) activity; } catch (ClassCastException e) { throw new ClassCastException(activity.toString() + " must implement OnViewSelectedListener!"); } } @Override public void onResume() { super.onResume(); } public interface OnViewSelectedListener { public void onViewSelected(Class&lt;? extends Activity&gt; activityClass); } public void onListItemClick(ListView l, View v, int position, long id) { ViewDetails details = (ViewDetails) getListAdapter().getItem(position); mListener.onViewSelected(details.viewActivity); } } </code></pre> <p>Note that whatever activity calls this fragment must implement the <code>OnViewSelectedListener</code> interface. If you added this to your main activity as a subclass, this wouldn't be required. The <code>public void onListItemClick(arg0, arg1, arg2, arg3)</code> callback is fine. You just swap fragments or call activities from inside there.</p>
 

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