So I have a RecyclerView inside another RecyclerView. And in my onBind() method in recyclerView1 i am trying to set the data for recyclerView2 , the fact is that recyclerView1 has a FirebaseRecyclerAdapter and recyclerView2 has a custom RecyclerAdapter and the onCreate() method in recyclerView2's adapter is never called , so the data set is good but it doesn't create my recyclerView2 in the interface this recycler is empty.
I put a breakpoint on the OrderViewHolder onCreateViewHolder and it just skips this .
Here is the code from the recyclerView2's adapter onCreate and onBind methods :
adapterReceivedCommands = new FirebaseRecyclerAdapter<Comanda, ReceivedCommandsViewHolder>(optionsReceivedCommands) {
#NonNull
#Override
public ReceivedCommandsViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_command,parent,false);
layoutManagerOrder = new GridLayoutManager(parent.getContext(),3);
orderRecycler = (RecyclerView) itemView.findViewById(R.id.order_recycler);
orderRecycler.setLayoutManager(layoutManagerOrder);
fragmentItem = (LinearLayout)itemView.findViewById(R.id.item_layout);
return new ReceivedCommandsViewHolder(itemView);
}
#Override
protected void onBindViewHolder(#NonNull final ReceivedCommandsViewHolder receivedCommandsViewHolder, int i, #NonNull final Comanda comanda) {
receivedCommandsViewHolder.tableNmb.setText(comanda.getmasa());
orderModelArrayList = comanda.getcomanda();
mAdaptor = new RecyclerViewAdapter(this, orderModelArrayList);
receivedCommandsViewHolder.recyclerView.setAdapter(mAdaptor);
}
};
adapterReceivedCommands.startListening();
receivedCommandsRecycler.setAdapter(adapterReceivedCommands);
return view;
}
And here is the code from the custom recyclerView2's adapter :
public class RecyclerViewAdapter extends RecyclerView.Adapter<OrderViewHolder>{
private ArrayList<OrderModel> orderModels = new ArrayList<OrderModel>();
private FirebaseRecyclerAdapter<Comanda, ReceivedCommandsViewHolder> context;
public RecyclerViewAdapter(FirebaseRecyclerAdapter<Comanda, ReceivedCommandsViewHolder> context, ArrayList<OrderModel> orderModels) {
this.orderModels = orderModels;
this.context = context;
}
#NonNull
#Override
public OrderViewHolder onCreateViewHolder(#NonNull ViewGroup parent, final int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_order,parent,false);
OrderViewHolder holder = new OrderViewHolder(view);
return holder;
}
#Override
public void onBindViewHolder(#NonNull OrderViewHolder holder, int position) {
OrderModel curentModel = orderModels.get(position);
holder.cantitateTxt.setText(curentModel.getCantitate().toString());
holder.comandaTxt.setText(curentModel.getDenumireProdus());
}
#Override
public int getItemCount() {
return 0;
}
Your item count is zero:
#Override
public int getItemCount() {
return 0;
}
So RecyclerView doesn't see any items to inflate. You should fix it to return the size of your list:
#Override
public int getItemCount() {
return orderModels.size();
}
Related
I'm trying to get a View position through his view hierarchy.
When I have a RecyclerView, for get the current view position I do:
if(view.getParent() instanceof RecyclerView){
RecyclerView rv = (RecyclerView)view.getParent();
if(rv.getLayoutManager()!=null) {
position = rv.getLayoutManager().getPosition(view);
}
}
But when a ListView or GridView appears, I'm not able to get a Layout Manager to get the position of the view. The idea is to make it in a generic way, without knowing the Custom Adapter the view has.
I have tried to get the position through the ListView and GridView adapter but I dont see any method that do that.
Someone can help me?
Thank you a lot!
Create interface for get position for outside adapter class
here is adapter code:
public class ReportMediaAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private ArrayList<File> reportMediaList = new ArrayList<>();
private Context context;
private int selectedPos = -1;
private OnRemoveImage onRemoveImage;
public ReportMediaAdapter(Context context, ArrayList<File> reportMediaList, OnRemoveImage onRemoveImage) {
this.reportMediaList = reportMediaList;
this.onRemoveImage = onRemoveImage;
this.context = context;
}
**public interface OnRemoveImage {
void onRemove(int pos);
}**
public class MyViewHolder extends RecyclerView.ViewHolder {
private ImageView iv_image, iv_remove;
public MyViewHolder(View itemView) {
super(itemView);
iv_image = itemView.findViewById(R.id.iv_image);
iv_remove = itemView.findViewById(R.id.iv_remove);
}
}
#Override
public int getItemCount() {
return reportMediaList.size()-1;
}
#NonNull
#Override
public RecyclerView.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_media, parent, false);
return new MyViewHolder(itemView);
}
#Override
public void onBindViewHolder(#NonNull RecyclerView.ViewHolder holder, int position) {
try {
MyViewHolder menuItemHolder = (MyViewHolder) holder;
CommonMethods.loadImage(context, reportMediaList.get(position), menuItemHolder.iv_image);
menuItemHolder.iv_remove.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
**onRemoveImage.onRemove(position);**
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
}
Here is activity code:
#Override
public void onRemove(int pos) {
// use adapter position
}
I have found a solution:
if(view.getParent() instanceof ViewGroup) {
position = ((ViewGroup)view.getParent()).indexOfChild(view);
}
I'm creating an app with firebase as a backend. I'm using same adapter for different activity, now it is working good but positions were mismatched for example: The output of position 0 shows in position 1 and for position 1 shows in position 2 and so on.. How to solve this problem, The problem with the positions of the output.
Adapter:
public class FrontlistAdapter extends FirebaseRecyclerAdapter<Gamedata, FrontlistAdapter.ViewHolder> {
private static final String TAG = "GameAdapter";
Context mContext;
int positions;
public FrontlistAdapter(#NonNull FirebaseRecyclerOptions<Gamedata> options, Context context) {
super(options);
mContext = context;
}
#Override
protected void onBindViewHolder(#NonNull ViewHolder holder, int position, #NonNull Gamedata model) {
holder.name.setText(model.getName());
holder.address.setText(model.getAddress());
//Picasso.get().load(model.getFrontcover()).into(holder.Frontcover);
Glide.with(mContext).load(model.getFrontcover()).into(holder.Frontcover);
positions = position; //<-- here I'm having positions but output shows different for each positions
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View v = layoutInflater.inflate(R.layout.gamerow,parent,false);
ViewHolder viewHolder = new ViewHolder(v);
return viewHolder;
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
ImageView Frontcover;
TextView name;
TextView address;
View v;
public ViewHolder(#NonNull View itemView) {
super(itemView);
Frontcover = (ImageView)itemView.findViewById(R.id.frontcover);
name = (TextView)itemView.findViewById(R.id.frontname);
address=(TextView)itemView.findViewById(R.id.frontaddress);
v = itemView;
itemView.setClickable(true);
itemView.setOnClickListener((View.OnClickListener) this);
}
#Override
public void onClick(View v) {
String className = mContext.getClass().getSimpleName();
Intent intent = null;
switch (className) {
case "GameActivity":
intent = new Intent(mContext, Center_details.class);
intent.putExtra("User", getRef(positions).getKey());
break;
case "KidsActivity":
intent = new Intent(mContext,AllCenterDetails.class);
intent.putExtra("Kids",getRef(positions).getKey());
break;
}
mContext.startActivity(intent);
}
}
}
first what you have to do is create the interface like this
public interface ClickListner {
void onClick(View view, int position);
}
then in your activity create object of the interface like this and pass to the adapter along with your other data
ClickListner listener = ClickListner();
FrontlistAdapter customAdapter = new
FrontlistAdapter(AcceptedOrdersActivity.this,listener);
riderView.setAdapter(customAdapter);
copy this outside onCreate and intent in this
private ClickListner ClickListner() {
ClickListner listener = new ClickListner() {
#Override
public void onClick(View view, int position) {
int tag = (int) view.getTag();
if(tag == 0)
{
//Intent here
}
}
};
return listener;
}
now your adapter should be this one
public class FrontlistAdapter extends
RecyclerView.Adapter<FrontlistAdapter .DataObjectHolder>{
ClickListner listenr;
Context context;
public FrontlistAdapter (Context context,ClickListner listenr) {
this.context = context;
this.listenr = listenr;
}
public static class DataObjectHolder extends RecyclerView.ViewHolder {
Context context;
public DataObjectHolder(View itemView,ClickListner listenr) {
super(itemView);
this.listenr = listenr;
Frontcover = (ImageView)itemView.findViewById(R.id.frontcover);
name = (TextView)itemView.findViewById(R.id.frontname);
address=(TextView)itemView.findViewById(R.id.frontaddress);
v = itemView;
setOnClickListeners();
}
private void setOnClickListeners() {
`v.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
v.setTag(0);
listenr.onClick(v, getAdapterPosition());
}
});
}
}
#NonNull
#Override
public FrontlistAdapter.DataObjectHolder onCreateViewHolder(#NonNull ViewGroup parent,
int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.cell_orderdetail_list, parent, false);
FrontlistAdapter.DataObjectHolder dataObjectHolder = new
FrontlistAdapter.DataObjectHolder(view,listenr);
return dataObjectHolder;
}
#Override
public void onBindViewHolder(#NonNull FrontlistAdapter.DataObjectHolder holder, int
position) {
}
#Override
public int getItemCount() {
return options.size;
}
}
positions = position;
This is your problem. You're setting one global position for everything in the adapter.
Everywhere you are currently using position, you should instead be calling getBindingAdapterPosition() on the ViewHolder.
I'm trying to figure out how to do this in the most simple way possible. Setting the recycler view to wrap the content of course, adjusts the recycler to the biggest item. Setting it to match_parent adjusts all the items to be the maximum at all times.
Setting the maximum width in the Adapter makes all the items be that size no matter what. Dumbfounded at the moment.
My Adapter class
public class MessageAdapter extends RecyclerView.Adapter<MessageAdapter.MessageViewHolder> {
private ArrayList<Message> messageList;
private Context context;
private MessageSelectListener messageSelectListener;
public MessageAdapter(Context context, ArrayList<Message> messageList){
this.messageList = messageList;
this.context = context;
}
//Create an interface
public interface MessageSelectListener{
void onMessageClick(Message message, int position);
}
public void setMessageClickListener(MessageSelectListener messageSelectListener){
this.messageSelectListener = messageSelectListener;
}
#NonNull
#Override
public MessageViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.message_list, parent, false);
RecyclerView.LayoutParams lp = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
view.setLayoutParams(lp);
//View view = new TextView(context);
//View rootView = LayoutInflater.from(context).inflate(R.layout.order_list, parent, false);
return new MessageViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull MessageViewHolder holder, final int position) {
final Message message = messageList.get(position);
holder.text.setText(message.getMessageText());
holder.text.setBackgroundResource(R.drawable.rounded_corner_white_noborder);
// passing Order and Position as parameter to interface method,
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
messageSelectListener.onMessageClick(message, position);
}
});
}
#Override
public int getItemCount() {
return messageList == null ? 0 : messageList.size();
}
public class MessageViewHolder extends RecyclerView.ViewHolder{
public TextView text;
public Message message;
public MessageViewHolder(#NonNull View itemView) {
super(itemView);
text = (TextView) itemView;
}
}
}
The Solution was to change the layout parameters in the onCreateViewHolder() method to WRAP_CONTENT
#NonNull
#Override
public MessageViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.message_list, parent, false);
RecyclerView.LayoutParams lp = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.**WRAP_CONTENT**, ViewGroup.LayoutParams.WRAP_CONTENT);
view.setLayoutParams(lp);
//View view = new TextView(context);
//View rootView = LayoutInflater.from(context).inflate(R.layout.order_list, parent, false);
return new MessageViewHolder(view);
}
RecyclerView items not visible
I am using a string array name item test and pass it to String array list, I used the same idea before and was working correctly
listItemsTest = new ArrayList<>(Arrays.asList(itemsTest));
recyclerView = findViewById(R.id.RecycleViewTest);
recycleViewAdapterTest = new RecycleViewAdapterTest(this,listItemsTest);
recyclerView.setAdapter(recycleViewAdapterTest);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
My Adapter
public class RecycleViewAdapterTest extends
RecyclerView.Adapter<RecycleViewAdapterTest.MyViewHolder> {
private ArrayList<String> ListTest;
Context context;
public RecycleViewAdapterTest(Context context, ArrayList<String> listTest) {
this.ListTest = listTest;
this.context = context;
}
#NonNull
#Override
public RecycleViewAdapterTest.MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.test_list,parent,false);
MyViewHolder holder = new MyViewHolder(view);
return holder;
}
#Override
public void onBindViewHolder(#NonNull RecycleViewAdapterTest.MyViewHolder holder, int position) {
}
#Override
public int getItemCount() {
return ListTest.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder {
LinearLayout linearLayout;
TextView textView;
public MyViewHolder(#NonNull View itemView) {
super(itemView);
linearLayout = itemView.findViewById(R.id.LLTest);
textView = itemView.findViewById(R.id.textViewTest);
}
}
#Override
public void onBindViewHolder(#NonNull RecycleViewAdapterTest.MyViewHolder holder, int position) {
String value = this.ListTest.get(position);
holder.textView.setText(value);// Try to make the textView public
}
because you need to set list value to viewholder's textview in OnBindViewHolder. :(
I am using a RecyclerView to view many Progress Bars as a slide show and an adapter.
This my adapter class
public class RecAdapter extends RecyclerView.Adapter<RecAdapter.ViewHolder> {
private static final String TAG = "RecAdapter";
private ArrayList progressbars = new ArrayList<>();
private Context pcontex;
public RecAdapter(Context pcontex ,ArrayList progressbars) {
this.progressbars = progressbars;
this.pcontex = pcontex;
}
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.progress,viewGroup,false);
ViewHolder holder = new ViewHolder(view);
return holder;
}
#Override
public void onBindViewHolder(#NonNull ViewHolder viewHolder, int i) {
Log.d(TAG, "onBindViewHolder: called");
}
#Override
public int getItemCount() {
return progressbars.size() ;
}
public class ViewHolder extends RecyclerView.ViewHolder {
ProgressBar b ;
ConstraintLayout parent;
public ViewHolder(#NonNull View itemView) {
super(itemView);
b = itemView.findViewById(R.id.progressBar);
parent= itemView.findViewById(R.id.parent);
}
}
}
My MainActivity code-
Prog = new ArrayList<>(); b2 = findViewById(R.id.progressBar) ;
b = findViewById(R.id.progressBar);
probitmap();
private void probitmap(){
Log.d(TAG, "probitmap: preparing bit maps");
Prog.add(b);
Prog.add(b2);
Recycle();// this method is out of on create
}
private void Recycle(){
Log.d(TAG, "Recycle: init recycleview");
recyclerView =findViewById(R.id.Rec);
RecAdapter Adapter = new RecAdapter(this,Prog);
recyclerView.setAdapter(Adapter);
LinearLayoutManager layoutManager
= new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
recyclerView.setLayoutManager(layoutManager);
}
The code is working perfectly but I need to retrieve every progress bar that is in the recycle view and set progress for each one.
I tried multiple ways but it doesn't work. Here is what I tried-
for (int i = 0; i < recyclerView.getChildCount(); i++) {
RecAdapter.ViewHolder holder = (RecAdapter.ViewHolder) recyclerView.findViewHolderForAdapterPosition(i);
holder.b.setProgress(50);
}
Use OnBindViewHolder to manage each "row" generated by the recycler.
#Override
public void onBindViewHolder(#NonNull ViewHolder viewHolder, int i) {
viewHolder.b.setProgress(progressbar.get(i)); //i is the position of the recycler
viewHolder.b.setListener....
}