Reputation: 15
I'm creating an app with a listview. I would like to go from an item in the listview to an other activity. I have the following code:
package be.intec.brussel;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class Topstores<TextView> extends Activity{
String[] items = {"C&A", "H&M","Esprit", "Tommy Hilfinger", "Shoe Discount", "Brantano", "Bell&Bo", "Scapino", "Zara", "Kruidvat"};
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.topstores);
ListView ShopView = (ListView) findViewById(R.id.ShopView);
ShopView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.test_list_item, items));
}
protected void onListItemClick(ListView ShopView, View v, int position, long id) {
if("C&A".equals(items[position])){
startActivity(Rating.class);
}
}
private void startActivity(Class<Rating> class1) {
}
}
My question is: What should i put after the startactivity method?
And i also want to know how you can set the name (title) of the item in an textview on the other activity?
Thanks for your help.
Upvotes: 0
Views: 900
Reputation: 40406
i think no need to create method like startActivity
because already we have...
ListView ShopView = (ListView) findViewById(R.id.ShopView);
ShopView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.test_list_item, items));
ShopView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
if("C&A".equals(items[position])){
Intent intent = new Intent(Topstores.this,Rating.class);
intent.putExtra("itemname",items[position]);
startActivity(intent);
}
}});
Now in Rating Activity...
class Rating extends Activity{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ratingxml);
Bundle b =getIntent().getExtras();
String itemname = b.getString("itemname");//here you get name
System.out.println(itemname);
textview.setText(itemname);//here set item in textview
}
}
Upvotes: 1
Reputation: 23186
private void startActivity(Class<Rating> clazz) {
Intent intent = new Intent(Topstores.this, clazz);
startActivity(intent);
}
The code creates an explicit intent to the Rating class and starts the Rating activity.
Upvotes: 0