Android ListView with onClick items Android ListView with onClick items android android

Android ListView with onClick items


In your activity, where you defined your listview

you write

listview.setOnItemClickListener(new OnItemClickListener(){       @Override    public void onItemClick(AdapterView<?>adapter,View v, int position){        ItemClicked item = adapter.getItemAtPosition(position);        Intent intent = new Intent(Activity.this,destinationActivity.class);        //based on item add info to intent        startActivity(intent);    }});

in your adapter's getItem you write

public ItemClicked getItem(int position){    return items.get(position);}


lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {    @Override    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {        Intent i = new Intent(getActivity(), DiscussAddValu.class);        startActivity(i);    }});


You start new activities with intents. One method to send data to an intent is to pass a class that implements parcelable in the intent. Take note you are passing a copy of the class.

http://developer.android.com/reference/android/os/Parcelable.html

Here I have an onItemClick. I create intent and putExtra an entire class into the intent. The class I'm sending has implemented parcelable. Tip: You only need implement the parseable over what is minimally needed to re-create the class. Ie maybe a filename or something simple like a string something that a constructor can use to create the class. The new activity can later getExtras and it is essentially creating a copy of the class with its constructor method.

Here I launch the kmlreader class of my app when I recieve an onclick in the listview.

Note: below summary is a list of the class that I am passing so get(position) returns the class infact it is the same list that populates the listview

List<KmlSummary> summary = null;...public final static String EXTRA_KMLSUMMARY = "com.gosylvester.bestrides.util.KmlSummary";...@Overridepublic void onItemClick(AdapterView<?> parent, View view, int position,        long id) {    lastshownitem = position;    Intent intent = new Intent(context, KmlReader.class);    intent.putExtra(ImageTextListViewActivity.EXTRA_KMLSUMMARY,            summary.get(position));    startActivity(intent);}

later in the new activity I pull out the parseable class with

kmlSummary = intent.getExtras().getParcelable(                ImageTextListViewActivity.EXTRA_KMLSUMMARY);//note://KmlSummary implements parcelable.//there is a constructor method for parcel in// and a overridden writetoparcel method// these are really easy to setup.public KmlSummary(Parcel in) {    this._id = in.readInt();    this._description = in.readString();    this._name = in.readString();    this.set_bounds(in.readDouble(), in.readDouble(), in.readDouble(),    in.readDouble());    this._resrawid = in.readInt();    this._resdrawableid = in.readInt();     this._pathstring = in.readString();    String s = in.readString();    this.set_isThumbCreated(Boolean.parseBoolean(s));}@Overridepublic void writeToParcel(Parcel arg0, int arg1) {    arg0.writeInt(this._id);    arg0.writeString(this._description);    arg0.writeString(this._name);    arg0.writeDouble(this.get_bounds().southwest.latitude);    arg0.writeDouble(this.get_bounds().southwest.longitude);    arg0.writeDouble(this.get_bounds().northeast.latitude);    arg0.writeDouble(this.get_bounds().northeast.longitude);    arg0.writeInt(this._resrawid);    arg0.writeInt(this._resdrawableid);    arg0.writeString(this.get_pathstring());    String s = Boolean.toString(this.isThumbCreated());    arg0.writeString(s);}

Good LuckDanny117