Android:通过单击按钮将TextView从一个活动添加到另一个活动的ListView

时间:2014-04-12 23:00:23

标签: java android eclipse listview

我的第一个活动中有一个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);





  }

1 个答案:

答案 0 :(得分:0)

你的Arraylist声明的问题,每当你通过意图再次初始化并清除最后的商店值以及你不能获得以前存储的值时,每次都会调用它,所以最好的方法是在其他类中声明arraylist并添加收到该arraylist的值并将其传递给适配器。