android如何将listview项目发送到另一个活动 [英] android How to send listview item to another activity

查看:77
本文介绍了android如何将listview项目发送到另一个活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将列表视图中的行项目发送到另一个活动,但是也许我做错了.

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

我制作了一个用于食物的应用程序.我希望当用户单击第一活动"时从该列表视图中将列表项发送到第二活动",并且当用户单击添加至购物车"时,该列表视图项转到Cart.class

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

但是,当我单击添加到购物车"时,活动发送给我的是Cart.class,但没有任何内容.

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

在cart.xml中,我有listvew.

In cart.xml I have listvew.

对不起,我的英语不好

谢谢.

第一次活动.

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

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


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

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




    }


    }
    });

}

第二活动.

public class GrilledChicken extends Activity {


Button butadd;


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


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


    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("list",list);


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

            }


    });

购物车类

public class Cart extends Activity {


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


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


    setContentView(R.layout.cart);


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

     }
 }

推荐答案

将以下代码放入您的 Cart.class

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

现在 list_data 包含数据.

还有另一种方法可以完成任务.

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

创建一个单独的全局类

Global.class

public class Globalclass {
    public static String list_data;

}

然后在您的 FirstActivity 中替换以下内容

And then in your FirstActivity replace the following

intent.putExtra("get",result);

使用

Globalclass.list_data=result;

现在您可以在任何地方访问 list_data ,如下所示:

Now you can access the list_dataanywhere like the following

String data=Globalclass.list_data;

这篇关于android如何将listview项目发送到另一个活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆