I am developing an android chat app and I would like the messages of the user you are chatting with to be aligned to the right and their messages to be aligned to the left. This is the condition that allows you to distinguish who the message belongs to, I can only write it on the bindview holder:
if(model.getUidSender().equals(fAuth.getCurrentUser().getUid()))
But how do I align the item to the right or left?
I have also thought about inflating two different layouts but I should go and operate in the CreateViewHolder in which I would not have the model.getUidSender value. How can I solve this problem?
This is my complete code:
#Override
protected void onStart() {
super.onStart();
CollectionReference collectRefMessage = FirebaseFirestore.getInstance().collection("roomChat")
.document("rooms")
.collection(chatId).document("message")
.collection("messages");
FirestoreRecyclerOptions<getMessage> options
= new FirestoreRecyclerOptions.Builder<getMessage>()
.setQuery(collectRefMessage.limit(1000).orderBy("date", Query.Direction.ASCENDING), getMessage.class)
.build();
FirestoreRecyclerAdapter<getMessage, getASetMsg> firestoreRecyclerAdapter = new FirestoreRecyclerAdapter<getMessage, getASetMsg>(options) {
#Override
protected void onBindViewHolder(#NonNull #NotNull getASetMsg holder, int position, #NonNull #NotNull getMessage model) {
holder.txtMsgText.setText(model.getText());
holder.dateATime.setText(model.getDate());
// uidCh = model.getUidSender();
//if(model.getUidSender().equals(fAuth.getCurrentUser().getUid()))
}
#NonNull
#NotNull
#Override
public getASetMsg onCreateViewHolder(#NonNull #NotNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_chat,parent,false);
getASetMsg viewHolder = new getASetMsg(view);
return viewHolder;
}
};
rView.setAdapter(firestoreRecyclerAdapter);
firestoreRecyclerAdapter.startListening();
}
public static class getASetMsg extends RecyclerView.ViewHolder {
TextView txtMsgText, dateATime;
public getASetMsg(#NonNull View itemView) {
super(itemView);
txtMsgText = itemView.findViewById(R.id.txtMsgText);
dateATime = itemView.findViewById(R.id.dateATime);
}
}
I managed to solve it like this:
if(model.getUidSender().equals(fAuth.getCurrentUser().getUid())) {
final FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) holder.messageCard.getLayoutParams();
params.gravity = GravityCompat.END; // or GravityCompat.END
holder.messageCard.setLayoutParams(params);
}
So, this is the chat card layout:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_alignParentEnd="true"
app:cardBackgroundColor="#color/textC"
android:layout_marginTop="10dp"
>
<com.google.android.material.card.MaterialCardView
android:id="#+id/messageCard"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:cardBackgroundColor="#color/textC"
app:cardCornerRadius="20dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/txtMsgText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hi, this is a message"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:textColor="#FFFFFF"
android:layout_marginTop="25dp"
android:layout_marginBottom="20dp"
android:textSize="22dp"/>
<TextView
android:id="#+id/dateATime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="30dp"
android:text="32/07/2021 00:00"
android:layout_marginTop="5dp"
android:textColor="#color/white"
android:layout_marginLeft="30dp"
/>
</RelativeLayout>
</com.google.android.material.card.MaterialCardView>
</FrameLayout>
Related
I am working on food delivery application. I have four tab in menu bar Home, Daily Meal, Favourite and My Cart. My all tab is work well but when I click on Daily Meal my page is rendering. What can I try next?
Here is my code:
daily_meal_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.DailyMeal.DailyMealFragment">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/daily_meal_rec"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
daily_meal_ver.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp">
<ImageView
android:id="#+id/imgview"
android:layout_width="match_parent"
android:layout_height="200dp"
android:scaleType="centerCrop"
android:src="#drawable/breakfast"
android:foreground="#drawable/blur"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/names"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/amita_bold"
android:text="Dinner"
android:textColor="#color/white"
android:textSize="40sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="#+id/imgview"
app:layout_constraintEnd_toEndOf="#+id/imgview"
app:layout_constraintStart_toStartOf="#+id/imgview"
app:layout_constraintTop_toTopOf="#+id/imgview"
app:layout_constraintVertical_bias="0.3" />
<TextView
android:id="#+id/desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/amita"
android:text="Choose your best dinner"
android:textColor="#color/white"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/names" />
<TextView
android:id="#+id/discounts"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/off_back"
android:padding="2sp"
android:text=" 20% OFF "
android:textColor="#FFFFFF"
android:textSize="16sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="#+id/imgview"
app:layout_constraintStart_toStartOf="#+id/imgview"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.821" />
</androidx.constraintlayout.widget.ConstraintLayout>
DailyMealModel.java
public class DailyMealModel {
int image;
String name;
String discount;
String description;
// getter setter and constructor
}
DailyMealFragment.java
public class DailyMealFragment extends Fragment {
RecyclerView recyclerView;
List<DailyMealModel> dailyMealModels;
DailyMealAdapter dailyMealAdapter;
public View onCreateView(#NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.daily_meal_fragment,container,false);
recyclerView = root.findViewById(R.id.daily_meal_rec);
dailyMealModels = new ArrayList<>();
dailyMealModels.add(new DailyMealModel(R.drawable.breakfast,"Breakfast","30% OFF","Description Description"));
dailyMealModels.add(new DailyMealModel(R.drawable.lunch,"Lunch","15% OFF","Description Description"));
dailyMealAdapter = new DailyMealAdapter(getContext(),dailyMealModels);
recyclerView.setAdapter(dailyMealAdapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
return root;
}
}
DailyMealAdapter.java
public class DailyMealAdapter extends RecyclerView.Adapter<DailyMealAdapter.viewholder> {
Context context;
List<DailyMealModel> list;
public DailyMealAdapter(Context context, List<DailyMealModel> list) {
this.context = context;
this.list = list;
}
#NonNull
#Override
public viewholder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
return new viewholder(LayoutInflater.from(parent.getContext()).inflate(R.layout.daily_meal_ver,parent,false));
}
#Override
public void onBindViewHolder(#NonNull viewholder holder, int position) {
holder.img.setImageResource(list.get(position).getImage());
holder.name.setText(list.get(position).getName());
holder.discount.setText(list.get(position).getDiscount());
holder.description.setText(list.get(position).getDescription());
}
#Override
public int getItemCount() {
return list.size();
}
public class viewholder extends RecyclerView.ViewHolder {
ImageView img;
TextView name,discount,description;
public viewholder(#NonNull View itemView) {
super(itemView);
img = itemView.findViewById(R.id.imgview);
name = itemView.findViewById(R.id.names);
description = itemView.findViewById(R.id.desc);
discount = itemView.findViewById(R.id.discounts);
}
}
}
Snapshot
As your attached screenshot,
please make sure all images and icons used in the app are lower sizes even loading dynamic images for that you can use tinypng.com or tinyjpg.com
also, using pagination in RecyclerView will improve your app performance.
and make sure JSON/XML list response is minimal.
I have a problem, I want to display a list in my second Activity but it only displays the last item and not the previous ones. I need to display all my items on Activity so.
What do I need to do to display them all?
My Activity:
mRecyclerView = (RecyclerView)findViewById(R.id.home_recyclerview_pokemonname);
mPokemonList = new ArrayList<>();
mPokemonList.add(new MyPokemonBank("Pikachu", "Electrik"));
mPokemonList.add(new MyPokemonBank("Dracaufeu", "Feu"));
mPokemonList.add(new MyPokemonBank("Miaouss", "Normal"));
mAdapter = new MyPokemonAdapter(mPokemonList);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.HORIZONTAL, false));
mRecyclerView.setAdapter(mAdapter);
}
My Adapter:
public class MyPokemonAdapter extends RecyclerView.Adapter<MyPokemonAdapter.MyViewHolder> {
ArrayList<MyPokemonBank> mPokemonList;
MyPokemonAdapter(ArrayList<MyPokemonBank> mPokemonList){
this.mPokemonList = (ArrayList<MyPokemonBank>) mPokemonList;
}
#NonNull
#Override
public MyPokemonAdapter.MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View view = layoutInflater.inflate(R.layout.pokemon_bank, parent, false);
return new MyViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull MyPokemonAdapter.MyViewHolder holder, int position) {
holder.display(mPokemonList.get(position));
}
#Override
public int getItemCount() {
return mPokemonList.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder{
private TextView mPokemonName;
private TextView mPokemonType;
public MyViewHolder(#NonNull View itemView) {
super(itemView);
mPokemonName = (TextView)itemView.findViewById(R.id.name);
mPokemonType = (TextView)itemView.findViewById(R.id.type);
}
public void display(MyPokemonBank myPokemonBank) {
this.mPokemonName.setText(MyPokemonBank.getName());
this.mPokemonType.setText(MyPokemonBank.getType());
}
}
}
As #Mehul Kabaria said, the problem lies in the layout of your item views. The root view in pokemon_bank.xml has the layout_width and layout_height set to match_parent.
So actually all your items are displayed and if you scroll, you will see that they are there. But each item fills the whole screen which makes it look like only one item is displayed.
Change the width and height dimensions of the root view in pokemon_bank.xml to wrap_content or a fixed size.
<androidx.appcompat.widget.LinearLayoutCompat
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="18sp"
android:text="Pikachu">
</TextView>
<TextView
android:id="#+id/type"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom|end"
android:padding="10dp"
android:text="Electrik">
</TextView>
</androidx.appcompat.widget.LinearLayoutCompat>
Depending on whether you use a LinearLayoutManager for a vertical or horizontal list, you can set either the width or the height to match_parent.
Edit:
Also, it looks like you have an error in the binding of your model data to the item view in the RecyclerView Adapter:
This here uses static methods to get values from the class MyPokemonBank.
public void display(MyPokemonBank myPokemonBank) {
this.mPokemonName.setText(MyPokemonBank.getName());
this.mPokemonType.setText(MyPokemonBank.getType());
}
Instead, they should use the parameter (lowercase myPokemonBank):
public void display(MyPokemonBank myPokemonBank) {
this.mPokemonName.setText(myPokemonBank.getName());
this.mPokemonType.setText(myPokemonBank.getType());
}
Do not use match_parent height for your recycler view item view. Because One item fills the whole screen so you do not see another.
I have checked your layout code and problem is in your pokemon_bank.xml. You need to change from match_parent to wrap_content to your linear layout compat like this it will work fine.
<androidx.appcompat.widget.LinearLayoutCompat
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="18sp"
android:text="Pikachu">
</TextView>
<TextView
android:id="#+id/type"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="bottom|end"
android:padding="10dp"
android:text="Electrik">
</TextView>
</androidx.appcompat.widget.LinearLayoutCompat>
You need to change from horizontal to vertical also into your main activity where you are setting layout manager like this mRecyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.VERTICAL, false));
instead of this line :
mRecyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.HORIZONTAL, false));
try this one hope will work for you
mRecyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
That is my second activity XML:
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/home_recyclerview_pokemonname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</androidx.recyclerview.widget.RecyclerView>
and pokemon_bank.xml
<androidx.appcompat.widget.LinearLayoutCompat
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="18sp"
android:text="Pikachu">
</TextView>
<TextView
android:id="#+id/type"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom|end"
android:padding="10dp"
android:text="Electrik">
</TextView>
</androidx.appcompat.widget.LinearLayoutCompat>
I tried a lot of solutions but no solution has worked for me.
I am getting data from the server and showing it inside a recycleview. The data shows fine but shows this error in the logcat
Access denied finding property "persist.vendor.log.tel_dbg"
E/RecyclerView: No adapter attached; skipping layout
E/RecyclerView: No adapter attached; skipping layout
sometimes when I refresh again and again the recyclerview becomes empty.
I am using viewpager consisting of 2 fragments inside a parent fragment.
this recycleview is using in a nested scrollview
some time data not showing
Code snippet:
homebuyer_adapter_recycler=new homebuyer_adapter_recycle(getActivity(), items);
LinearLayoutManager home = new LinearLayoutManager(getActivity());
home.setOrientation(LinearLayoutManager.VERTICAL);
allitemsgrid.setLayoutManager(home);
allitemsgrid.setAdapter(r);
Here is more about
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent,
final int viewType) {
View v = LayoutInflater.from(context).inflate(R.layout.items_view,parent,
false);
RecyclerView.LayoutParams lp = new
RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
v.setLayoutParams(lp);
ViewHolder viewHolder = new ViewHolder(v);
HERE IS MY item view
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
app:cardCornerRadius="6dp"
android:layout_marginBottom="3dp"
app:cardBackgroundColor="#color/white">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="150dp">
<com.makeramen.roundedimageview.RoundedImageView
android:layout_width="match_parent"
android:layout_height="150dp"
app:riv_corner_radius="6dp"
android:id="#+id/image_items"
android:scaleType="fitXY"
/>
<com.makeramen.roundedimageview.RoundedImageView
android:layout_width="match_parent"
android:layout_height="150dp"
app:riv_corner_radius="6dp"
android:background="#drawable/blackshade"
android:scaleType="fitXY"/>
<TextView
android:id="#+id/items_all"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="#dimen/_5sdp"
android:textColor="#color/white"
android:fontFamily="sans-serif-smallcaps"
android:paddingLeft="20dp"
android:text="Message" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="20dp"
android:text="Timing"
android:padding="#dimen/_5sdp"
android:textColor="#color/white"
android:layout_alignParentBottom="true"
android:fontFamily="#font/mylight"
android:layout_alignParentRight="true"
android:textSize="10dp"
android:id="#+id/shoptimming"/>
</RelativeLayout>
<TextView
android:id="#+id/name_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:alpha="0.6"
android:fontFamily="#font/arimo_bold"
android:paddingLeft="20dp"
android:text="Name"
android:textColor="#color/black"
android:textSize="17dp"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:orientation="horizontal">
<TextView
android:id="#+id/minumum_order"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_weight="1"
android:drawablePadding="10dp"
android:fontFamily="sans-serif"
android:paddingLeft="20dp"
android:text="Minimum " />
<TextView
android:id="#+id/price_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:fontFamily="sans-serif"
android:gravity="left"
android:paddingLeft="20dp"
android:text="charges"
/>
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
Here is my adapter class
public class homebuyer_adapter_recycle extends RecyclerView.Adapter<homebuyer_adapter_recycle.ViewHolder> {
private ArrayList<seller_information> listData;
private LayoutInflater layoutInflater;
int lastpostition=-1;
Context context;
public homebuyer_adapter_recycle(Context aContext, ArrayList<seller_information> listData) {
this.listData = listData;
layoutInflater = LayoutInflater.from(aContext);
context=aContext;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder
(#NonNull ViewGroup parent, final
int viewType) {
View v = LayoutInflater.from(parent.getContext()).
inflate(R.layout.items_view,parent, false);
ViewHolder viewHolder = new ViewHolder(v);
Log.i("inadapter","calling time");
v.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
phone_number_shop = listData.get(viewType).getPhonenumber();
homebuyer.Delivery_charges_shop =
listData.get(viewType).getDiliveryfee();
homebuyer.Shop_name = listData.get(viewType).getShopname();
homebuyer.minimum_order = listData.get(viewType).getMinorder();
//profession=items.get(i).getName();
// Toast.makeText(getActivity(),phn,Toast.LENGTH_SHORT).show();
all_and_cetegory_items items = new all_and_cetegory_items();
Bundle b = new Bundle();
b.putString("phone",homebuyer.phone_number_shop);
items.setArguments(b);
FragmentTransaction fragmentTransaction =
((AppCompatActivity)context).getSupportFragmentManager()
.beginTransaction();
fragmentTransaction.replace(R.id.nav_host_fragment, items);
fragmentTransaction.addToBackStack(null).commit();
}
});
return viewHolder;
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
holder.name.setText(listData.get(position).getShopname());
//
Picasso.with(context).load(listData.get(position)
.getShopimage()).into(holder.shopimage);
Glide.with(context).load(listData.get(position).getShopimage())
.into(holder.shopimage);
holder.dilivery.setText("Rs "+listData.get(position).getDiliveryfee()+"
Delivery fee");
holder.minorder.setText("Rs "+listData.get(position).getMinorder()+" minimum");
holder.items.setText(listData.get(position).getShopmessage());
holder.time.setText("Service Available
"+listData.get(position).getStartingtime()+" to
"+listData.get(position).getEndingtime());
Log.i("inadapter","calling time"+listData.get(position).getShopname());
}
#Override
public int getItemCount() {
return listData.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView name;
TextView dilivery;
TextView items;
TextView minorder;
TextView time;
ImageView shopimage;
public ViewHolder(#NonNull View v) {
super(v);
name = (TextView) v.findViewById(R.id.name_item);
dilivery = (TextView) v.findViewById(R.id.price_item);
minorder = (TextView) v.findViewById(R.id.minumum_order);
items=(TextView)v.findViewById(R.id.items_all);
time=(TextView)v.findViewById(R.id.shoptimming);
shopimage=(ImageView) v.findViewById(R.id.image_items);
}
}
}
In error it is saying that the Adapter for RecylerView is not attached. So, try to add adapter to the layout using:
recyclerView.setAdapter(categoryAdapter);
I am assuming homebuyer_adapter_recycler is your adapter.
According to that, you haven't really set your adapter as the logcat is specifying.
Add the below code instead of allitemsgrid.setAdapter(r);
allitemsgrid.setAdapter(homebuyer_adapter_recycler);
If my assumption or suggestion is wrong, it is okay. It is probably because your question is not very clear and does not provide the necessary details. Please provide more details such as the adapter class code and what is allitemsgrid, homebuyer_adapter_recycler and r.
Before marking as duplicate et al, viewpager from recyclerview is kind of triccky. The code is able to retrieve data from db and display it in a recyclerview with cardview. I want to be able to scroll the images in the viewpager while in the recyclerview as shown in the wireframe below, the dots represent number of images retrieved from DB which should be swipeeable right or left. Imageview seems not to be swipeable.
IMAGE ADAPTER.JAVA
public class ImageAdapter extends RecyclerView.Adapter<ImageAdapter.ViewHolder> {
List<ImageList>imageLists;
Context context;
public ImageAdapter(List<ImageList> imageLists, Context context) {
this.imageLists = imageLists;
this.context = context;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v= LayoutInflater.from(parent.getContext()).inflate(R.layout.image_list,parent,false);
return new ViewHolder(v);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
ImageList imageList = imageLists.get(position);
holder.tvname.setText(imageList.getName());
holder.tv2name.setText(imageList.getName2());
String picture = imageList.getImageurl();
String picture2 = imageList.getImage2url();
String picture3 = imageList.getImage3url();
String[] imageUrls = {picture, picture2, picture3};
Picasso.get()
.load(picture)
.placeholder(R.drawable.placeholder)
.error(R.drawable.error)
.resize(0, 200)
.into(holder.img);
}
#Override
public int getItemCount() {
return imageLists.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
private TextView tvname, tv2name;
private ImageView img;
public ViewHolder(View itemView) {
super(itemView);
img=(ImageView)itemView.findViewById(R.id.image_view);
tvname=(TextView)itemView.findViewById(R.id.txt_name);
tvname=(TextView)itemView.findViewById(R.id.txt_name2);
}
}
}
XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="5dp"
app:cardElevation="3dp"
app:cardUseCompatPadding="true"
android:padding="2dp"
android:layout_margin="5dp">
<ImageView
android:id="#+id/image_view"
android:layout_width="match_parent"
android:layout_height="200dp"
android:contentDescription="ServerImg"
android:scaleType="fitXY" />
</androidx.cardview.widget.CardView>
<TextView
android:textSize="25sp"
android:textColor="#color/colorPrimaryDark"
android:textStyle="bold"
android:id="#+id/txt_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:textSize="25sp"
android:textColor="#color/colorPrimaryDark"
android:textStyle="bold"
android:id="#+id/txt_name2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
So I am displaying a recylerview with an Image and the name of a person. Below that is a seekbar, which the user can move to rate that person.
Now I want to store the ratings (=progress) of each seekbar in a list. However right now it only stores the rating of the last seekbar in the list.
So it stores only the progress of the last seekbar right now. I would like that when the user clicks the button that every current rating value of every seekbar is stored in a list.
Thank you very much.
That is is my layout_listitem:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="3dp"
android:id="#+id/parent_layoutREC"
android:orientation="horizontal"
android:layout_marginTop="13dp"
android:gravity="center">
<de.hdodenhof.circleimageview.CircleImageView
android:paddingTop="2dp"
android:paddingLeft="2dp"
android:layout_width="73dp"
android:layout_height="73dp"
android:id="#+id/kunde_imageREC"
android:src="#mipmap/ic_launcher"/>
<TextView
android:layout_marginLeft="40dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Canada"
android:id="#+id/kunde_nameREC"
android:textColor="#000"
android:textSize="19sp"
android:textStyle="bold"/>
</LinearLayout>
<SeekBar
android:layout_marginRight="35dp"
android:layout_marginLeft="35dp"
android:layout_marginTop="8dp"
android:id="#+id/seek_Bar"
android:max="10"
android:progress="5"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="0 1 2 3 4 5"
android:textSize="20sp"/>
That is my RecyclerViewAdapter class:
public class RecyclerViewAdap extends
RecyclerView.Adapter<RecyclerViewAdap.ViewHolder> {
private static final String TAG = "RecyclerViewAdap";
private ArrayList<String> mImageUrl = new ArrayList<>();
private ArrayList<String> mKundeNamen = new ArrayList<>();
public ArrayList<Integer> mBewertungen = new ArrayList<>();
private Context mContext;
public String bewertung;
private TextView progressSeekbar;
public RecyclerViewAdap(ArrayList<String> imageUrl, ArrayList<String> kundeNamen, Context context) {
mImageUrl = imageUrl;
mKundeNamen = kundeNamen;
mContext = context;
}
public class ViewHolder extends RecyclerView.ViewHolder {
CircleImageView imageView;
TextView kundeName;
LinearLayout parentLayout;
SeekBar seekBar1;
public ViewHolder(#NonNull View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.kunde_imageREC);
kundeName = itemView.findViewById(R.id.kunde_nameREC);
parentLayout = itemView.findViewById(R.id.parent_layoutREC);
seekBar1 = itemView.findViewById(R.id.seek_Bar);
progressSeekbar = itemView.findViewById(R.id.progress_seekbar);
seekBar1.setOnSeekBarChangeListener(seekBarChangeListener);
//int progress = seekBar1.getProgress();
//String.valueOf(Math.abs((long)progress)).charAt(0);
//progressSeekbar.setText("Bewertung: " +
// String.valueOf(Math.abs((long)progress)).charAt(0) + "." + String.valueOf(Math.abs((long)progress)).charAt(1));
}
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_listitem, parent, false);
ViewHolder holder = new ViewHolder(view);
Log.d(TAG, "onCreateViewHolder: ");
return holder;
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, final int position) {
Glide.with(mContext)
.load(mImageUrl.get(position))
.into(holder.imageView);
holder.kundeName.setText(mKundeNamen.get(position));
holder.parentLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(mContext, mKundeNamen.get(position),
Toast.LENGTH_SHORT).show();
}
});
holder.seekBar1.setTag(position);
}
#Override
public int getItemCount() {
return mImageUrl.size();
}
SeekBar.OnSeekBarChangeListener seekBarChangeListener = new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
// updated continuously as the user slides the thumb
progressSeekbar.setText(String.valueOf(progress));
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
// called when the user first touches the SeekBar
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
// called after the user finishes moving the SeekBar
if (seekBar.getTag().toString().equals("1")) {
mBewertungen.add(seekBar.getProgress());
if (mBewertungen.size() == 2) {
mBewertungen.remove(0);
Toast.makeText(mContext, "onProgressChanged: " + mBewertungen.toString(), Toast.LENGTH_SHORT).show();
}
}
if (seekBar.getTag().toString().equals("2")) {
mBewertungen.add(seekBar.getProgress());
if (mBewertungen.size() == 3) {
mBewertungen.remove(1);
Toast.makeText(mContext, "onProgressChanged: " + mBewertungen.toString(), Toast.LENGTH_SHORT).show();
}
}
}
};
}
That is the activity where the Recyclerview is being displayed:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".BewertungEsserActvity"
android:orientation="vertical"
app:layoutManager="LinearLayoutManager"
android:background="#drawable/gradient_background">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include
android:id="#+id/toolbar"
layout="#layout/toolbar" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="14dp"
android:text="Bewerte deine Gäste"
android:textColor="#color/colorDarkGrey"
android:textSize="30sp"
android:textStyle="bold" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/colorBlack"
android:layout_marginBottom="10dp"/>
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/recycler_view">
</androidx.recyclerview.widget.RecyclerView>
<ProgressBar
android:visibility="invisible"
android:id="#+id/progressbar_4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="13dp" />
<Button
android:layout_marginRight="66dp"
android:layout_marginLeft="66dp"
android:id="#+id/bewerten_Btn"
android:layout_width="match_parent"
android:layout_height="55dp"
android:layout_marginTop="10dp"
android:background="#drawable/btn_background_profil"
android:padding="10dp"
android:text="Bewerten"
android:textAllCaps="false"
android:textColor="#color/colorWhite"
android:textSize="16sp"
android:textStyle="normal" />
<View
android:layout_marginBottom="10dp"
android:layout_marginTop="34dp"
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="#color/colorBlack" />
</LinearLayout>
</ScrollView>
You can do it in multiple ways:
1- You can store seekbars value as a property in your model and update that property on OnSeekBarChangeListener and when the user hits the button get the data source from the adapter and then run a loop on it and call getter of that attribute.
2- You can store seekbars value as a List<> in your adapter and whenever the user hits the button get the list from the adapter.