将所选项的数据传递给另一个活动 - android

时间:2013-06-18 11:26:01

标签: android android-arrayadapter custom-lists

我有一个菜单(fishcombovaluemeals),,当用户选择多个项目时,我想要那些       要在另一个活动(购物车)的列表中显示的项目,我已尝试了很多但数据
    永远不会出现在购物车中! onitemclick中出了什么问题! ,      我有

fishcombovaluemeal.java  
RowItem.java  
CustomListViewAdapter.java  

fishcombovaluemealsactivitycode:

package com.example.newlist;

  public class FishComboValueMeals extends Activity  implements
  OnItemClickListener {
          ImageButton  Ib2;
          public static final String[] titles = new String[] {
" Fillet-O-Fish (Medium Value     Meals) ",
"Fillet-O-Fish (Large Value Meals)  ",
"Double Fillet-O-Fish (Medium     Value Meals)",
" Double Fillet-O-Fish (Large Value     Meals)  ",

};




public static final String[] descriptions = new String[] {
        "Light, flaky filet of white fish    topped with tangy tartar   ",
        "Light, flaky filet of white fish     topped with tangy tartar   ",
        "2 patties of tender fish filet over a       layer of cheese,    ",
        " 2 patties of tender fish filet over a       layer of "

};
public static final Integer[] images = { 
                           R.drawable.imfc1,
               R.drawable.imfc2,        
                           R.drawable.imfc3, 
                           R.drawable.imfc4  };



public static final Double[] prices = { 20.0, 22.73, 24.77,  27.04 };

ListView listView;
List<RowItem> rowItems;


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_breakfast);


    rowItems = new ArrayList<RowItem>();
    for (int i = 0; i < titles.length; i++) {
        RowItem item = new RowItem    (images[i], titles[i], descriptions[i],
                prices[i]);
        rowItems.add(item);
    }
    listView = (ListView) findViewById(R.id.list);
    CustomListViewAdapter adapter = new     CustomListViewAdapter(this,
            R.layout.list_item,     rowItems);
    listView.setAdapter(adapter);
    listView.setOnItemClickListener(this);


    Ib2 = (ImageButton) findViewById    (R.id.Button02);


    Ib2.setOnClickListener(new View.OnClickListener() {

    @Override
public void onClick(View arg0) {
// TODO Auto-generatedmethod stub
Intent openStartingPoint = new Intent (getApplicationContext(),ShoppingCart.class);


            startActivity (openStartingPoint);

        }
    });}    



@Override
public void onItemClick(AdapterView<?> parent, View   view, int position,
        long id) {

Intent i = new Intent(getApplicationContext(), ShoppingCart.class);

i.putExtra("EXTRA", "Item " + (position + 1) + ": " + rowItems.get(position));
    startActivity(i);
        }


}

4 个答案:

答案 0 :(得分:1)

获取列表视图的选定项目并通过意图发送值

@Override
public void onItemClick(AdapterView<?> parent, View   view, int position,
        long id) {
String value = lv1.getItemAtPosition(position).toString();
Intent i = new Intent(getApplicationContext(), ShoppingCart.class);
i.putExtra("EXTRA", value);
    startActivity(i);
}

并获取其他活动中的值

Bundle bundle=getIntent().getExtras();
String value=bundle.getString("EXTRA");

答案 1 :(得分:0)

您可以使用一个非常简单的类来将数据存储到其中。 我警告你,这个课程只是一个简单的命题,如何解决你的问题。 它有许多缺点,但它有效; ) 你可以改进它:) 只需在Application Class中初始化它。

以下是示例:

/**
 * Manager to handle passing data between Activities
 * 
 * @author Klawikowski
 * 
 */
public class ManagerBundle implements IConstants
{
    public static final String TAG = "ManagerBundle";

    private static Hashtable< Integer , Object > sDataTable;

    public static void init()
    {
        Log.i( TAG , "[ManagerBundle] Initializing ..." );
        sDataTable = new Hashtable< Integer , Object >();
    }

    public static void addBundle( int pKey , Object pObjectToPass )
    {
        Log.i( TAG , "[ManagerBundle] Adding Object to Manager using key: " + pKey );
        sDataTable.put( pKey , pObjectToPass );
    }

    public static Object getBundle( int pKey )
    {
        Log.i( TAG , "[ManagerBundle] Getting Object from Manager using key: " + pKey );
        Object lData = sDataTable.get( pKey );
        sDataTable.remove( pKey );
        return lData;
    }
}

只需简单地在启动Activity之前放置一个Table / Array或任何其他容器,然后从第二个收集它; )

答案 2 :(得分:0)

你可以试试这个

@Override
public void onItemClick(AdapterView<?> parent, View   view, int position,
        long id) {
    RowItem selItem =  rowItems.get(position);//get the selected RowItem from the list
    String[] selValues= selItem.getMethodName();//your method name in RowItem.java class
                                                //which returns array  
    //if you have getter and setters you can access them and put them in array
    //String[] selArray = new String[size];//size = number of value you want to supply
    //String[0]= selItem.getTitle();//for title
    //String[1] = selItem.getDescription();//for description and so on
    //then send the array as parameter 

    // remaining is same as answered by the Jay Gajjar

    Bundle b=new Bundle();
    b.putStringArray("EXTRA", selArray);
    Intent i = new Intent(getApplicationContext(), ShoppingCart.class);
    i.putExtras(b);
    startActivity(i);
}

并检索ShoppingCart

中的值
Bundle b=this.getIntent().getExtras();
String[] array=b.getStringArray("EXTRA");

答案 3 :(得分:0)

您需要创建菜单项的单例数组。当用户执行多选jst将selectedindex存储到数组,然后将该数组传递给intent。

以下是多选列表视图的教程

http://www.vogella.com/articles/AndroidListView/article.html

以下是在意图中发送数组的代码

Bundle b=new Bundle();
b.putStringArray(key, new String[]{value1, value2});
Intent i=new Intent(context, Class);
i.putExtras(b);

Bundle b=this.getIntent().getExtras();
String[] array=b.getStringArray(key);