Saving a temporary arraylist for use in an adapter

I have a problem. I need to save ArrayList

before I delete some of the items. So I am using additional "users opied" ArrayList

with duplicate "users" ArrayList. But it doesn't work, I don't know why. After deleting some items in "users", ArrayList

this action also affects "usersCopied" ArrayList

. How to get rid of this problem? I want to keep the original list and not modify it, I will use this list to restore the original data. Thank!

public class SelectUser extends ActionBarActivity implements View.OnClickListener {

public static ArrayList<com.taxizauglom.utils.User> users;
public static ArrayList<com.taxizauglom.utils.User> usersCopied;

@Override
protected void onCreate(Bundle savedInstanceState) {

    users = new ArrayList<>();
    // copy of users list, not changing
    usersCopied = null;

    mCallback = new LoaderManager.LoaderCallbacks<ArrayList<com.taxizauglom.utils.User>>() {
        @Override
        public Loader<ArrayList<com.taxizauglom.utils.User>> onCreateLoader(int id, Bundle args) {
            progressDialog.show();
            return new GetUser(getApplicationContext());
        }
        @Override
        public void onLoadFinished(Loader<ArrayList<com.taxizauglom.utils.User>> loader, ArrayList<com.taxizauglom.utils.User> data) {
            users = data;
            adapter = new MyListAdapter(users);
            listUsers.setAdapter(adapter);
            // Copying data
            if (usersCopied == null){
                Log.d(Constants.tag, "Not copied!");
                copyData(data);
            } else
            {
                Log.d(Constants.tag, "Already copied!");
            }
        }
    };

    getSupportLoaderManager().initLoader(1, null, mCallback).forceLoad(); 
}

public void copyData(ArrayList<com.taxizauglom.utils.User> data) {
    usersCopied = new ArrayList<>(data);
}
public class MyListAdapter extends BaseAdapter {

    ArrayList<User> users;

    public MyListAdapter(ArrayList<User> users) {
        this.users = users;
    }
    @Override
    public void notifyDataSetChanged() {
        super.notifyDataSetChanged();
        // here is a problem
        //After removing item from "users" list, in list "usersCopied" also was removed that element. Why?
        Log.d(Constants.tag, String.valueOf("MyListAdapter users.size: " + users.size()));
        Log.d(Constants.tag, String.valueOf("MyListAdapter usersCopied.size: " + usersCopied.size()));
    }

}

@Override
public void onClick(View v) {

    switch (v.getId()) {

        case R.id.btnSort:
            sortData();
            break;
    }
}

// I wanna remove element only in list users
//restoring initial data from "userCopied" list
//and removing from "users" list
public void sortData() {
    adapter.users = usersCopied;
    adapter.users.remove(0);
    adapter.notifyDataSetChanged();
}

}

      

+3


source to share


6 answers


At the moment, you are still referencing user data. To make sure you don't reference anymore, change the copyData method:

public void copyData(ArrayList<com.taxizauglom.utils.User> data) {
    usersCopied = new ArrayList<com.taxizauglom.utils.User>();
    for (User user : data){
        //Use the data from this loop user to create a new one.
        usersCopied.add(new User(....)); 
    }
}

      



It seems unnecessary, but if you don't create new user objects, you will keep the link in tact.

+1


source


ok i faced the same problem. 1. Try to create another temporary realist. 2. temporary alist = data; 3. And then execute user.addall (alist).



Hope it helps.

0


source


Try to use Collections.copy(destination, source);

0


source


try it

create a new list of arrays and copy the elements from the original array to the new one using the addAll method, this will solve your problem.

public void copyData(ArrayList<com.taxizauglom.utils.User> data) {
    usersCopied = new ArrayList<com.taxizauglom.utils.User>();
    usersCopied.addAll(data);
}

      

0


source


Create a separate class for the elements you want to keep e.g. you want to keep

1) ID 2) Name 3) School

than create a class

public class ShowItems{
    public String ID;
    public String NAME;
    public String SCHOOL;

    public ShowItems(String textID, String textNAME, String textSCHOOL) {
        ID = textID;
        NAME = textNAME;
        SCHOOL = textSCHOOL;
    }   
}

      

Now create ArrayList

with a type ShowItems

like this

  ArrayList<ShowItems> products = new ArrayList<ShowItems>();

      

And add data like

 products.add("1", "MADDY", "CONVENT SCHOOL");

      

If you want to get data at some position, do it like

String strID = products.get(position).ID;
String strNAME = products.get(position).NAME;
String strSCHOOL = products.get(position).SCHOOL;

      

Want to remove items at a specific position ...? Not a problem like this ....

products.remove(position);

      

0


source


Thanks for the advice! I found the problem.

He was there:

public void sortData() {
//Here i refer to temporary list, and change it all the time.
adapter.users = usersCopied;
//Instead of I had to use this:
adapter.users = new ArrayList<>(usersCopied); 
    adapter.users.remove(0);
    adapter.notifyDataSetChanged();
}

      

0


source







All Articles