2014-04-12 86 views
1

我在第一個活動中有一個textview,無法將其內容傳遞給第二個列表視圖。我正嘗試將食物名稱添加到菜單中。以下是我的接收活動。有誰能告訴我如何解決這個問題嗎?提前感謝Android:通過點擊按鈕將TextView從一個活動添加到另一個活動的ListView

FoodItemActivity.java

public class FoodItemActivity extends Activity { 

private TextView foodHeader; 
private TextView foodPrice; 


@Override 
    public void onCreate(Bundle bundle) { 
    super.onCreate(bundle); 
    setContentView(R.layout.food_layout_screen); 

    // connect menuHeader to menuListHeaderTextview for the header, receives the intent from MainActivity below 
    foodHeader = (TextView) findViewById(R.id.foodItemHeader); 
    Intent i = getIntent(); 
    String menuItem = i.getStringExtra("childItem"); 
    foodHeader.setText(menuItem); 

    addToOrderBtn(); 
    viewOrderBtn(); 

    } 

    // add to order button 
    public void addToOrderBtn(){ 

     Button addToOrder_Btn= (Button) findViewById(R.id.btn_addToOrder); 
     addToOrder_Btn.setOnClickListener(new OnClickListener(){ 

      @Override 
      public void onClick(View arg0){ 

       foodHeader = (TextView)findViewById(R.id.foodItemHeader); 
       //foodPrice = (TextView)findViewById(R.id.foodItemPrice); 

       String sendHeader = foodHeader.getText().toString(); 
       //String sendPrice = foodPrice.getText().toString(); 


       Intent myIntent = new Intent(FoodItemActivity.this, OrderActivity.class); 
       myIntent.putExtra("sendHeader", sendHeader); 
       //myIntent.putExtra("sendPrice", sendPrice); 
       startActivity(myIntent); 
      } 
     }); 
    } 

    // view order button 
    public void viewOrderBtn(){ 

     Button viewOrder_Btn= (Button) findViewById(R.id.btn_viewOrder); 
     viewOrder_Btn.setOnClickListener(new OnClickListener(){ 

      @Override 
      public void onClick(View arg0){ 

       Intent myIntent = new Intent(FoodItemActivity.this, OrderActivity.class); 
       startActivity(myIntent); 
      } 

     });    
    }  

OrderACtivity.java

public class OrderActivity extends ListActivity { 

public ArrayList<String> orderList = new ArrayList<String>(); 
private String[] list; 
private ArrayAdapter<String> adapter; 
private ListView lv; 
private String listItem; 

@Override 
    public void onCreate(Bundle bundle) { 
    super.onCreate(bundle); 
    setContentView(R.layout.order_screen); 

    Intent i = new Intent(); 
    listItem = i.getStringExtra("sendHeader"); 
    // adds the received sata to ArrayList orderList 
    orderList.add(listItem); 


    // assign lv to order_screen's ListView component order_list 
    lv = (ListView) findViewById(R.id.order_list); 

    // convert ArrayList orderList to Array 
    list = (String[]) orderList.toArray(); 

    // assign adapter to "this" context, with the layout page order_list_item, and the info obtained in list 
    adapter = new ArrayAdapter<String>(this, R.layout.order_list_item, list); //R.id.order_food_name); 
    // set the data behing lv to adapter 
    adapter.notifyDataSetChanged(); 
    lv.setAdapter(adapter); 





    } 
+0

怎麼回事?你有沒有嘗試記錄ListItem,你是否收到價值?你的問題非常模糊 –

+1

你正在用Intent創建一個新的Intent,然後調用listItem = i.getStringExtra(「sendHeader」);爲什麼你會認爲你的Intent '會有額外的字符串嗎?如果這是'Activity'接收數據的代碼,那麼你應該使用'getIntent()',它給你提供'Intent',它被用來由第一個Activity來啓動該Activity。 – Squonk

回答

0

與你的ArrayList聲明中的問題,其獲取每個當u通過其意圖再初始化和清除最後儲存時間打電話值,所以你不會得到先前存儲的值,所以最好的方法是聲明在其他類中的數組列表,並將收到的值添加到該數組列表並將其傳遞給適配器。

+0

關於如何完成這些任何提示? –

+0

在包內創建類,例如該類內的TempValues declare public static ArrayList orderList = new ArrayList ();並在您的OrderActivity活動中替換orderList.add(listItem);與TempValues.orderList.add(listItem); –

+0

如果您的問題得到解決,然後接受答案。 –

相關問題