I have an RecycleView. i take an ArrayList for Fetch all my Category that i inserted in mySqliteDatabase. and finally i set this ArrayList into RecycleView using its Adapter.
Problem:
(1) First Open CategoryActivity. (no categories) (2) Add First Category, Second, Third no one is Refreshing. (3) but after Adding First if i go back. and come again in that Activity. and if now i am insert Second categories then all [next items] are getting refresh.
My Problem is whenever i insert 1st Category. it insert successfully but it is not shown in my RecycleView or an ArrayList [means Recycleview not refreshing].
but my Main Problem is my RecycleView is not Refreshing only FirstCategory on My ArrayList.. After adding First item my next all Arraylist item is getting Refreshed.
Below is my All Code.
CategoryAdapter
public class CategoryAdapter extends RecyclerView.Adapter<CategoryAdapter.MyViewHolder> implements Filterable {
private ArrayList<CategoryModel> dataList;
class MyViewHolder extends RecyclerView.ViewHolder {
TextView tvCategoryItem;
MyViewHolder(View view) {
super(view);
tvCategoryItem = (TextView) view.findViewById(R.id.tvCategoryName);
}
}
CategoryAdapter(ArrayList<CategoryModel> dataSet) {
this.dataList = dataSet;
this.filterList = dataSet;
}
// I used this method too. but same problem.
void refreshAdapter(ArrayList<CategoryModel> dataSet) {
dataList.clear();
dataList.addAll(dataSet);
notifyDataSetChanged();
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.rv_row_category_list, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
CategoryModel categoryModel = dataList.get(position);
holder.tvCategoryItem.setText(categoryModel.getCategoryName());
}
@Override
public int getItemCount() {
return dataList.size();
}
}
CategoryListActivity
MyDatabase myDb;
CategoryModel categoryModel;
RecyclerView recyclerCategory;
RecyclerView.LayoutManager layoutManager;
CategoryAdapter adapter;
ArrayList<CategoryModel> allCategory;
int CategoryType;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_category);
myDb = new MyDatabase(this);
recyclerCategory = (RecyclerView) findViewById(R.id.recyclerCategory);
CategoryType = CaseActivity.CategoryType;
Log.d(GlobalConstant.KEY_CATEGORY_TYPE, "" + CategoryType);
allCategory = myDb.getCategoryByType(CategoryType);
if (allCategory.size() == 0) {
Toast.makeText(this, "No Category", Toast.LENGTH_SHORT).show();
}
adapter = new CategoryAdapter(allCategory);
recyclerCategory.setAdapter(adapter);
layoutManager = new LinearLayoutManager(this);
recyclerCategory.setLayoutManager(layoutManager);
recyclerCategory.setItemAnimator(new DefaultItemAnimator());
recyclerCategory.setHasFixedSize(true);
recyclerCategory.addOnItemTouchListener(new RecyclerItemClickListener(this, recyclerCategory,
new RecyclerItemClickListener.OnItemTouchListener() {
@Override
public void onItemClick(View view, int position) {
TextView tvCategoryItem = (TextView) view.findViewById(R.id.tvCategoryName);
String CategoryName = tvCategoryItem.getText().toString();
Intent intent = new Intent();
CategoryId = allCategory.get(+position).getCategoryId();
intent.putExtra("CategoryId", String.valueOf(CategoryId));
Log.e("Clicked Cat Id is ", "" + CategoryId);
intent.putExtra("CategoryName", CategoryName);
setResult(Activity.RESULT_OK, intent);
finish();
}
@Override
public void onLongItemClick(View view, int position) {
}
}
));
}
private void openAlert() {
final EditText etCategoryName = new EditText(this);
CustomAlertDialog dialog1 = new CustomAlertDialog(this);
dialog1.setTitle("Add Category");
dialog1.setView(etCategoryName);
dialog1.setCancelable(true);
dialog1.setPositiveButton("Add", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
final String CategoryName = etCategoryName.getText().toString().trim();
final String storedCategoryName = myDb.ExistCategory(CategoryName);
if (CategoryName.equals("") && CategoryName.length() == 0) {
Builder builder1 = GlobalConstant.createAlert(CategoryListActivity.this, "Warning", "Enter Category Name");
builder1.setPositiveButton("Back", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
});
} else {
if (CategoryName.equals(storedCategoryName)) {
Builder builder1 = GlobalConstant.createAlert(CategoryListActivity.this, "Warning", "Category Already Exists");
builder1.setPositiveButton("Back", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
});
} else {
dialogInterface.dismiss();
categoryModel = new CategoryModel(CategoryName, String.valueOf(CategoryType));
myDb.createCategory(categoryModel);
Toast.makeText(CategoryListActivity.this, "CategoryName " + CategoryName
+ "\nCategoryType " + CategoryType, Toast.LENGTH_SHORT).show();
allCategory.clear();
allCategory = myDb.getCategoryByType(CategoryType);
adapter.notifyDataSetChanged();
}
}
}
});
dialog1.show();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_add:
openAlert();
default:
return super.onOptionsItemSelected(item);
}
}
}
my createCategory() method.
public long createCategory(CategoryModel categoryModel) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(CATEGORY_NAME, categoryModel.getCategoryName());
values.put(CATEGORY_TYPE, categoryModel.getCategoryType());
long CategoryId = db.insert(TABLE_CATEGORY, null, values);
db.close();
return CategoryId;
}
and Finally my getCategoryByType() method on My Database .
public ArrayList<CategoryModel> getCategoryByType(int CategoryType) {
ArrayList<CategoryModel> Categories = new ArrayList<>();
// String selectQuery = "SELECT " + CATEGORY_NAME + " FROM " + TABLE_CATEGORY + " WHERE " + CATEGORY_TYPE + " = " + CategoryType;
String selectQuery = "SELECT * FROM " + TABLE_CATEGORY + " WHERE " + CATEGORY_TYPE + " = " + CategoryType + " ORDER BY CategoryId DESC";
Log.e(DB_LOG, selectQuery);
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor != null && cursor.getCount() > 0) {
if (cursor.moveToFirst()) {
do {
CategoryModel categoryModel = new CategoryModel();
categoryModel.setCategoryId(cursor.getInt(cursor.getColumnIndex(CATEGORY_ID)));
categoryModel.setCategoryName(cursor.getString(cursor.getColumnIndex(CATEGORY_NAME)));
categoryModel.setCategoryType(cursor.getString(cursor.getColumnIndex(CATEGORY_TYPE)));
Categories.add(categoryModel);
} while (cursor.moveToNext());
}
cursor.close();
}
return Categories;
}
Any Type of Help, Suggestion is much Appreciated.....Thanks in advance
Below is Screenshot after adding First Item.
First Item Inserted but not shown in RecycleView
My LogCat Screenshot is below.
Change your Activity onCreate:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_category);
myDb = new MyDatabase(this);
recyclerCategory = (RecyclerView) findViewById(R.id.recyclerCategory);
CategoryType = CaseActivity.CategoryType;
Log.d(GlobalConstant.KEY_CATEGORY_TYPE, "" + CategoryType);
allCategory = new ArrayList<>();
adapter = new CategoryAdapter(allCategory);
layoutManager = new LinearLayoutManager(this);
recyclerCategory.setLayoutManager(layoutManager);
recyclerCategory.setItemAnimator(new DefaultItemAnimator());
recyclerCategory.setHasFixedSize(true);
recyclerCategory.setAdapter(adapter);
allCategory = myDb.getCategoryByType(CategoryType);
adapter.notifyDataSetChange();
if (allCategory.size() == 0) {
Toast.makeText(this, "No Category", Toast.LENGTH_SHORT).show();
}
}
and inside setPositiveButton , replace
adapter = new CategoryAdapter(allCategory);
recyclerCategory.setAdapter(adapter);
adapter.notifyDataSetChanged();
with
adapter.notifyDataSetChanged();
Also replace
allCategory = myDb.getCategoryByType(CategoryType);
with
allCategory.addAll(myDb.getCategoryByType(CategoryType));
And try to run your program again.
change your onCreate
in this way
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_category);
myDb = new MyDatabase(this);
recyclerCategory = (RecyclerView) findViewById(R.id.recyclerCategory);
CategoryType = CaseActivity.CategoryType;
Log.d(GlobalConstant.KEY_CATEGORY_TYPE, "" + CategoryType);
allCategory = myDb.getCategoryByType(CategoryType);
if (allCategory.size() == 0) {
Toast.makeText(this, "No Category", Toast.LENGTH_SHORT).show();
}
adapter = new CategoryAdapter(allCategory);
recyclerCategory.setAdapter(adapter);
layoutManager = new LinearLayoutManager(this);
recyclerCategory.setLayoutManager(layoutManager);
recyclerCategory.setItemAnimator(new DefaultItemAnimator());
recyclerCategory.setHasFixedSize(true);
}
and now you can use adapter.notifyDataSetChanged();
without recreation of adapter
and set them to RecyclerView
if you make in setPositiveButton
allCategory = myDb.getCategoryByType(CategoryType);
then your reference to allCategory
is lost.
you need instead
allCategory.clear();
allCategory.addAll(myDb.getCategoryByType(CategoryType));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.