简体   繁体   中英

Pass ArrayList element to the next Intent

I'd like to pass an ArrayList element to the next Intent, if some one can help me!

try{
                JSONArray jArray = new JSONArray(result);
                JSONObject json_data=null;
                for(int i=0;i<jArray.length();i++)
                {
                   json_data = jArray.getJSONObject(i);
                   r.add(json_data.getString("Nom_Serveur"));
               }
               setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, r));
               ListView list = getListView();
               list.setTextFilterEnabled(true);
               list.setOnItemClickListener(new OnItemClickListener(){

                public void onItemClick(AdapterView<?> arg0, View arg1,
                        int arg2, long arg3) {
                    Intent i = new Intent(Serveur_EnPanne.this, Info_serveur.class);
                    i.putExtra("key", ??)
                    startActivity(i);   

                }

do this way:

public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
         Intent i = new Intent(Serveur_EnPanne.this, Info_serveur.class);
         i.putExtra("key", arg0.getSeletectedItem().toString());
         startActivity(i);
}

or

public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
         Intent i = new Intent(Serveur_EnPanne.this, Info_serveur.class);
         i.putExtra("key", r.get(arg2));
         startActivity(i);
}

Use this code.Here arraylist is your arraylist.

ArrayList<String[]> arraylist=new ArrayList<String[]>();
  intent = new Intent(this,Your.class);
        intent.putExtra("al2", arraylist);

To retrieve it in other class

Bundle extras = getIntent().getExtras();
arraylist= extras.getStringarray("al2");

arraylist is parse by the following method

putIntegerArrayListExtra(String name, ArrayList<Integer> value)

putParcelableArrayListExtra(String name, ArrayList<? extends Parcelable> value)

putStringArrayListExtra(String name, ArrayList<String> value)

putCharSequenceArrayListExtra(String name, ArrayList<CharSequence> value)

Then you can read from you next activity by replacing put with get with key string as argument,eg

myIntent.getStringArrayListExtra("arrayPeople");

Onother method of passing the arraylist is by bundle:

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

Getting the data to another intent by :

Bundle b = getIntent().getExtra().putParcelableArrayListExtra(String name);

Hope this might help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM