Pass the arraylist with particular id from one cla

2019-07-25 09:50发布

问题:

I want to pass the data from one class to another by passing the arraylist with id ..

 listviewfirst.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            // TODO Auto-generated method stub

                    Intent i= new Intent(MainActivity.this,Questionactivity.class);
                    // To pass data 
                   i.putExtra("uploadarraylist", uploadarraylist);
                   startActivity(i);


        }

Here I have pass the array list, but based on id no I want to pass the data to next activity page.

回答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
public void onItemClick(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");


回答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);


回答3:

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



回答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;

public class Bicycle implements Serializable{

  public int speed;

  public Bicycle(int startSpeed) {
    speed = startSpeed;
  }        
}

And in your activity:

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


回答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:

public static 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);