1

我有一个菜单(fishcombovaluemeals),,当用户选择超过 1 个项目时,我希望这些项目出现在另一个活动(购物车)的列表中,我尝试了很多,但数据
从未出现在购物车中!有什么问题onitemclick!?, 我有

fishcombovaluemeal.java  
RowItem.java  
CustomListViewAdapter.java  

fishcombovaluemeals活动代码:

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

4 回答 4

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");
于 2013-06-18T11:41:13.213 回答
0

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

这是示例:

/**
 * 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 或任何其他容器,然后从第二个容器中收集它;)

于 2013-06-18T11:34:35.523 回答
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");
于 2013-06-18T11:34:38.050 回答
0

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

这是多选列表视图的教程

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);
于 2013-06-18T11:36:18.733 回答