Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

notifyItemInserted() in RecyclerView and animation

When I'm adding a new item, I'm calling method

public void addItem(ArrayList<ArrayList<String>> arrayList, int position){
    this.arrayList=arrayList;
    notifyItemInserted(position);
}

and expect to see added item animated, but only previous item in recyclerView can be animated. I tried to change position like (position+1) or (position-1), but it doesn't help. Activity's code:

private void showDialog(final String table_name) {
   ...
   final Cursor c = db.rawQuery("SELECT * FROM '"+table_name+"'",null);
 final String mas[]=c.getColumnNames();
    c.close();
    final List<EditText> list = new ArrayList<>();
    for (int m = 1; m < mas.length; m++) {
        list.add(new EditText(this));
        list.get(m-1).setHint(mas[m]);
        linearLayout.addView(list.get(m-1));
    }
    alert.setView(linearLayout);
    alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {

         ArrayList<String> arrayList=new ArrayList<String>();
            for (int i=0;i<list.size();i++)
                arrayList.add(list.get(i).getText().toString());
            try {
                arrayList=AESEncryption.encrypt2(arrayList);
                ContentValues contentValues=new ContentValues();
                for (int i=1;i<mas.length;i++){
                    contentValues.put(mas[i],arrayList.get(i-1));
                }
                db.insert(table_name, null, contentValues);
                adapter.addItem(startDecrypt(),adapter.getItemCount());
            } catch (BadPaddingException e) ...

    });
    ...
    alert.show();
}
...
 private void fillListView() throws GeneralSecurityException, IOException, ClassNotFoundException {
        dbHelper = new DBHelper(this);
        dbHelper.setFILENAME(getIntent().getExtras().getString("categoryName"));
        db = dbHelper.getWritableDatabase();
        dbHelper.createTable(db);
         Cursor c = db.rawQuery("SELECT * FROM '"+FILENAME+"'",null);
         String mas[]=c.getColumnNames();
        String[] mas2 = Arrays.copyOfRange(mas, 1, mas.length);
        c.close();
       adapter=new RVAdapter(startDecrypt(),mas2,PasswordManagerActivity.this,FILENAME, AESEncryption);
        rv.setAdapter(adapter);
        rv.setHasFixedSize(true);
}

Please, give me a tip, wise men.

like image 730
Rinat Sakaev Avatar asked Mar 15 '23 21:03

Rinat Sakaev


1 Answers

You are not adding an item in your addItem() method. You assign a complete new list to your adapter.

Change your addItem method to something like this:

public void addItem(String item, int position) {
    this.arrayList.add(position, item);
    notifyItemInserted(position);
}

To add an item to your listview call:

adapter.addItem("test", 0);

Maybe you should look at the SortedList class, it could make your life easier.

See the docs here: https://developer.android.com/reference/android/support/v7/util/SortedList.html

like image 148
Andre Classen Avatar answered Apr 02 '23 19:04

Andre Classen