public class HashTagSearchAdapter extends RecyclerView.Adapter<HashTagSearchAdapter.HashTagViewHolder> {
Context context;
List<TagSearchObject> tagResponses;
public HashTagSearchAdapter (Context context, List<TagSearchObject> tagResponses){
this.context = context;
this.tagResponses = tagResponses;
for (int i =0;i<tagResponses.size();i++){
Log.e("tagfor",tagResponses.get(i).toString());
}
Log.e("constructor",tagResponses.size()+"");
Log.e("constructor","here");
}
#Override
public HashTagViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.tag_search_list,parent,false);
Log.e("onCreate","here");
return new HashTagViewHolder(view);
}
#Override
public void onBindViewHolder(HashTagViewHolder holder, int position) {
TagSearchObject userTagResponse = tagResponses.get(position);
Log.e("tag",userTagResponse.toString());
holder.name.setText(userTagResponse.getName());
holder.count.setText(userTagResponse.getChallengeCount()+" public posts");
}
#Override
public int getItemCount() {
Log.e("getItemCount",this.tagResponses.size()+"");
return tagResponses.size();
}
class HashTagViewHolder extends RecyclerView.ViewHolder{
ImageView hashtag;
TextView name,count;
public HashTagViewHolder(View itemView) {
super(itemView);
name = (TextView) itemView.findViewById(R.id.name);
count = (TextView) itemView.findViewById(R.id.count);
}
}
}
here logcat is :
E/tagfor:TagSearchObject{id='1', name='hahah', challengeCount='1'}
E/constructor: 1
E/constructor: here
Because you have not added a layout manager and/or your recycler view is not visible. Since it's not visible, no attempt to draw is made and itemCount is never called because, well, there is nothing to draw.
set layout manager before setAdapter to the RecyclerView
LinearLayoutManager llm = new LinearLayoutManager(getApplicationContext());
HashTagSearchAdapter hashTagSearchAdapter = new
HashTagSearchAdapter(context,tagSearchObjects);
recyclerView.setLayoutManager(llm);
recyclerView.setAdapter(hashTagSearchAdapter);
Related
I want to hide checkbox of all lists if I clicked one list within activity using recyclerview.
But when I implement it, only the check box in the recyclerview list of the clicked position disappears. How can I hide the checkbox of the entire list?
Of course it is possible on the reyclerview adapter, but I want this function to be implemented in activity.
In activity, the adapter was declared:
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerViewAdapter = new RecyclerViewAdapter(this, patientList, selected_patientList);
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(recyclerViewAdapter);
And I wrote the code that hides the check box when click the recyclerview list in activity :
recyclerView.addOnItemTouchListener(new RecyclerItemClickListener(this, recyclerView, new RecyclerItemClickListener.OnItemClickListener() {
#Override
public void onItemClick(View view, int position) {
CheckBox checkBox = (CheckBox) view.findViewById(R.id.checkBox) ;
checkBox.setVisibility(View.GONE);
}
And my recyclerview adapter code is
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder> {
private Activity activity;
public ArrayList<PatientItem> patientList = new ArrayList<>();
public ArrayList<PatientItem> selected_patientList = new ArrayList<>();
Context mContext;
boolean checkboxIsVisible = true ;
public RecyclerViewAdapter(Context context, ArrayList<PatientItem> patientList, ArrayList<PatientItem> selected_patientList) {
this.mContext = context;
this.patientList = patientList;
this.selected_patientList = selected_patientList;
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView patientType;
TextView clinicID;
TextView patientName;
TextView dateFirst;
TextView dateFinal;
CheckBox checkBox ;
ConstraintLayout cl_listitem;
public MyViewHolder(View itemView) {
super(itemView);
patientType = (TextView) itemView.findViewById(R.id.typeViewItem);
clinicID = (TextView) itemView.findViewById(R.id.clinicIDItem);
patientName = (TextView) itemView.findViewById(R.id.patientNameItem);
dateFirst = (TextView) itemView.findViewById(R.id.dateFirstItem);
dateFinal = (TextView) itemView.findViewById(R.id.dateFinalItem);
checkBox = (CheckBox) itemView.findViewById(R.id.checkBox) ;
cl_listitem = (ConstraintLayout) itemView.findViewById(R.id.cl_listitem);
}
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.patient_list_item, parent, false);
final RecyclerViewAdapter.MyViewHolder vHolder = new RecyclerViewAdapter.MyViewHolder(itemView) ;
return vHolder;
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
PatientItem data = patientList.get(position);
holder.patientType.setText(data.getPatientType());
holder.clinicID.setText(data.getClinicID());
holder.patientName.setText(data.getPatientName());
holder.dateFirst.setText(data.getDateFirst());
holder.dateFinal.setText(data.getDateFinal());
holder.checkBox.setChecked(data.isDeleteBox());
holder.checkBox.setVisibility(checkboxIsVisible?View.VISIBLE:View.GONE);
if(data.getPatientType() == "P"){
holder.patientType.setText("");
holder.patientType.setBackground(ContextCompat.getDrawable(mContext, R.drawable.parkinson));
}
else if(data.getPatientType() == "ET"){
holder.patientType.setText("");
holder.patientType.setBackground(ContextCompat.getDrawable(mContext, R.drawable.essential_tremor));
}
else{
holder.patientType.setText("ㅡ");
}
}
#Override
public int getItemCount() {
return patientList.size();
}
public void clear() {
int size = patientList.size() ;
patientList.clear() ;
notifyItemRangeRemoved(0, size);
}
//어댑터 정비
public void refreshAdapter() {
this.selected_patientList = selected_patientList;
this.patientList = patientList;
this.notifyDataSetChanged();
}
}
I solved this problem like this.
I declared onBindViewHolder at RecyclerViewAdapter:
holder.checkBox.setVisibility(checkboxIsVisible?View.VISIBLE:View.GONE);
And, I declared visible() method in RecyclerViewAdapter :
public void visible(){
checkboxIsVisible = false ;
this.notifyDataSetChanged();
}
Than, I use visible method in activity :
recyclerView.addOnItemTouchListener(new RecyclerItemClickListener(this, recyclerView, new RecyclerItemClickListener.OnItemClickListener() {
#Override
public void onItemClick(View view, int position) {
recyclerViewAdapter.visible();
}
When I run this, when I click on one list, all the check boxes on the list disappear.
I have to set just four data from API in Recyclerview. The data from API are dynamic.If there are more than 4 data then the last one should hide itself and the recent data should appear at the top. I have researched but could not find the applicable solution.
My Adpater
public class EventRecyclerAdapter extends RecyclerView.Adapter<EventRecyclerAdapter.MyViewHolder> {
Context context;
private ArrayList<String> start;
private AdapterView.OnItemClickListener listener;
private ArrayList<String> desc;
private ArrayList<EventData>data;
public EventRecyclerAdapter(Context context, ArrayList<EventData> data) {
this.context = context;
this.data = data;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.content_events, parent, false);
return new EventRecyclerAdapter.MyViewHolder(itemView);
}
#Override
public void onBindViewHolder(EventRecyclerAdapter.MyViewHolder holder, int position) {
holder.start.setText(data.get(position).getStartDate());
holder.des.setText(Html.fromHtml(data.get(position).getName()));
}
#Override
public int getItemCount() {
return data.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
private TextView start, des, viewall,name;
private CardView cardView;
public MyViewHolder(View itemView) {
super(itemView);
start = (TextView) itemView.findViewById(R.id.startdate);
des = (TextView) itemView.findViewById(R.id.description);
viewall = (TextView) itemView.findViewById(R.id.viewall);
}
}
}
I have set the data as follows
private void initEventRecycler() {
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
eventR.setLayoutManager(mLayoutManager);
eventR.setItemAnimator(new DefaultItemAnimator());
eventR.setNestedScrollingEnabled(false);
eventRecyclerAdapter = new EventRecyclerAdapter(MainActivity.this, events);
eventR.setAdapter(eventRecyclerAdapter);
}
If you have shorted data in the array list you are getting in adapter then use this code to show only 5 items.
#Override
public int getItemCount() {
if (data!= null && data.size() < 5) {
return data.size();
} else {
return 5;
}
}
if you have a data list in random order then first short it from latest to old and send to adapter.
Hope it will work for you.
EventData[] mData = new EventData[4];
int writeIndex = 0;
public void add(Object o) {
ring[writeIndex % ring.length] = o;
writeIndex++;
}
// Return the size of your dataset (invoked by the layout manager)
#Override
public int getItemCount() {
return mData.lenght;
}
You can also Check ArrayCircularQueue.java
RecyclerView setAdapter do but calling method ChatAdapter, registerAdapterDataObserver, onAttachedToRecyclerView, getItemCount 5 calling
xml layout_height is match_parent
getItemCount is 5 not null, but onCreateViewHolder is not called.
MyActivity.java:
List<ChatMessage> messages = new ArrayList<ChatMessage>();
LinearLayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
reyclerview_message_list.setAdapter(new ChatAdapter(messages, R.layout.chat_send));
reyclerview_message_list.setLayoutManager(new LinearLayoutManager(this));
reyclerview_message_list.setItemAnimator(new DefaultItemAnimator());
reyclerview_message_list.addItemDecoration(new DividerItemDecoration(getApplicationContext(), layoutManager.getOrientation()));
Adapter.java:
public class ChatAdapter extends RecyclerView.Adapter<ChatAdapter.ViewHolder> {
private List<ChatMessage> itemList;
private int itemLayout;
public ChatAdapter(List<ChatMessage> items, int itemLayout){
this.itemList = items;
this.itemLayout = itemLayout;
}
#Override
public ChatAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(itemLayout, viewGroup, false);
return new ChatAdapter.ViewHolder(view);
}
#Override
public void onBindViewHolder(ChatAdapter.ViewHolder viewholder, int position) {
ChatMessage item = itemList.get(position);
viewholder.messageBody.setText(item.getMessageBody());
viewholder.messageTime.setText(item.getMessageTime());
viewholder.messageState.setText(item.getMessageState());
viewholder.itemView.setTag(item);
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView messageBody, messageTime, messageState;
public ViewHolder(View itemView) {
super(itemView);
messageBody = (TextView)itemView.findViewById(R.id.text_message_body);
messageTime= (TextView)itemView.findViewById(R.id.text_message_time);
messageState= (TextView)itemView.findViewById(R.id.text_message_state);
}
}
#Override
public int getItemCount() {
return itemList.size();
}
Why is onCreateViewHolder not called?
I just update your Adapter code and its working well.
public class ChatAdapter extends RecyclerView.Adapter<ChatAdapter.ViewHolder> {
private List<ChatMessage> itemList;
private int itemLayout;
private Context mContext;
public ChatAdapter(List<ChatMessage> items, int itemLayout, Context context){
this.itemList = items;
this.itemLayout = itemLayout;
this.mContext = context;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
View view = LayoutInflater.from(mContext).inflate(itemLayout, viewGroup, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(ViewHolder viewholder, int position) {
ChatMessage item = itemList.get(position);
viewholder.messageBody.setText(item.getMessageBody());
viewholder.messageTime.setText(item.getMessageTime());
viewholder.messageState.setText(item.getMessageState());
viewholder.itemView.setTag(item);
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView messageBody, messageTime, messageState;
public ViewHolder(View itemView) {
super(itemView);
messageBody = (TextView)itemView.findViewById(R.id.text_message_body);
messageTime= (TextView)itemView.findViewById(R.id.text_message_time);
messageState= (TextView)itemView.findViewById(R.id.text_message_state);
}
}
#Override
public int getItemCount() {
return itemList.size();
}
}
And you Activity or Fragment first get the RecyclerView and then set adapter like this:
RecyclerView reyclerview_message_list = findViewById(R.id.recyclerView);
reyclerview_message_list.setAdapter(new ChatAdapter(messages, R.layout.list_row, this)); // passed another parameter Context
reyclerview_message_list.setLayoutManager(new LinearLayoutManager(this));
reyclerview_message_list.setItemAnimator(new DefaultItemAnimator());
reyclerview_message_list.addItemDecoration(new DividerItemDecoration(getApplicationContext(), layoutManager.getOrientation()));
Hope this will solve your problem.
You didn't notify your adapter after setting the value. While you setting the adapter messages is empty. When you adding the value in messages using addAll(message_objects) those data are added in ArrayList but you have to notify the adapter using notifyDataSetChanged(). Declare your adapter in class level then create object of adapter before setting the adapter in recyclerview and notify the adapter after you added objects in ArrayList.
private ChatAdapter chatAdapter;//declare it globally(class level)
chatAdapter = new ChatAdapter(messages, R.layout.chat_send);
reyclerview_message_list.setAdapter(chatAdapter);
reyclerview_message_list.setLayoutManager(new LinearLayoutManager(this));
reyclerview_message_list.setItemAnimator(new DefaultItemAnimator());
reyclerview_message_list.addItemDecoration(new DividerItemDecoration(getApplicationContext(), layoutManager.getOrientation()));
messages.addAll(ChatMessage_Objects_you_getting_from_web_service);
chatAdapter.notifyDataSetChanged();
Prompt, faced with scrollToPosition speed. I RecyclerView 900 positions and need to quickly establish a position as scrollToPosition very slow, c ListView such problems were not setSelection working out perfectly. It is possible to accelerate the scrollToPosition?
public class EventAdapter extends RecyclerView.Adapter<EventAdapter.Holder> {
final static public String TAG = "EventAdapter";
Context context;
List<Event> items;
public EventAdapter(List<Event> items) {
this.items = items;
}
#Override
public EventAdapter.Holder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_list_event, parent, false);
context = parent.getContext();
return new EventAdapter.Holder(v);
}
#Override
public void onBindViewHolder(EventAdapter.Holder holder, int position) {
holder.setModel(context, items.get(position), position);
}
#Override
public int getItemCount() {
return items.size();
}
public class Holder extends RecyclerView.ViewHolder {
public View block;
Holder(View view) {
super(view);
block = (View) view.findViewById(R.id.cv);
}
public void setModel(Context context, Event model, int position) {
}
}
}
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
rv.setLayoutManager(linearLayoutManager);
rv.setHasFixedSize(true);
linearLayoutManager.scrollToPosition(index);
There are couple of ways to do it:
1) Try smoothScrollToPosition as : [will certainly work]
Toast.makeText(this, "Scroll...", Toast.LENGTH_SHORT).show();
myList.smoothScrollToPosition(0);
2) Also read about scrollToPositionWithOffset as this may help too
I having a recyclerview with GridLayoutManager, it populating the grid properly but not updating the data.
Below is my code,
public class AppsGridViewAdapter extends RecyclerView.Adapter<AppsGridViewAdapter.GridViewHolder>{
private ArrayList<App> appArrayList;
private Context context;
public AppsGridViewAdapter(Context context,ArrayList<App> appArrayList){
this.appArrayList = appArrayList;
this.context = context;
}
public static class GridViewHolder extends RecyclerView.ViewHolder{
CompoundAppButton compoundAppButton;
public GridViewHolder(CompoundAppButton itemView){
super(itemView);
compoundAppButton = (CompoundAppButton)itemView;
}
}
#Override
public GridViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
CompoundAppButton appButton = new CompoundAppButton(context,null);
return new GridViewHolder(appButton);
}
#Override
public void onBindViewHolder(GridViewHolder holder, int position) {
App app = appArrayList.get(position);
holder.compoundAppButton.updateButtonConfig(app);
}
#Override
public int getItemCount() {
return appArrayList.size();
}
}
Having valid values in the list, but not updating. Where CustomAppButton is a customview. Anyone can help me why it doesnt update.
UPDATE:
I calling recyclerview from my activity, below is the specific code
recyclerView = (RecyclerView)findViewById(R.id.recycler_view);
recyclerView.addItemDecoration(new MarginDecoration(this));
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new GridLayoutManager(this, 3));
AppsGridViewAdapter adapter = new AppsGridViewAdapter(this,appList);
recyclerView.setAdapter(adapter);
The activity contains only recyclerview.
updateButtonConfig is a function in CustomAppButton class, this is where view is updated. getting data to this function, have checked the log.
public void updateButtonConfig(App app){
Picasso.with(context)
.load(app.getAppIcon())
.placeholder(R.drawable.app_icon)
.into(appIcon);
appName.setText(app.getAppName());
category.setText(app.getAppCategory());
price.setText(app.getAppPrice());
appPackage = app.getAppPackage();
}