2013-06-18 28 views
1

我有一個菜單(fishcombovaluemeals),,並且當用戶選擇多於1項我想那些 項目在列表中出現在另一個活動(購物車),我嘗試了很多,但數據
從未出現在購物車! 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); 
     } 


} 
+0

如何檢索下一個活動的價值? –

+0

Intent in = this.getIntent(); CharSequence text = in.getCharSequenceExtra(「EXTRA」); 我不知道charsequence是否正確! – user2491711

+0

嘗試像這樣檢索它:'String item; Bundle data = getIntent()。getExtras(); item =(String)data.get(「EXTRA」);'。在你的onCreate()方法中。我認爲這更準確。 –

回答

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

只需簡單地把一個表/陣列或開始活動之前的任何其他容器,並從第二收集一個它; )

+0

THANKs試圖幫助,但說實話,我不明白的代碼,我仍然是一個初學者 – user2491711

+0

好吧,所有你需要做的就是把你選擇的項目排列或像這樣。然後,您只需通過addBundle方法將該集合放入ManagerBundle。正如你所看到的,我們將數據保存在散列表中 - 這是一個集合,其中數據存儲在唯一鍵下。例如,你可以使ManagerBundle.addBundle(「MyKey」,「42」); 。通過這個調用,你已經在鍵值「MyKey」下存儲了字符串值。 回到你的問題。在存儲數據時,在您的案例的新活動「ShoppingCart」中,您只需調用ManagerBundle.getBundle(「MyKey」);你必須投下你得到的對象 – Klawikowski

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

您需要創建菜單項的單陣列來獲取值。當用戶執行多選select 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); 
1

獲得列表視圖的選擇的項目和經由發送值的代碼intent

@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"); 
+0

我試過這段代碼,但在購物車中仍然沒有任何東西,只是一個空格頁 – user2491711

+0

謝謝你試圖幫助 – user2491711

相關問題