-1

This is my first time using eclipse for android, I want to know how can I get a value from listview and send it to another activity; something like session in c#

I want to make it so when I choose one of item in listview, and send it to another activity

listview1.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
/*some code to save data in MainActivity*/
Intent in = new Intent(MainActivity.this,Order.class);
startActivity(in);
}});

and show it in another listview in another activity;

array_list=new String[7];
/*array_list[0]= *something to get data from MainActivity* */
ListView lv = (ListView) findViewById(R.id.listViewMakanan);
ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,array_list);
lv.setAdapter(adapter);

EDIT: this is my array string in MainActivity:

private String array_list[];

array_list=new String[7];
array_list[0]="Nasi Gr Seafood";
array_list[1]="Nasi Gr Magelangan";
array_list[2]="Cap Cay Goreng";
array_list[3]="Cap Cay Kuah";
array_list[4]="Sapi Cabe Hijau";
array_list[5]="Iga Lada Hitam";
array_list[6]="Sapo Tahu Ayam";

and I use this to put my array in ListView:

ListView lv = (ListView) findViewById(R.id.listview1);
        ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1, array_list);
        lv.setAdapter(adapter);
Darjeeling
  • 959
  • 5
  • 19
  • 40

2 Answers2

0

Make it like this.

listview1.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
  public void onItemClick(AdapterView<?> parent, View view,int position, long id) {

   Toast.makeText(getApplicationContext(), array_list[position], Toast.LENGTH_LONG).show();
   /*some code to save data in MainActivity*/
      Intent in = new Intent(MainActivity.this,Order.class);
             in.putExtra("ListValue", array_list[position]);    
             startActivity(in);
      }});

In your Order.class

   //to get the value from the MainActivity.this.
    String value= getIntent().getExtras().getString("ListValue");

Hope this will help you.

Nirmal
  • 2,340
  • 21
  • 43
0

You can use Intent to pass the data between Activities.

Second Solution:

You can use public static Arraylist<String> MyArraylist in your MainActivity & you can access it in another Activity as MainActivity.MyArraylist.

Community
  • 1
  • 1
Bhoomika Brahmbhatt
  • 7,404
  • 3
  • 29
  • 44