I have a textview in my first activity and cannot pass its content to the listview in the second. I am trying to take a food name and add it to a menu. Below is my receiving activity. Can anybody tell me how to fix this? Many thanks in advance
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);
}
Arraylist声明存在问题,每次u传递其意图时都会调用该数组,并再次初始化其初始值并清除最后存储的值,这样您就不会获得先前存储的值,因此最好的方法是在其他类中声明该arraylist并将接收到的值添加到该arraylist中,将其传递给适配器。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.