Skip to content Skip to sidebar Skip to footer

Pass The Arraylist With Particular Id From One Class To Another In Android

I want to pass the data from one class to another by passing the arraylist with id .. listviewfirst.setOnItemClickListener(new OnItemClickListener() { @Override p

Solution 1:

As you have ArrayList<HashMap<String, Object>> uploadarraylist

And you want to pass particular HashMap for given selected index to next Activity then,

listviewfirst.setOnItemClickListener(new OnItemClickListener() {
@Override
publicvoidonItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
         HashMap<String, Object> hashMap = uploadarraylist.get(arg2);
         Intent i= new Intent(MainActivity.this,Questionactivity.class);
         // To pass data                 
         i.putExtra("hashMap", hashMap);
         startActivity(i);
        }

And in Questionactivity

Intent intent = getIntent();    
HashMap<String, Object> hashMap = (HashMap<String, Object>) intent.getSerializableExtra("hashMap");

Solution 2:

You can pass the array list values through Bundle.

>           Intent i= new Intent(context, QuestionActivity.class);>           Bundle bundle = new Bundle();> 
>           bundle.putStringArrayList("messages", book_mark);>           bundle.putIntegerArrayList("cell_ids", cell_ids);>           bundle.putIntegerArrayList("cat_ids", cat_ids);>           bundle.putIntegerArrayList("bkmId", bkmId);>           bookmark.putExtras(bundle);>           startActivity(i);

Solution 3:

Make that arrayList as static list and then use it on another class by using declared calss reference.

Solution 4:

If you have an arraylist such as: ArrayList<YourClass> list; Make your class implement the Serializable interface. After that you can add your arraylist in the putExtra() method.

import java.io.Serializable;

publicclassBicycleimplementsSerializable{

  publicint speed;

  publicBicycle(int startSpeed) {
    speed = startSpeed;
  }        
}

And in your activity:

ArrayList<Bicycle> list = newArrayList<Bicycle>();
Intent i = newintent(currentActivity.this, nextActivity.class);
i.putExtra("array", list);
startActivity(i);

Solution 5:

You can also use this as given below, so after that there is no need to pass via Intent:

You may declare your ArrayList as a static one like this:

publicstatic ArrayList<String> array = new ArrayList<String>(); 

By doing this you can access your ArrayList from any Activity by:

activityname.array;

where activityname is the activity or class in which you declare the static ArrayList.

Or if you want to use intent than you can do this:

Intent i = new Intent(this,activityname.class);
Bundle bun = new Bundle();
bun.putIntegerArrayListExtra(String name, ArrayList<Integer> value);
//bun.putParcelableArrayListExtra(String name, ArrayList<? extends Parcelable> value);//bun.putStringArrayListExtra(String name, ArrayList<String> value);
i.putExtra(String name,bun);
startActivity(i);

Post a Comment for "Pass The Arraylist With Particular Id From One Class To Another In Android"