0

In my android application i have 2 activities(act1,act2).in act1 i have a gridview and some data and when click to one item it will start the act2.in act2 i have a multi column listview and some data and when i press a button the data can add to list.the problem is when i back to act1 and select another item from gridview and the act2 again starts and my listview becomes empty(because the listview adapter is in act2 oncreate method).but i also need the previous data in list.if there is any idea to save or keep the listview data between activities.or any common method or class can be used to insert items to 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 Answers2

0

I would suggest to use a database and link your list view to that. That way you wouldn't lose any data throughout the application.

Vaibhav Ajay Gupta
  • 463
  • 2
  • 4
  • 18
  • ::if there any other method to keep the data in listview by using any seperate class for populating and updating the list. – user2006703 Mar 10 '14 at 09:20
  • Yes you can always make a separate class and use it variables as the global variables to access or modify anything. But the problem is this you have to intialise these variables at the start of your application and later use them. You wouldn't lose any information until your application is shutdown. – Vaibhav Ajay Gupta Mar 10 '14 at 09:24
  • @ Vaibhav Ajay Gupta:please give the structure of that class to insert and retrive the data from listview – user2006703 Mar 10 '14 at 14:00
  • The structure will be like a pojo java class with the getter and setter function for it's variables. What you need to do use it's variable as global variable for your application. You create a list view using arraylist so the one of the variable of your class should be arraylist that can be modified by calling it's respective setter function and it's value can be used anywhere using it's getter function. – Vaibhav Ajay Gupta Mar 10 '14 at 14:18
  • sir i try this technique but there is some erros in my class implimentation so my app will shutdown abnormally.if there is any way to give the overall structure of that class – user2006703 Mar 10 '14 at 14:34
0

Create Bundle object in Appliation class(Every android application have this Class)

  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;
    }
}

use putXXX() and getXXX() method for travliing in your lisview data across the application.

You can use this in any Activity or class :

someVariableORObject = MyXYZApplication.MyAppDataBundle.getXXX()

or

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

Lavekush Agrawal
  • 6,040
  • 7
  • 52
  • 85
  • what is meant by app=this – user2006703 Mar 10 '14 at 15:29
  • @ Lavekush :i have to set the adpter in MyXYZApplication class but it produce a lot of errors and also use the listview as global for all activity – user2006703 Mar 10 '14 at 15:42
  • You need to set your adapter in your activity. and then set your list array data in Bundle from anywhere, after that you will get your list data in your adapter class by just calling getXXX() always remember update bundle object data (List data) whenever you want notify your list with new data. – Lavekush Agrawal Mar 11 '14 at 04:35
  • Every android application have this class which called by android system very first time when your application launching. Here you declare and assign all application level data. – Lavekush Agrawal Mar 11 '14 at 04:38
  • `app == this // Application context object.` – Lavekush Agrawal Mar 11 '14 at 04:39