0

我正在创建一个带有列表视图的应用程序。我想从列表视图中的一个项目转到另一个活动。我有以下代码:

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


    }
}

我的问题是:我应该在 startactivity 方法之后放什么?

而且我还想知道如何在其他活动的文本视图中设置项目的名称(标题)?

谢谢你的帮助。

4

2 回答 2

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

    }
}
于 2012-06-01T17:03:44.567 回答
0
private void startActivity(Class<Rating> clazz) {
    Intent intent = new Intent(Topstores.this, clazz);
    startActivity(intent);
}

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

于 2012-06-01T17:09:36.150 回答