cross_fire cross_fire - 5 months ago 43
Android Question

android How to send listview item to another activity (SOLVED)

I am trying to send row item from list view to another activity but maybe I do something wrong.

I made one app for food.
And I want when the user click to "First Activity" the list item from this listview to be send to "Second Activity" and when the user click to "Add to cart" the listview item go to Cart.class

But when I click to "Add to cart" the Activity is send me tо Cart.class but there have nothing.

In cart.xml I have listvew.

Sorry for my bad english

Thanks in advance.

I wrote the new changes in the code

First Activity.

public class UnderCal extends Activity {


String classes[] = {"Grilled chicken","Asiago","Spicy"};


int[] meal = new int[]{
R.drawable.grilledchicken,
R.drawable.asiago,
R.drawable.spicy


};

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

this.requestWindowFeature(Window.FEATURE_NO_TITLE);


setContentView(R.layout.under_menu);



final List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>();

for(int i=0;i<3;i++){
HashMap<String, String> hm = new HashMap<String,String>();

hm.put("food", Integer.toString(meal[i]));

hm.put("txt", "" + classes[i]);

aList.add(hm);

}


// Keys used in Hashmap
String[] from = {"food","arrow","txt"};

// Ids of views in listview_layout
int[] to = { R.id.food,R.id.arrow,R.id.txt};

// Instantiating an adapter to store each items
// R.layout.listview_layout defines the layout of each item
SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), aList, R.layout.list_layout, from, to);

// Getting a reference to listview of main.xml layout file
final ListView listView = ( ListView ) findViewById(R.id.mylist);

// Setting the adapter to the listView
listView.setAdapter(adapter);



listView.setDivider(new ColorDrawable(0xffffffff));
listView.setDividerHeight(1);


listView.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View v, int position, long id)
{


if (position == 0)
{
Intent intent = new Intent(UnderCal.this,GrilledChicken.class);


//new
intent.putExtra("get", aList.get(position));

//old
// String result = (String) listView.getItemAtPosition(position).toString();
// intent.putExtra("get",result);
startActivity(intent);

overridePendingTransition(R.anim.animation3, R.anim.animation4);




}


}
});

}


Second Activity.

public class GrilledChicken extends Activity {


Button butadd;

//new
HashMap<String, String> hm;

String list;

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

this.requestWindowFeature(Window.FEATURE_NO_TITLE);


setContentView(R.layout.grilled_chicken);


//new
hm =(HashMap<String, String>)getIntent().getSerializableExtra("get");

//old
//Bundle extras = getIntent().getExtras();
//list = extras.getString("get");

butadd=(Button) findViewById(R.id.butadd);


butadd.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub


Intent intent = new Intent(GrilledChicken.this,Cart.class);
// intent.putExtra("hm",hm);

intent.putExtra("get",hm);


intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(intent);

}


});


Cart.class

public class Cart extends Activity {

//new
HashMap<String, String> hm;

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);


//Remove title bar
this.requestWindowFeature(Window.FEATURE_NO_TITLE);


setContentView(R.layout.cart);

//new
final List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>();

hm =(HashMap<String, String>)getIntent().getSerializableExtra("get");
aList.add(hm);


if(hm!=null){

String[] from = {"food","arrows","txt"};

// Ids of views in listview_layout
int[] to = { R.id.food,R.id.arrow,R.id.txt};


ListView listView = ( ListView ) findViewById(R.id.list);

SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), aList, R.layout.list_layout, from, to);

listView.setAdapter(adapter);

listView.setDivider(new ColorDrawable(0xffffffff));
listView.setDividerHeight(1);

}



//old
//Bundle extras = getIntent().getExtras();
//String pos = extras.getInt("list");

}
}

Answer

Put the following code in your Cart.class

Bundle extras = getIntent().getExtras();
 String   list_data = extras.getString("list"); 

Now list_data contains the data.

There is another way through which you can do the task also.

Create a separate Global Class

Global.class

public class Globalclass {
    public static String list_data;

}

And then in your FirstActivity replace the following

intent.putExtra("get",result);

with

Globalclass.list_data=result;

Now you can access the list_dataanywhere like the following

String data=Globalclass.list_data;