Skip to content Skip to sidebar Skip to footer

Open Multiple Listview Item Click To One Class

Hope you guys can help. I have a activity which handles all the 10 image button clicks and list view intents. What i am looking to do is have 1 layout for all the list view button

Solution 1:

If I understand what you want, you could create a class with something like a static Arraylist to be appended each time an item is clicked. So create a class something like

publicclassDataclass
{
    static ArrayList<String> dataArray = new ArrayList<String>();;

    publicData()
    {
        // empty constructor but could be used if needed
    }

Then you can add different getters/setters or whatever you need here. When you click on an item you just call something like

Data.dataArray.add("stuff");

then retrieve items in here in your next Activity.

If that is too complicated or more than you need then you can just pass an ArrayList or whatever object you need through the Intent

Intents

Also, just a preference but since all of your Buttons do the same thing, you can do away with initializing them and setting listeners on all of them. In xml just add

`android:onClick="someFunctionName"`

to each Button then use that function name

publicvoidsomeFunctionName(View v){
switch(v.getId()) {
// if one of the image buttons is pressed...

    Intent intent = newIntent(this, Listviewact.class);
    // pass ID of pressed button to listview-activity
    intent.putExtra("buttonId", v.getId());  
    startActivity(intent);
    break;
// here you could place handling of other clicks if necessary...        
}

There is also no need for the case statements since they all do the same thing and you don't need implements OnClickListener in the Activity declaration

Solution 2:

You're using the ListView but not using any of it's callbacks? Here, this is my code that I use for my ListView. I'm putting activities in my array, but you could put anything. Modifying the R.layout.mfd_view allows you to put whatever you want for each list item. A Button if that's what you need. Hope this helps. I'm still learning myself.

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;

publicclassMyListFragmentextendsListFragment {
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
 */privatestaticfinal ViewDetails[] ACTIVITY_DETAILS = {
        newViewDetails(R.string.action_largeTach,
                R.string.largeTach_description, LargeTachActivity.class),
        newViewDetails(R.string.action_map, R.string.map_description,
                MyMapHandler.class),
        newViewDetails(R.string.action_navigation,
                R.string.navigation_description, NavigationActivity.class),
        newViewDetails(R.string.action_raceMode,
                R.string.raceMode_description, RaceModeActivity.class),
        newViewDetails(R.string.action_settings,
                R.string.settings_description, SettingsFragment.class),
        newViewDetails(R.string.action_extraInfo,
                R.string.extraInfo_description, ExtraInfoActivity.class) };

/**
 * @author PyleC1
 * 
 *         A POJO that holds a class object and it's resource info
 */publicstaticclassViewDetails {
    privatefinal Class<? extendsActivity> viewActivity;
    privateint titleId;
    privateint 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<? extendsActivity> viewActivity) {

        super();
        this.titleId = titleId;
        this.descriptionId = descriptionId;
        this.viewActivity = viewActivity;
    }

    public Class<? extendsActivity> 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.
 */privatestaticclassCustomArrayAdapterextendsArrayAdapter<ViewDetails> {
    publicCustomArrayAdapter(Context context, ViewDetails[] activities) {
        super(context, R.layout.mfd_view, R.id.mfdTitle, activities);
    }

    @Overridepublic View getView(int position, View convertView, ViewGroup parent) {
        MFDView mfdView;

        if (convertView instanceof MFDView) {
            mfdView = (MFDView) convertView;
        } else {
            mfdView = newMFDView(getContext());
        }

        ViewDetailsdetails= getItem(position);

        mfdView.setTitleId(details.titleId);
        mfdView.setDescriptionId(details.descriptionId);

        return mfdView;
    }
}

publicvoidonAttach(Activity activity) {
    super.onAttach(activity);

    ListAdapterlistAdapter=newCustomArrayAdapter(getActivity(),
            ACTIVITY_DETAILS);
    setListAdapter(listAdapter);

    try {
        mListener = (OnViewSelectedListener) activity;
    } catch (ClassCastException e) {
        thrownewClassCastException(activity.toString()
                + " must implement OnViewSelectedListener!");
    }
}

@OverridepublicvoidonResume() {
    super.onResume();
}

publicinterfaceOnViewSelectedListener {
    publicvoidonViewSelected(Class<? extends Activity> activityClass);
}

publicvoidonListItemClick(ListView l, View v, int position, long id) {
    ViewDetailsdetails= (ViewDetails) getListAdapter().getItem(position);

    mListener.onViewSelected(details.viewActivity);
}
}

Note that whatever activity calls this fragment must implement the OnViewSelectedListener interface. If you added this to your main activity as a subclass, this wouldn't be required. The public void onListItemClick(arg0, arg1, arg2, arg3) callback is fine. You just swap fragments or call activities from inside there.

Post a Comment for "Open Multiple Listview Item Click To One Class"