在活动之间保持listview数据

时间:2014-03-10 08:56:40

标签: android android-layout listview android-listview android-activity

在我的Android应用程序中,我有2个活动(act1,act2)。在act1我有一个gridview和一些数据,当点击一个项目时,它将启动act2.in act2我有一个多列listview 和一些数据,当我按下按钮时,数据可以添加到列表。问题是当我回到act1并从gridview中选择另一项并且act2再次启动并且我的listview变为空(因为listview适配器在act2 oncreate方法中)但是我还需要list中的先前数据。如果有任何想法在活动之间保存或保存listview数据。或者可以使用任何常用方法或类将项目插入listview。

ACT1(MenuActivity)

gridview.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v,
                int position, long id) {



            // to show a message box with particular name
            //Toast.makeText(getApplicationContext(),((TextView) v.findViewById(R.id.VegName)).getText(), Toast.LENGTH_SHORT).show();

            TextView Name=(TextView) v.findViewById(R.id.VegName);
            TextView Price=(TextView) v.findViewById(R.id.VegPrice);


             Intent i = new Intent(MenuActivity.this, AddOrderActivity.class);
             i.putExtra("Name", Name.getText().toString());
             i.putExtra("Price", Price.getText().toString());

             startActivity(i);


        }
    });

ACT2(AddOrderActivity)

public class AddOrderActivity extends Activity implements View.OnClickListener 
 {
private ListView list;
private ArrayList<HashMap<String, String>> mylist;
private HashMap<String, String> map;
SimpleAdapter mSchedule;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activityaddorder);   
Button btnAddItem = (Button) findViewById(R.id.btnAddItem);
btnAddItem.setOnClickListener(this);
list = (ListView) findViewById(R.id.listOrders);
mylist = new ArrayList<HashMap<String, String>>();
// used to show the heading of listview
map = new HashMap<String, String>();
map.put("txtItem", "Item");
map.put("txtQuantity", "Quantity");
map.put("txtTotal", "Total");
mylist.add(map);
mSchedule = new SimpleAdapter(AddOrderActivity.this, mylist, R.layout.listview_row,
new String[] { "txtItem", "txtQuantity", "txtTotal" }, new int[] {
R.id.txtItem, R.id.txtQuantity, R.id.txtTotal });
list.setAdapter(mSchedule); 
}

@Override
//i want to add new row on this button click
public void onClick(View v) {
 map.put("txtItem", "1");
    map.put("txtQuantity", "2");
    map.put("txtTotal", "2");
    mylist.add(map);              
    mSchedule.notifyDataSetChanged();
}
}   

2 个答案:

答案 0 :(得分:0)

我建议使用数据库并将列表视图链接到该数据库。这样你就不会在整个应用程序中丢失任何数据。

答案 1 :(得分:0)

在Appliation类中创建Bundle对象(每个Android应用程序都有此类)

  public class MyXYZApplication extends Application {

     public static Bundle MyAppDataBundle = new Bundle();  // Use this abject across the application

     @Override
     public void onCreate() {
       super.onCreate();
       app = this;
    }
}

使用putXXX()和getXXX()方法在整个应用程序中浏览lisview数据。

您可以在任何活动或类中使用它:

someVariableORObject = MyXYZApplication.MyAppDataBundle.getXXX()

MyXYZApplication.MyAppDataBundle.putXXX("key", value)