android如何将listview项发送到另一个活动

时间:2013-07-31 07:07:25

标签: android listview listitem

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

我做了一个食物应用程序。 我希望当用户点击“第一活动”时,此列表视图中的列表项将发送到“第二个活动”,当用户点击“添加到购物车”时,列表视图项目转到Cart.class

但是当我点击“添加到购物车”时,活动会将我发送给Cart.class但没有任何内容。

在cart.xml中我有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);

            }


    });

Cart.class

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

     }
 }

4 个答案:

答案 0 :(得分:2)

要从列表视图中获取项目,您必须编写以下代码。

String item = food.get(position).toString();

在你的Itemclick方法上写下这个

答案 1 :(得分:1)

将以下代码放入Cart.class

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

现在list_data包含数据。

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

创建单独的全局类

<强> Global.class

public class Globalclass {
    public static String list_data;

}

然后在您的FirstActivity中替换以下

intent.putExtra("get",result);

Globalclass.list_data=result;

现在您可以像以下一样访问list_data

String data=Globalclass.list_data;

答案 2 :(得分:1)

尝试一次我希望这会对你有所帮助。

首先在YourFirstActivity

中执行此操作
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent intent = new Intent(getActivity(), YourSecondActivity.class);
            YourModel yourModel = (YourModel) parent.getItemAtPosition(position);
            intent.putExtra("yourModel", yourModel);
            startActivity(intent);
        }
    });

在另一个Activity执行此操作。

YourModel yourModel= (YourModel) getIntent().getSerializableExtra("yourModel");

yourModel object,您将获得ListView所选项YourFirstActivityYourSecondActivity的所有数据。

答案 3 :(得分:0)

多个发送ListView项目: -

 ArrayList<String>checked11 = new ArrayList<String>();
 SparseBooleanArray checked = listView1.getCheckedItemPositions();
 final ArrayList<String> selectedItems = new ArrayList<String>();

 for (int i = 0; i < checked.size(); i++) {
     int position = checked.keyAt(i);
     if (checked.get(i))
         selectedItems.add(checked11.get(position));
 }

 String[] outputStrArr = new String[selectedItems.size()];

  for (int i = 0; i < selectedItems.size(); i++) {
       outputStrArr[i] = selectedItems.get(i);
  }

使用Bunddle:

 Bundle bundle = new Bundle();
 Intent intent = new Intent(getApplicationContext(),
                                    OtherActivity.class);
 bundle.putStringArray("selectedItems", outputStrArr);
 intent.putExtra("screen2", "sub");
 intent.putExtras(bundle);
 intent.putExtra(EXTRA_RESPONSE, selected);
 startActivity(intent);