I making an app for tablet so it will have two recyclerview on one activity. What I want to do is when I click an item on first recyclerview, the item information will show automatically on another recyclerview as a list. But what actually my coding do is when I click the item, the item not show automatically on another recyclerview. When I exit the app and relaunch back, finally show the item on another recyclerview.
Actually I have tried put notifysetdatachanged but it still the same.
This MainActivity.java
private void displayItemPurchased() {
itemList = new ArrayList<>();
Cursor cursorItemPurchased = myDb.getItemPurchased();
while (cursorItemPurchased.moveToNext()) {
String itemId = cursorItemPurchased.getString(1);
String itemDescription = cursorItemPurchased.getString(2);
int itemQty = cursorItemPurchased.getInt(3);
Double itemAmt = cursorItemPurchased.getDouble(4);
String itemTimestamp = cursorItemPurchased.getString(5);
Product_Item item = new Product_Item();
item.setItemDescription(itemId);
item.setItemDescription(itemDescription);
item.setItemQty(itemQty);
item.setItemAmt(itemAmt);
item.setItemTimestamp(itemTimestamp);
itemList.add(item);
}
adapterItemsList = new AdapterItemsList(getApplication(), itemList, cursorItemPurchased);
rvList.setAdapter(adapterItemsList);
adapterItemsList.notifyDataSetChanged();
}
This AdapterItem.java
public class AdapterItem extends RecyclerView.Adapter<AdapterItem.MyViewHolder> {
private int row_index = -1;
private ArrayList<Product_Item> itemsList;
private Context ctx;
private LayoutInflater inflater;
//SQLite
DatabaseHelper myDb;
public AdapterItem(Context ctx, ArrayList<Product_Item> itemsList) {
inflater = LayoutInflater.from(ctx);
this.ctx = ctx;
this.itemsList = itemsList;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.recycler_view_items_list, parent, false);
myDb = new DatabaseHelper(ctx);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull final MyViewHolder holder, final int position) {
Product_Item item = itemsList.get(position);
final String id = item.getItemId();
final String name = item.getItemDescription();
final int qty = Integer.parseInt(holder.txtQty.getText().toString());
final Double amt = item.getItemAmt();
final String timestamp = item.getItemTimestamp();
holder.itemId_tv.setText(id);
holder.itemName_tv.setText(name);
holder.btnPlus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
holder.btnMinus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
row_index = position;
notifyDataSetChanged();
myDb.insertItemPurchased(id, name, qty, amt, timestamp);
}
});
if (row_index == position) {
holder.layoutQTY.setVisibility(View.VISIBLE);
holder.layoutQTY.setAnimation(holder.fromBottom);
} else {
holder.layoutQTY.setVisibility(View.INVISIBLE);
}
}
@Override
public int getItemCount() {
return itemsList.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
Animation fromBottom, fromTop;
LinearLayout layoutQTY;
TextView itemId_tv, itemName_tv,txtQty;
ImageButton btnPlus,btnMinus;
MyViewHolder(View itemView) {
super(itemView);
fromBottom = AnimationUtils.loadAnimation(ctx,R.anim.qtyfrombottom);
fromTop = AnimationUtils.loadAnimation(ctx,R.anim.qtyfromtop);
itemId_tv = itemView.findViewById(R.id.txtItemId);
itemName_tv = itemView.findViewById(R.id.txtName);
layoutQTY = itemView.findViewById(R.id.layoutQTY);
txtQty = itemView.findViewById(R.id.txtQty);
btnPlus = itemView.findViewById(R.id.btnPlus);
btnMinus = itemView.findViewById(R.id.btnMinus);
}
}
}
And This AdapterItemList
public class AdapterItemsList extends RecyclerView.Adapter<AdapterItemsList.ItemListViewHolder> {
private ArrayList<Product_Item> itemsList;
private Context mContext;
private Cursor mCursor;
public AdapterItemsList(Context ctx, ArrayList<Product_Item> itemsList, Cursor cursor){
mContext = ctx;
this.itemsList = itemsList;
mCursor = cursor;
}
public class ItemListViewHolder extends RecyclerView.ViewHolder{
public TextView nameText;
public TextView countText;
public TextView priceText;
public ItemListViewHolder(@NonNull View itemView) {
super(itemView);
nameText = itemView.findViewById(R.id.txtItemList);
countText = itemView.findViewById(R.id.txtQty);
priceText = itemView.findViewById(R.id.txtPriceItem);
}
}
@NonNull
@Override
public ItemListViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
LayoutInflater inflater = LayoutInflater.from(mContext);
View view = inflater.inflate(R.layout.recycler_view_pay_list_item,viewGroup,false);
return new ItemListViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ItemListViewHolder holder, int position) {
Product_Item item = itemsList.get(position);
final String id = item.getItemId();
final String name = item.getItemDescription();
final int qty = item.getItemQty();
final Double amt = item.getItemAmt();
final String timestamp = item.getItemTimestamp();
holder.nameText.setText(name);
holder.countText.setText(String.valueOf(qty));
holder.priceText.setText(String.valueOf(amt));
}
@Override
public int getItemCount() {
return mCursor.getCount();
}
public void swapCursor (Cursor newCursor){
if (mCursor != null){
mCursor.close();
}
mCursor = newCursor;
if (newCursor != null){
notifyDataSetChanged();
}
}
If you want to add item to recylcerview , in your adding adapter do this : - Create a method :
public void addSingleProduct(Product_Item productItem){
itemsList.add(productItem);
notifyDataSetChanged();
}
But you need also to remove it from the other adapter where you already have it Create a method :
public void removeItem(Product_Item productItem){
itemsList.remove(productItem)
int position = itemsList.indexOf(productItem);
notifyItemRemoved(position);
}
And than call the methods in where your adapter instance is , in your case the MainActivity.
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.