Dynamically updating a specific view in a RecyclerView - android

I'm currently working on android using RecyclerView, let's say I have 2 TextView in my custom row, I wanted to dynamically change the text of one in the TextView, how can I do that?
I do have the following code in my MainActivity
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private AdaptersOnline adaptersOnline;
private RecyclerView.LayoutManager mLayoutManager;
private List<ModelClientInformation> modelOnlineLists = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = getApplicationContext();
mLayoutManager = new LinearLayoutManager(this);
adaptersOnline = new AdaptersOnline(this, modelOnlineLists);
recyclerView = (RecyclerView)findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(adaptersOnline);
}
//call this to new row
public void initializeClient(String id, String data1, String data2){
this.modelOnlineLists.add(new ModelClientInformation(id, data1, data2));
adaptersOnline.notifyDataSetChanged();
}
//Call this method to update textview
public void updateSpecificViewItem(String theID){
//get position base on the ID
adaptersOnline.updateTextView(
adaptersOnline.getPositionBaseOnItemID(theID));
}
}
in my Adapter Class
public class AdaptersOnline extends RecyclerView.Adapter<AdaptersOnline.TheViewHolder> {
Context mContext;
public List<ModelClientInformation> onlineList;
public AdaptersOnline(Context mContext, List<ModelClientInformation> modelOnlineList){
this.mContext = mContext;
this.onlineList = modelOnlineList;
}
public class TheViewHolder extends RecyclerView.ViewHolder {
TextView text1, text2;
public TheViewHolder(View view) {
super(view);
text1 = (TextView)view.findViewById(R.id.text1);
text2 = (TextView)view.findViewById(R.id.text2);
}
}
#Override
public TheViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.recyclerview_row, parent, false);
return new TheViewHolder(itemView);
}
#Override
public void onBindViewHolder(TheViewHolder holder, int position) {
final ModelClientInformation info = onlineList.get(position);
holder.text1.setText(holder.getText1());
holder.text1.setText(holder.getText2());
}
/**Function to Return the size of List**/
#Override
public int getItemCount() {
return onlineList.size();
}
/**Function to Clear the List**/
public void clear(){
onlineList.clear();
}
/**Possibly way to update one of the TextView here**/
public void updateTextView(int position){
//what should I do to update the TextView
}
/*Get the position of item inside data list base on the given ID*/
public int getPositionBaseOnItemID(String theID) {
int length = onlineList.size();
for (int i = 0; i < length; i ++) {
if(onlineList.get(i).getItemID().equals(theID)) {
return i;
}
}
return -1; //Item not found
}
}
and the Pojo
public ModelClientInformation class{
private String theID, text1, text2;
public ModelClientInformation(String theID, String text1, String text2){
this.theID = theID;
this.text1 = text1;
this.text2 = text2;
}
public String getItemID(){
return theID;
}
public String getText1(){
return text1;
}
public String getText2(){
return text2;
}
}
I'm don't have any idea to do it. . .
Anyone can help me?
UPDATE:
Please look at my changes,
1: I want to update one of the TextView inside MainActivity class by calling updateSpecificViewItem("theID").
2: Get the position of the item base on the given id by calling getPositionBaseOnItemID("theID").
3: To finally update the specific item, I want to call updateTextView(int position) method.
the only problem I'm facing right now is the number 3, how can I update only the text2 and not the entire item?

You need to override onBindViewHolder (TheViewHolder holder, int position, List payload)
#Override
public void onBindViewHolder(HelloViewHolder holder, int position, List<Object> payload) {
if (payloads.isEmpty()) {
super.onBindViewHolder(holder, position , payloads);
}else{
for (Object payload : payloads) {
if (payload instanceof String) {
holder.textView.setText(payload.toString)
}
}
}
}
And to update your textView you just need to call
adapter.notifyItemChanged(position , "an string for example")
This gives you a partial update of your view.
Hope this helps.

Solving my problem
MainActivity
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private AdaptersOnline adaptersOnline;
private RecyclerView.LayoutManager mLayoutManager;
private List<ModelClientInformation> modelOnlineLists = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = getApplicationContext();
mLayoutManager = new LinearLayoutManager(this);
adaptersOnline = new AdaptersOnline(this, modelOnlineLists);
recyclerView = (RecyclerView)findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(adaptersOnline);
}
//call this to new row
public void initializeClient(String id, String data1, String data2){
this.modelOnlineLists.add(new ModelClientInformation(id, data1, data2));
adaptersOnline.notifyDataSetChanged();
}
//Call this method to update specific Item
public void updateSpecificViewItem(String theID, String newText){
int position = adaptersOnline.getPositionBaseOnItemID(theID); // get position base on the ID
ModelClientInformation oldItem = adaptersOnline.getOnlineList().get(position); // From my Adapter I created a new method `getOnlineList()` that returns the list item of specific position.
ModelClientInformation newItem = new ModelClientInformation(
oldItem.ItemID(), // get and add the old Item ID
oldItem.getText1(), // Get and add the old Text1
newText // add the new text for text2
);
adaptersOnline.updateTextView(position, newItem); // call updateTextView() from the adapter and pass the position and the newItem.
}
}
Adapter
public class AdaptersOnline extends RecyclerView.Adapter<AdaptersOnline.TheViewHolder> {
Context mContext;
public List<ModelClientInformation> onlineList;
public AdaptersOnline(Context mContext, List<ModelClientInformation> modelOnlineList){
this.mContext = mContext;
this.onlineList = modelOnlineList;
}
public class TheViewHolder extends RecyclerView.ViewHolder {
TextView text1, text2;
public TheViewHolder(View view) {
super(view);
text1 = (TextView)view.findViewById(R.id.text1);
text2 = (TextView)view.findViewById(R.id.text2);
}
}
#Override
public TheViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.recyclerview_row, parent, false);
return new TheViewHolder(itemView);
}
#Override
public void onBindViewHolder(TheViewHolder holder, int position) {
final ModelClientInformation info = onlineList.get(position);
holder.text1.setText(holder.getText1());
holder.text1.setText(holder.getText2());
}
/**Function to Return the size of List**/
#Override
public int getItemCount() {
return onlineList.size();
}
/**Function to Clear the List**/
public void clear(){
onlineList.clear();
}
/**Function to return the Data List**/
public List<ModelClientInformation> getOnlineList(){
return this.onlineList;
}
/**Function to update the specific Item**/
public void updateTextView(int position, ModelClientInformation newItem){
onlineList.set(position, newItem); //set the item
notifyItemChanged(position, newItem); //notify the adapter for changes
}
/*Get the position of item inside data list base on the given ID*/
public int getPositionBaseOnItemID(String theID) {
int length = onlineList.size();
for (int i = 0; i < length; i ++) {
if(onlineList.get(i).getItemID().equals(theID)) {
return i;
}
}
return -1; //Item not found
}
}
Pojo
public ModelClientInformation class{
private String theID, text1, text2;
public ModelClientInformation(String theID, String text1, String text2){
this.theID = theID;
this.text1 = text1;
this.text2 = text2;
}
public String getItemID(){
return theID;
}
public String getText1(){
return text1;
}
public String getText2(){
return text2;
}
}
I'm not so sure if this is how it should be done but I'm able to update the specific List Item and it's view base on position.
Thanks to everyone who gave me the idea of notifyItemChanged() ! hope this help the other too.

In your adapter,
String textToUpdate;
#Override
public void onBindViewHolder(TheViewHolder holder, int position) {
final ModelClientInformation info = onlineList.get(position);
holder.text1.setText(holder.getText1());// update the textview u want by setting the "textToUpdate" variable
holder.text1.setText(holder.getText2());
}
public void updateTextView(String text){
textToUpdate = text;
notifyDatasetChanged();
}
Hope it helps !

Create a a public static in the RecylerView class, simply assign a text in that method. Next, in the class where your geting a hook to the recycler view instance, make sure to add a onclick and ontouch interface to store the position of the selected text row. Use the recycler view instance to calling notifyItemChanged.
Here is a description from Documentation.
notifyItemChanged added in version 22.1.0
void notifyItemChanged (int position)
Notify any registered observers that the item at position has changed. Equivalent to calling notifyItemChanged(position, null);.
This is an item change event, not a structural change event. It indicates that any reflection of the data at position is out of date and should be updated. The item at position retains the same identity.

Related

Dynamically Calculate Sum on RecyclerView Android

App Screen
I want to automatically get the sum of the items once added in the recycler view and at the same time get the sum of the items when the Quantity changes and when an item from the recyclerView is removed. I am new in Android, and I cannot think of any way to achieve this. I am thinking something like the code below, but I don't know how to implement it.
TextView ProductPrice = itemView.findViewById(R.id.productPricelbl);
TextView QTY = itemView.findViewById(R.id.Quantity);
Int total;
private void GetTotal(){
total += Integer.parseInt(ProductPrice.getText().toString()) * Integer.parseInt(QTY .getText().toString())
}
I am using a custom adapter for my recycler view with the code below
public class ListViewAdapter extends RecyclerView.Adapter<DemoVH>{
ArrayList<ProductListViewModel> productList;
Context mContext;
public ListViewAdapter(ArrayList<ProductListViewModel> productList, Context context) {
this.productList = productList;
this.mContext = context;
}
#NonNull
#Override
public DemoVH onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.recycler_view_row,parent,false);
return new DemoVH(view).linkadapter(this);
}
#Override
public void onBindViewHolder(#NonNull DemoVH holder, int position) {
final ProductListViewModel productListViewModel = productList.get(position);
holder.Product_Name.setText(productListViewModel.getProductName());
holder.Product_Barcode.setText(productListViewModel.getProductBarcode());
holder.Product_Price.setText(productListViewModel.getProductPrice());
}
#Override
public int getItemCount() {
return productList.size();
}
}
class DemoVH extends RecyclerView.ViewHolder{
TextView Product_Name;
TextView Product_Barcode;
TextView Product_Price;
TextView Product_QTY;
ImageView addQTY;
ImageView lessQTY;
private ListViewAdapter myAdapter;
int Quantity = 1;
public DemoVH(#NonNull View itemView) {
super(itemView);
Product_Name = itemView.findViewById(R.id.ProductNameLbl);
Product_Barcode = itemView.findViewById(R.id.ProductBarcodeLbl);
Product_Price = itemView.findViewById(R.id.ProductPriceLbl);
Product_QTY = itemView.findViewById(R.id.QTYLbl);
addQTY = itemView.findViewById(R.id.AddQTYBtn);
lessQTY = itemView.findViewById(R.id.lessQTYBtn);
itemView.findViewById(R.id.RemoveBtn).setOnClickListener(view -> {
myAdapter.productList.remove(getAdapterPosition());
myAdapter.notifyItemRemoved(getAdapterPosition());
});
addQTY.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Quantity = Quantity+=1;
Product_QTY.setText(String.valueOf(Quantity));
}
});
lessQTY.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(Quantity == 1){
Product_QTY.setText("1");
}else{
Quantity = Quantity-=1;
Product_QTY.setText(String.valueOf(Quantity));
}
}
});
}
public DemoVH linkadapter(ListViewAdapter adapter){
myAdapter = adapter;
return this;
}
}
You can take a global variable in recycler view adapter and keep adding the amount in that variable in onBindViewHolder().
And when any item is increased or decreased or removed you can add or decrease that amount from that global variable.

How to fill blank space when hide an item in recyclerview

i have a recyclerview in my activity and in each item i have two button and a ordernumber when minus bottom make zero the ordernumber, i want to hide this item and other item come up and fill the blank space.I hide the item with setVisibility() but i don`t know how to handle blank space.
this is my recyclerview adapter:
package com.test.mohammaddvi.snappfood.Adapter;
public class RecyclerViewBuyBasketAdapter extends RecyclerView.Adapter<RecyclerViewBuyBasketAdapter.SingleItemBuyBasket> {
private ArrayList<FinalFood> foodList;
private Context mContext;
private View view;
public RecyclerViewBuyBasketAdapter(ArrayList<FinalFood> foodList, Context mContext) {
this.foodList = foodList;
this.mContext = mContext;
}
#NonNull
#Override
public RecyclerViewBuyBasketAdapter.SingleItemBuyBasket onCreateViewHolder(ViewGroup parent, int viewType) {
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.buybasketitem, null);
return new RecyclerViewBuyBasketAdapter.SingleItemBuyBasket(view);
}
#Override
public void onBindViewHolder(final RecyclerViewBuyBasketAdapter.SingleItemBuyBasket holder, int position) {
FinalFood food = foodList.get(position);
holder.foodName.setText(food.getName());
holder.foodDetails.setText(food.getDetails());
holder.foodPrice.setText(food.getPrice());
holder.foodOrderNumber.setText(food.getOrdernumber() + "");
handleClick(holder, view);
}
private void handleClick(final RecyclerViewBuyBasketAdapter.SingleItemBuyBasket holder, final View view) {
holder.foodPlusButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
FinalFood food = foodList.get(holder.getAdapterPosition());
int orderNumber = food.getOrdernumber();
int newOrderNumber = orderNumber + 1;
food.setOrdernumber(newOrderNumber);
holder.foodOrderNumber.setText(newOrderNumber + "");
}
});
holder.foodMinusButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
FinalFood food = foodList.get(holder.getAdapterPosition());
int orderNumber = food.getOrdernumber();
if (orderNumber == 1) {
int newOrderNumber = orderNumber - 1;
food.setOrdernumber(newOrderNumber);
view.setVisibility(View.GONE);
} else {
int newOrderNumber = orderNumber - 1;
food.setOrdernumber(newOrderNumber);
holder.foodOrderNumber.setText(newOrderNumber + "");
}
}
});
}
#Override
public int getItemCount() {
return (null != foodList ? foodList.size() : 0);
}
public class SingleItemBuyBasket extends RecyclerView.ViewHolder {
TextView foodName;
TextView foodPrice;
Button foodPlusButton;
Button foodMinusButton;
TextView foodOrderNumber;
TextView foodDetails;
SingleItemBuyBasket(View itemView) {
super(itemView);
this.foodName = itemView.findViewById(R.id.foodNameInBuyBasket);
this.foodPrice = itemView.findViewById(R.id.foodPriceInBuyBasket);
this.foodDetails = itemView.findViewById(R.id.foodDetailsInBuyBasket);
this.foodPlusButton = itemView.findViewById(R.id.plusbuttonInBuyBasket);
this.foodMinusButton = itemView.findViewById(R.id.minusbuttonInBuyBasket);
this.foodOrderNumber = itemView.findViewById(R.id.ordernumberInBuyBasket);
}
}
}
and this is my activity:
package com.test.mohammaddvi.snappfood;
public class BuyBasket extends AppCompatActivity{
ArrayList<FinalFood> foods = new ArrayList<>();
RecyclerViewBuyBasketAdapter recyclerViewBuyBasketAdapter;
RecyclerView recyclerView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_buy_basket);
recyclerView = findViewById(R.id.recyclerview);
foods= (ArrayList<FinalFood>) getIntent().getSerializableExtra("final");
recyclerViewBuyBasketAdapter = new RecyclerViewBuyBasketAdapter(foods, BuyBasket.this);
recyclerView.setLayoutManager(new LinearLayoutManager(BuyBasket.this, LinearLayoutManager.VERTICAL, false));
recyclerView.setHasFixedSize(true);
recyclerView.setAdapter(recyclerViewBuyBasketAdapter);
}
}
It's better you delete the entire row rather than changing visibility. This is the code to remove the row. You need to call removeItem(position); method in your button onclickListener
public void removeItem(int position)
{
// Remove specified position
models.remove(position);
// Notify adapter to remove the position
notifyItemRemoved(position);
// Notify adapter about data changed
notifyItemChanged(position);
// Notify adapter about item range changed
notifyItemRangeChanged(position, arraylist.size());
}
Inside your xml, define two parents. One for the hidden content and the other for the item that you want to appear when you hide the first one
Try playing with View when clicking in one parent or the other:
layout.setVisibility(View.GONE) or view.setVisibility(View.VISIBLE);

RecyclerView: wrong position in filtered List

I have a RecyclerView list of CardView items. I then use a simple filter method with a SearchView widget to filter the list. When I then click on a filtered CardView to launch a CardViewDetails Activity, the UI is showing the CardView from the original List and not the filtered List. For example, I have a list of twenty items in the original List. When I enter a search constraint the filtered List correctly shows three CardViews in the RecyclerView. When I click on the third CardView in the List, the UI returns the third CardView from the original List and not the third CardView from the filtered List. What am I missing here?
Adapter:
public class MyRecylerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private List<ListItem> mListItems, filteredList;
Context mContext;
private RecyclerItemClickListener recyclerItemClickListener;
private RecyclerView mRecyclerView;
/**********************************************************/
private String searchString = "";
/**********************************************************/
public MyRecylerAdapter(Context context, List<ListItem> listItems) {
this.mContext = context;
this.mListItems = listItems;
this.filteredList = new ArrayList<>();
this.filteredList.addAll(this.mListItems);
}
// RecyclerItemClickListener is the public interface file used to reach the MainActivity
public void setOnItemClickListener(RecyclerItemClickListener recyclerItemClickListener) {
this.recyclerItemClickListener = recyclerItemClickListener;
}
// Get the Item's position.
public ListItem getItem(int position) {
return filteredList.get(position);
}
#Override
public int getItemCount() {
if (filteredList.size() >0) {
return filteredList.size();
}
else {
return mListItems.size();
}
}
public void setFilter(List<ListItem> listItems, String searchString) {
// Note: the String is to get s.toString() from the Main Activity SearchView.
filteredList = new ArrayList<>();
filteredList.addAll(listItems);
this.searchString = searchString;
notifyDataSetChanged();
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_contact_item, parent, false);
final ItemHolder itemHolder = new ItemHolder(view);
// Attach a Click listener to the items's (row) view.
// itemView is from the ItemHolder() below.
// onItemClick is the click method in MainActivity.
itemHolder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int adapterPos = itemHolder.getAdapterPosition(); // get the item position.
if (adapterPos != RecyclerView.NO_POSITION) {
if (recyclerItemClickListener != null) {
// pass the item to the Main Activity
// through the RecyclerItemClickListener file and its
// public interface.
recyclerItemClickListener.onItemClick(itemHolder.itemView,adapterPos);
}
}
}
});
return itemHolder;
}
private static class ItemHolder extends RecyclerView.ViewHolder {
private TextView cardBlankText2;
private ItemHolder(View itemView) {
super(itemView);
cardBlankText2 = (TextView) itemView.findViewById(R.id.cardBlankText2);
}
public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
final ListItem listItem = filteredList.get(position);
final ItemHolder itemHolder = (ItemHolder) holder;
itemHolder.cardBlankText2.setText(listItem.getTodo());
}
Activity:
public class MainActivity extends AppCompatActivity implements
RecyclerItemClickListener {
private List<ListItem> allList = new ArrayList<>();
private RecyclerView mRecyclerView;
private SQLiteDB sqLiteDB;
private MyRecylerAdapter adapter;
private CardView cardview;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sqLiteDB = SQLiteDB.getInstance(this);
mRecyclerView = (RecyclerView)findViewById(R.id.list_recyclerview);
final LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
mRecyclerView.setLayoutManager(layoutManager);
allList = sqLiteDB.getAllDBItems();
adapter = new MyRecylerAdapter(this, allList);
adapter.setOnItemClickListener(this);
mRecyclerView.setAdapter(adapter);
}
#Override
public void onItemClick(View view, int position) {
cardview = (CardView) view;
cardview.setEnabled(false);
// Create a new intent to send data from this MainActivity to the CardViewDetails
// Activity.
Intent intent = new Intent(this,CardViewDetails.class);
ListItem listItem = adapter.getItem(position);
// Add the item object to the Intent. The item object can be used because the
// model class implements Parcelable so it holds all of the getters
// that can be snagged in the next Activity with the
// getParcelableExtra method.
intent.putExtra("item",listItem);
intent.putExtra("position",position);
startActivity(intent);
finish();
}
// SearchView
final EditText mSearchEditText = (EditText) mSearchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
mSearchEditText.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
final ArrayList<ListItem> filteredModelList = filter(allList, s.toString());
if (!mSearchView.isIconified() && filteredModelList.size() == 0) {
Toast.makeText(MainActivity.this, "Not Found", Toast.LENGTH_SHORT).show();
// re-load the list so the Adapter refreshes the RecyclerView list View.
adapter.clear();
adapter.addAll(allList);
} else if (!mSearchView.isIconified() && filteredModelList.size() > 0) {
adapter.setFilter(filteredModelList, s.toString());
mRecyclerView.scrollToPosition(0);
}
}
}
});
private ArrayList<ListItem> filter(List<ListItem> models, String query) {
query = query.toLowerCase();
final ArrayList<ListItem> filteredModelList = new ArrayList<>();
for (ListItem listItem : models) {
final String text = listItem.getTodo().toLowerCase();
final String text2 = listItem.getNote1().toLowerCase();
final String text3 = listItem.getNote2().toLowerCase();
if (text.contains(query) || text2.contains(query) ||
text3.contains(query)) {
filteredModelList.add(listItem);
}
}
return filteredModelList;
}
RecyclerItemClickListener:
public interface RecyclerItemClickListener {
void onItemClick(View view, int position);
}
CardViewDetails:
public class CardViewDetails extends AppCompatActivity {
private int position;
private SQLiteDB helper;
List<ListItem> listItems;
private CardView cardview;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_details);
final CardView cardView = (CardView) findViewById(R.id.dets);
// Create a variable for the skychill footer text.
final TextView skychilltext5;
// A db helper instance is needed for the removeItem() below
// when the user Longclicks the skycard for deletion.
helper = new SQLiteDB(this);
// Get the position of the clicked on R. list CardView from
// the MainActivity's intent bundle.
Bundle extras = getIntent().getExtras();
if (extras != null) {
// get the CardView item using the int position from the
// MainActivity's onItemClick() and the putExtra in the intent.
position = extras.getInt("position",0); // 0 is default value
}
cb2 = (TextView) findViewById(R.id.cb2);
helper = new SQLiteDB(this);
listItems = new ArrayList<>();
listItems = helper.getAllDBItems();
cb2.setText(listItems.get(position).getTodo());
...
}
After applying filter the sql DB (getAllDBItems ) data remain same. You are passing only position to CardViewDetail. And the sql data is of original list.
You should pass your ListItem as parcelable to CardViewDetails instead of position. your problem will be solved.

Unable to add second child in recyclerview

Unable to add second child in Recyclerview I am passing two different arrays to RecyclerAdapter to display two child layout with different data and views.Is there any solution to add different child layout using same header layout.I added horizontal Recyclerview in vertical Recyclerview and I want to display details like I attached the image
private void setupRecyclerView(RecyclerView recyclerView) {
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
RecyclerAdapter recyclerAdapter = new RecyclerAdapter();
int[] images = new int[]{
R.drawable.finance,
R.drawable.business,
R.drawable.financejob,
R.drawable.ecomchallenges
};
ArrayList<ChildView> childViews = new ArrayList<>();
childViews.add(new ChildView(images[0], "The \"Best\" Startup Pitch Deck - How To Raise Venture Capital", "$100"));
childViews.add(new ChildView(images[1], "An Entire MBA in 1 Course:Award Winning Business School Prof", "$100"));
childViews.add(new ChildView(images[2], "What Finance Job is for You? Explanation of 14 Finance Roles", "$100"));
childViews.add(new ChildView(images[3], "Learn To Build Beautiful HTML5 And CSS3 Websites In 1 Month", "$100"));
int[] courseImage = new int[] {
R.drawable.php,
R.drawable.development,
R.drawable.web,
R.drawable.java
};
ArrayList<CourseByType> courseByTypes = new ArrayList<>();
courseByTypes.add(new CourseByType("Technology", courseImage[0]));
courseByTypes.add(new CourseByType("Business", courseImage[1]));
courseByTypes.add(new CourseByType("Photography", courseImage[2]));
courseByTypes.add(new CourseByType("Development", courseImage[3]));
Log.d("","Above adapter");
recyclerAdapter.addItem(new GroupView("Business", childViews));
Log.d("","Below Child");
recyclerAdapter.addCourseByType(new CourseByHeader("Technology", courseByTypes));
Log.d("","Below Course");
recyclerView.setAdapter(recyclerAdapter);
}
This is the main fragment where I set the values to two different
arraylist ArrayList<ChildView> childViews = new ArrayList<>()
and
ArrayList<CourseByType> courseByTypes = new ArrayList<>()
Values of child views are passing properly but CourseByType values are not passing.This is the adapter class for this fragment class.
RecyclerAdapter.java
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {
ArrayList<PassValues> containerArrayList;
ArrayList<GroupView> groupViews;
ArrayList<CourseByHeader>courseByHeaders;
private static final int TYPE_HEADER = 0;
private static final int TYPE_ITEM = 1;
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context = parent.getContext();
View view = LayoutInflater.from(context).inflate(R.layout.group_title, parent, false);
return new ViewHolder(view);
}
public RecyclerAdapter(){
containerArrayList = new ArrayList<>();
groupViews = new ArrayList<>();
courseByHeaders = new ArrayList<>();
}
public void addContainer(PassValues container){
containerArrayList.add(container);
}
public void addItem(GroupView groupView){
Log.d("","Inside Group method");
groupViews.add(groupView);
}
public void addCourseByType(CourseByHeader courseByHeader){
Log.d("","Inside Course method");
courseByHeaders.add(courseByHeader);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
Log.d("", "Pass Values out of IF" + position);
ChildViewAdapter childViewAdapter = new ChildViewAdapter();
if(position == 0){
GroupView groupView = groupViews.get(position);
holder.title.setText(groupView.getTitle());
Log.d("", "Passing Values" + groupView.getTitle());
holder.recyclerView.setLayoutManager(new LinearLayoutManager(holder.recyclerView.getContext(), LinearLayoutManager.HORIZONTAL, false));
holder.recyclerView.setOnFlingListener(null);
childViewAdapter.addChild(groupView.getChildViewList());
holder.recyclerView.setAdapter(childViewAdapter);
}
if (position == 1) {
CourseByHeader courseByHeader = courseByHeaders.get(position);
holder.title.setText(courseByHeader.getTitle());
Log.d("", "Passing Values" + courseByHeader.getTitle());
holder.recyclerView.setLayoutManager(new LinearLayoutManager(holder.recyclerView.getContext(), LinearLayoutManager.HORIZONTAL, false));
holder.recyclerView.setOnFlingListener(null);
childViewAdapter.addCourse(courseByHeader.getCourseByTypes());
holder.recyclerView.setAdapter(childViewAdapter);
}
}
#Override
public int getItemCount() {
if(getItemViewType(0) == TYPE_HEADER)
return groupViews.size() ;
if (getItemViewType(1) == TYPE_ITEM)
return courseByHeaders.size();
else return -1;
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView title;
RecyclerView recyclerView;
public ViewHolder(View itemView) {
super(itemView);
title = (TextView)itemView.findViewById(R.id.course_title);
recyclerView = (RecyclerView)itemView.findViewById(R.id.group_recycler);
}
}
}
This RecyclerAdapter contains one RecyclerView in that first row has one image and 3 textviews and 2nd row has 1 ImageView and 1 TextView. At position first,one image and 3 textviews are shown but it's not going on 2nd view
This is the view I getting after run on emulator.
This are two child for RecyclerViews
ChildView.java
public class ChildView {
int image;
String course, price;
public ChildView(int image, String course, String price) {
this.image = image;
this.course = course;
this.price = price;
}
public int getImage() {
return image;
}
public String getCourse() {
return course;
}
public String getPrice() {
return price;
}
}
CourseByType.java
public class CourseByType {
String courseName;
int courseImage;
public CourseByType(String courseName, int courseImage) {
this.courseName = courseName;
this.courseImage = courseImage;
}
public String getCourseName() {
return courseName;
}
public int getCourseImage() {
return courseImage;
}
}
CourseByHeader.java
public class CourseByHeader {
String title;
ArrayList<CourseByType> courseByTypes;
public CourseByHeader(String title, ArrayList<CourseByType> courseByTypes) {
this.title = title;
this.courseByTypes = courseByTypes;
}
public String getTitle() {
return title;
}
public ArrayList<CourseByType> getCourseByTypes() {
return courseByTypes;
}
}
GroupView.java
public class GroupView {
String title;
ArrayList<ChildView> childViewList;
String courseBy;
ArrayList<CourseByType> courseByTypes;
public GroupView(String title, ArrayList<ChildView> childViewList) {
this.title = title;
this.childViewList = childViewList;
}
public String getTitle() {
return title;
}
public ArrayList<ChildView> getChildViewList() {
return childViewList;
}
}
Groupview and CouseByType class have title and child list for recycleradapter
ChildViewAdapter.java
public class ChildViewAdapter extends RecyclerView.Adapter {
ArrayList<ChildView> childViewList;
ArrayList<CourseByType> courseByTypes;
private static final int TYPE_HEADER = 0;
private static final int TYPE_ITEM = 1;
public class ViewHolder extends RecyclerView.ViewHolder{
public ViewHolder(View itemView) {
super(itemView);
}
}
public class GroupHolder extends ViewHolder {
public ImageView iamView;
public TextView course, price;
public GroupHolder(View itemView) {
super(itemView);
iamView = (ImageView) itemView.findViewById(R.id.course_image);
course = (TextView) itemView.findViewById(R.id.course_by);
price = (TextView) itemView.findViewById(R.id.price);
}
}
public void addCourse(ArrayList<CourseByType> courseByType){
courseByTypes = courseByType;
}
public void addChild(ArrayList<ChildView> childView){
childViewList = childView;
}
public class Course extends ViewHolder {
public ImageView courseTypeImage;
public TextView courseType;
public Course(View itemView) {
super(itemView);
courseTypeImage = (ImageView)itemView.findViewById(R.id.course_image);
courseType = (TextView)itemView.findViewById(R.id.course_name_course);
}
}
public ChildViewAdapter() {
childViewList = new ArrayList<>();
courseByTypes = new ArrayList<>();
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context = parent.getContext();
RecyclerView.ViewHolder vh = null;
View v;
if(viewType == TYPE_HEADER){
v = LayoutInflater.from(context).inflate(R.layout.recycler_item, parent, false);
return new GroupHolder(v);
}if(viewType == TYPE_ITEM){
v = LayoutInflater.from(context).inflate(R.layout.type_of_courses, parent, false);
return new Course(v);
}
return vh;
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if(holder instanceof GroupHolder){
Log.d("","instance of Group Holder");
ChildView childView = childViewList.get(position);
((GroupHolder)holder).iamView.setImageResource(childView.getImage());
((GroupHolder)holder).course.setText(childView.getCourse());
((GroupHolder)holder).price.setText(childView.getPrice());
return;
}
if(holder instanceof Course){
Log.d("","instance of Course ");
CourseByType courseByType = courseByTypes.get(position);
((Course)holder).courseTypeImage.setImageResource(courseByType.getCourseImage());
((Course)holder).courseType.setText(courseByType.getCourseName());
return;
}
}
#Override
public int getItemCount() {
int size;
if(childViewList.size()>0){
return size = childViewList.size();
}else return size = courseByTypes.size();
}
#Override
public int getItemViewType(int position) {
if(childViewList.size() != 0 && childViewList.size()>0){
return TYPE_HEADER;
}else return TYPE_ITEM;
}
}
This childview adapter has two view types first is one image and 3 text and second view type contain one image and one text.When I pass values from fragment only first view type get displayed and second view type not gets value from fragment.
To show multiple different views in a recyclerview, you have to override getItemViewType() in the recyclerview adapter.
//getItemViewType enables dynamic viewholder creation
#Override
public int getItemViewType(int position) {
//you will need to add a integer with variable name viewTypeCode
//for view1 set viewTypeCode = 100 and for view2 set viewTypeCode = 200
viewTypeCode = itemList.get(position).getViewTypeCode();
return viewTypeCode;
}
This is how the onCreateViewHolder will be different for multiple viewtypes. You will have to modify yours like this
#Override
public FeedViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
switch (viewType) {
case 100: return new FeedViewHolder(layoutInflater.inflate(R.layout.v1, parent, false),100);
case 200: return new FeedViewHolder(layoutInflater.inflate(R.layout.v2, parent, false),200);
}
return null;
}
OnBindViewHolder will be similarly modified
#Override
public void onBindViewHolder(final FeedViewHolder holder, int position) {
viewTypeCode = itemList.get(position).getViewTypeCode();
switch ( viewTypeCode) {
case 100:
//your code for v1
case 200:
//your code for v2
}
}
Similarly the ViewHolder class is modified
class FeedViewHolder extends RecyclerView.ViewHolder{
//declare variables here
public FeedViewHolder(View v, int viewType) {
super(v);
switch (viewType) {
//instead of itemView.findViewById you will have to use v.findViewById
case 100:
//your code for v1
case 200:
//your code for v2
}
}
For further reference refer to this SO answer
Don't pass two separate list.Make a custom class like this-
class MyClass {
int viewTypeCode;
CustomClass1 c1;
CustomClass2 c2;
//add the setter getter
}
In your activity while preparing the data.
List<MyClass> itemList = new ArrayList<>();
//put whatever logic you need to make the order of the list
//if CustomClass1 object is put then setViewTypeCode(100), setCustomClass2 = null
//if CustomClass2 object is put then setViewTypeCode(200), setCustomClass1 = null
After data is built, then send this to the adapter.

android: Sort List after Adding items to the existing list using RecyclerView.Adapter

I have a RecyclerView which shows list of items sorted in descending order that works fine when I initially launch the screen, but when I add some more items to the existing list and setting it to the adapter with notifyDataSetChanged() method the existing list gets sorted in ascending order and the new items get added at the bottom of the view.
below is adapter
public class RecycleAdapter extends RecyclerView.Adapter<RecycleAdapter.ViewHolder> {
private List<Cinema> lists;
private int savedItems;
public RecycleAdapter(List<Cinema> cinemas, int savedItems) {
this.lists = cinemas;
this.savedItems = savedItems;
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView cinemaTitle;
public TextView cinemaDescription;
public TextView cinemaDate;
public LinearLayout newLayout;
public ViewHolder(View view) {
super(view);
cinemaTitle = (TextView) view.findViewById(R.id.cinema_title);
cinemaDescription = (TextView) view.findViewById(R.id.description_text);
cinemaDate = (TextView) view.findViewById(R.id.cinema_date);
newLayout = (LinearLayout) view.findViewById(R.id.new_cinema_layout);
}
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_cinema, parent, false);
return new ViewHolder(itemView);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
Cinema cinema = cinemas.get(position);
if (cinema.getId() > savedItems) {
holder.newLayout.setVisibility(View.VISIBLE);
} else {
holder.newLayout.setVisibility(View.INVISIBLE);
}
String title = cinema.getMessage().trim();
String description = cinema.getDescription().trim();
String date = cinema.getPublishedDate().trim();
holder.cinemaTitle.setText(title);
holder.cinemaDescription.setText(description);
holder.cinemaDate.setText(date);
}
public void setCineams(List<Cinema> cinemas) {
this.cinemas = cinemas;
notifyDataSetChanged();
}
#Override
public int getItemCount() {
return cinemas.size();
}
}
and the method which updates the list from Fragment is given below:
#Override
public void onCinemaUpdated() {
cinemas = firebaseHelper.getAllCinemas();
//Method which sorts the lists in descending order after adding new items into it
sortCinemas();
if (recycleAdapterAdapter != null) {
recycleAdapterAdapter.setCineams(cinemas);
}
}
I am not sure why am I getting this behaviour. Can anyone clarify on this?
Change your constructor and setCinemas method code to : Let me know if it helps..
public RecycleAdapter(List<Cinema> cinemas, int savedItems) {
this.lists = cinemas;
this.savedItems = savedItems;
}
public void setCineams(List<Cinema> cinemas) {
this.lists = cinemas;
notifyDataSetChanged();
}

Categories

Resources