RecyclerView does not update after add data

F4bioo picture F4bioo · Feb 9, 2016 · Viewed 12.2k times · Source

I'm migrating from ListView to RecyclerView, but after entering some data in SQLite, my list is not updated using notifyDataSetChanged (); so I always have to call the setAdapter () method;

I have simplified my code to post here. can anyone help me fix the notifyDataSetChanged (); to work in my code please?

RecyclerViewAdapter

public class RecyclerViewAdapter extends RecyclerView.Adapter<ViewHolder> {

private List<Model> model;
private Context context;

public RecyclerViewAdapter(Context context, List<Model> model) {
    this.context = context;
    this.model = new ArrayList<>(model);
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.custom_layout, parent, false);
    return new ViewHolder(context, view, model);
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    final Model model = model.get(position);
    holder.bind(model);
}

@Override
public int getItemCount() {
    return model.size();
} }

ViewHolder

public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

private List<Model> model = new ArrayList<>();
private Context context;

private TextView textViewGroup;
private TextView textViewName;

public ViewHolder(Context context, View itemView, List<Model> model) {
    super(itemView);

    this.model = model;
    this.context = context;

    itemView.setOnClickListener(this);

    textViewGroup = (TextView) itemView.findViewById(R.id.textViewGroup);
    textViewName = (TextView) itemView.findViewById(R.id.textViewName);

}

public void bind(Model model) {
    textViewGroup.setText(model.getGroup());
    textViewName.setText(model.getName());
    }
}

@Override
public void onClick(View view) {
    Model model = this.model.get(getAdapterPosition());
    Intent intent = new Intent(this.context, ShowGroup.class);
    intent.putExtra("DATA", model);
    intent.putExtra("POSITION", getAdapterPosition());
    ((Activity) this.context).startActivityForResult(intent, 7);

    Log.i(">>", "_IDHOLDER: " + model.getId());
    Log.i(">>", "_POSITION: " + getAdapterPosition());
    Log.i(">>", ".");
} }

Model

public class Model implements Serializable {

public static final String TABLE = "GROUP_NAME";
public static final String ID = "_ID";
public static final String GROUP = "GROUP";
public static final String NAME = "NAME";

private long id;
private String group;
private String name;

public Model() {
    setId(0);
}

public long getId() {
    return id;
}

public void setId(long id) {
    this.id = id;
}

public String getGroup() {
    return group;
}

public void setGroup(String group) {
    this.group = group;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
} }

Repository

public class Repository {

private SQLiteDatabase db;

public Repository(SQLiteDatabase db) {
    this.db = db;
}

private ContentValues fillModelValues(Model model) {
    ContentValues values = new ContentValues();
    values.put(Model.GROUP, model.getGroup());
    values.put(Model.NAME, model.getName());

    return values;
}

public void insert(Model model) {
    ContentValues values = fillModelValues(model);
    db.insertOrThrow(Model.TABLE, null, values);
}

public List<Model> consDataBase(Context context) {

    List<Model> listModel = new ArrayList<>();

    Cursor cursor = db.query(Model.TABLE, null, null, null, null, null, null);

    if (cursor.getCount() > 0) {
        cursor.moveToFirst();

        do {
            Model model = new Model();
            model.setId(cursor.getLong(cursor.getColumnIndex(Model.ID)));
            model.setGroup(cursor.getString(cursor.getColumnIndex(Model.GROUP)));
            model.setName(cursor.getString(cursor.getColumnIndex(Model.NAME)));

            listModel.add(model);

        } while (cursor.moveToNext());
    }

    cursor.close();

    return listModel;
} }

MainActivity

public class MainActivity extends AppCompatActivity {

private List<Model> listModel;
private LinearLayoutManager layoutManager;
private RecyclerView recyclerView;
private RecyclerViewAdapter recyclerViewAdapter;

private FloatingActionButton fab;

private DataBase dataBase;
private SQLiteDatabase db;
private Repository repository;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    layoutManager = new LinearLayoutManager(this);
    recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
    recyclerView.setLayoutManager(layoutManager);

    fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            startActivityForResult(new Intent(MainActivity.this, AddGroup.class), 4);
        }
    });

    try {
        dataBase = new DataBase(this);
        db = dataBase.getWritableDatabase();
        repository = new Repository(db);

        listModel = repository.consDataBase(context);
        recyclerViewAdapter = new RecyclerViewAdapter(context, listModel);
        recyclerView.setAdapter(recyclerViewAdapter);
        recyclerView.setHasFixedSize(true);

    } catch (SQLException e) {

    }
}

@Override
protected void onDestroy() {
    super.onDestroy();
    if (db != null) {
        db.close();
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == 4) {

        listModel = repository.consDataBase(context);
        recyclerViewAdapter = new RecyclerViewAdapter(context, listModel);
        recyclerView.setAdapter(recyclerViewAdapter);       
    }
} }

AddGroup

public class AddGroup extends AppCompatActivity {

private EditText editTextGroup;
private EditText editTextName;

private DataBase dataBase;
private SQLiteDatabase db;
private Repository repository;
private Model model;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add_group);

    editTextGroup = (EditText) findViewById(R.id.editTextGroup);
    editTextName = (EditText) findViewById(R.id.editTextName);

    try {
        dataBase = new DataBase(this);
        db = dataBase.getWritableDatabase();
        repository = new Repository(db);
        model = new Model();

    } catch (SQLException e) {

    }
}

private void saveModel() {
    try {
        model.setGroup(editTextGroup.getText().toString().trim());
        model.setName(editTextName.getText().toString().trim());
        if (model.getId() == 0) {
            repository.insert(model);
        }

    } catch (Exception e) {

    }
}

@Override
protected void onDestroy() {
    super.onDestroy();
    if (db != null) {
        db.close();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu_add_group, menu);

    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == R.id.action_save) {
        saveModel();            

    }
    return super.onOptionsItemSelected(item);
} }

Answer

F4bioo picture F4bioo · Feb 11, 2016

it's working now

RecyclerViewAdapter

public class RecyclerViewAdapter extends RecyclerView.Adapter<ViewHolder> {

    private List<Model> model;
    private Context context;

    public RecyclerViewAdapter(Context context, List<Model> model) {
    this.context = context;
        this.model = new ArrayList<>(model);
    }

    ...

    public void notify(List<Model> list) {
        if (model != null) {
        model.clear();
        model.addAll(list);

        } else {
            model = list;
        }
        notifyDataSetChanged();
    }
}

Then in onActivityResult

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == 4) {
    listModel = repository.consDataBase(this);
        recyclerViewAdapter.notify(listModel);
    }
}

thank you :)