1

Is that even possible? Lets say I created a ListView with an arraylist thats poplated with objects in one activity. Now I clicked on an item off the list view and grabbed the index. Once I clicked on the item it takes me to another activity along with the index number.(Using Intent). Now my question is I want to view the clicked item's information using the index number to access the arraylist that was created in the previous activity. Is this possible?

UPDATED WITH ITEM CLASS. NOT SURE HOW SHOULD I MODIFY TO SINGLETON CLASS

public class Item {

private String name;
private String category;
private int grade;

public void setName(String n){

    name = n;
}

public void setCategory(String c){

    category = c;
}
public void setGrade(int g){

    grade = g;
}
public String getName(){
    return name;
}
public String getCategory(){

    return category;

}
public int getGrade(){
    return grade;
}

@Override
public String toString(){

    return getName();
}


}
Carlitos
  • 419
  • 4
  • 20

5 Answers5

1

I can think of three different methods which can help you to do that:

1st method

You can pass the object you want to use as an extra in an Intent. That will require the object to be serializable as @androidLover said earlier. Please, refer to the link from his / her response.

2nd method

To use some kind of a Singleton class which will contain the list of objects. http://www.tutorialspoint.com/design_pattern/singleton_pattern.htm

Due to the fact that, after the click event on the ListView, you have the index of the desired object, you can easily get that object from the Singleton class.

3rd method

Use something called an EventBus. http://square.github.io/otto/

That will slightly increase the complexity of your app, thus it's a bit of an overkill to use this approach. But still it's a valid one.

Todor Kostov
  • 1,789
  • 1
  • 13
  • 20
  • I like the idea of using the Singleton class. I think that is what I was mostly looking for. I do have a class for an item. I dont know if this is what I should modify – Carlitos Apr 16 '16 at 23:15
  • 1
    Your singleton class should have a list of Items in it. You should not modify the Item class, but rather create a new separated class. It will be something like a container for the list of Items. Please, refer to the following gist to see an example [link](https://gist.github.com/todorBG/85bd61103215b72831cb479d73476d9a) – Todor Kostov Apr 17 '16 at 07:43
0

you can send objects from one activity to another so you can send the hole list or just the item that you want.

Community
  • 1
  • 1
androidLover
  • 302
  • 1
  • 4
0

Using the Intent, you can do pass the list and position to another activity:

Intent intent = new Intent(MainActivity.this, NextActivity.class);
intent.putExtra("KeyList", MyList);
intent.putExtra("PositionKey", thePositionClicked);

In the NextActiviy, you just need to get the List and the Position:

Intent intent = getIntent();
ArrayList<String> reciveList = intent.getStringArrayListExtra("KeyList");
String position = intent.getStringExtra("PositionKey");

Now you just need to match the position in reciveList. You can check intent documentation for more details: http://developer.android.com/reference/android/content/Intent.html

Adley
  • 511
  • 1
  • 6
  • 19
  • Just a short remark - That's useful if you just want to pass simple objects through activities (e.g. String, Integer, etc.). They are serializable. But if you want to pass your own objects (let's say Person, Car, etc.) you have to make them serializable, otherwise it's not gonna work. – Todor Kostov Apr 17 '16 at 07:23
  • Oh, yes. if you pass an object you have to implements Serializable in your object. So you pass the object to another activity and you can get using intent.getSerializableExtra() – Adley Apr 17 '16 at 13:18
0

Fisrt Method you can use
putParcelableArrayListExtra (String name, ArrayList<? extends Parcelable> value) from here. And pass your ArrayList and get your object with the index you pass within OnItemClick in you ListView in you second Activity.

Second Method you can use
in your OnItemClick listView activiy get the index and the object and then convert that object using Gson to GsonObject and pass it using putExtra you find an example here.

Community
  • 1
  • 1
Gujarat Santana
  • 9,854
  • 17
  • 53
  • 75
-1

try use Parcelable to pass selected object through intent

falk
  • 1
  • 1