我在第一個活動中有一個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);
}
怎麼回事?你有沒有嘗試記錄ListItem,你是否收到價值?你的問題非常模糊 –
你正在用Intent創建一個新的Intent,然後調用listItem = i.getStringExtra(「sendHeader」);爲什麼你會認爲你的Intent '會有額外的字符串嗎?如果這是'Activity'接收數據的代碼,那麼你應該使用'getIntent()',它給你提供'Intent',它被用來由第一個Activity來啓動該Activity。 – Squonk