我应该在方法startactivity中加入什么?

时间:2012-06-01 17:01:06

标签: android

我正在创建一个包含listview的应用。我想从列表视图中的项目转到其他活动。我有以下代码:

    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) {


    }
}

我的问题是:在开始活动方法之后我应该放什么?

我还想知道如何在其他活动的textview中设置项目的名称(标题)?

感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

我认为不需要创建类似startActivity的方法,因为我们已经......

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);
                }
          }});

现在处于评分活动......

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

    }
}

答案 1 :(得分:0)

private void startActivity(Class<Rating> clazz) {
    Intent intent = new Intent(Topstores.this, clazz);
    startActivity(intent);
}

代码为Rating类创建一个明确的意图并启动Rating活动。