Can someone please tell what's the error in that specific line.
Here is the error message showing.
package com.example.souravkumar.sqaurewallpapers;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.squareup.picasso.Picasso;
/**
* Created by Sourav Kumar on 11/3/2017.
*/
public class popular extends AppCompatActivity {
private RecyclerView recyclerView;
private DatabaseReference myRef;
#Override
public void onCreate(#Nullable Bundle savedInstanceState, #Nullable PersistableBundle persistentState) {
super.onCreate(savedInstanceState, persistentState);
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
myRef = FirebaseDatabase.getInstance().getReference();
FirebaseRecyclerAdapter<image_details, BlogViewHolder> adapter = new FirebaseRecyclerAdapter<image_details, BlogViewHolder>(
image_details.class,
R.layout.individual_row,
myRef) {
#Override
protected void onBindViewHolder(BlogViewHolder holder, int position, image_details model) {
holder.setDate(model.getDate());
holder.setUrl(model.getUrl());
}
#Override
public BlogViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return null;
}
};
recyclerView.setAdapter(adapter);
}
public static class BlogViewHolder extends RecyclerView.ViewHolder {
TextView textView;
ImageView imageView;
public BlogViewHolder(View itemView) {
super(itemView);
textView = (TextView)itemView.findViewById(R.id.date);
imageView = (ImageView)itemView.findViewById(R.id.imageView);
}
public void setDate(Long date) {
textView.setText(date.toString());
}
public void setUrl(String url) {
Picasso.with(itemView.getContext())
.load(url)
.resize(50, 50)
.centerCrop()
.into(imageView);
}
}
}
Link to the whole code
The latest version (3.x) of FirebaseUI implements a different method of initializing a FirebaseRecyclerAdapter than previous versions. From the using the FirebaseRecyclerAdapter documentation:
First, configure the adapter by building FirebaseRecyclerOptions. In
this case we will continue with our chat example:
FirebaseRecyclerOptions<Chat> options =
new FirebaseRecyclerOptions.Builder<Chat>()
.setQuery(query, Chat.class)
.build();
Next create the FirebaseRecyclerAdapter object. You should already
have a ViewHolder subclass for displaying each item.
So, for your example, you'll need to do something similar to:
FirebaseRecyclerOptions<image_details> options =
new FirebaseRecyclerOptions.Builder<image_details>()
.setQuery(myRef, image_details.class)
.build();
FirebaseRecyclerAdapter adapter = new FirebaseRecyclerAdapter<image_details, BlogViewHolder>(options) {
#Override
public BlogViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.individual_row, parent, false);
return new BlogViewHolder(view);
}
#Override
protected void onBindViewHolder(BlogViewHolder holder, int position, image_details model) {
// Bind the image_details object to the BlogViewHolder
// ...
}
};
Finally, the FirebaseRecyclerAdapter uses an event listener to monitor changes to the Firebase query. To begin listening for data, call adapter.startListening() and adapter.stopListening() to stop the listener. It's recommended to call these in onStart() and onStop() respectively. Further details on this are available in the FirebaseRecyclerAdapter lifecycle documentation.
You missed a method to start the activity.
Add this in onStart() method:
firebaseRecyclerAdapter.startListening();
Hello your error is due to a missing argument in these () that are found beside this
<image_details, BlogViewHolder>
so you missed one more argument of your BlogViewHolder class so I will add the piece of code and refer your missing argument by a comment in this :
FirebaseRecyclerAdapter<image_details, BlogViewHolder> adapter = new FirebaseRecyclerAdapter<image_details, BlogViewHolder>(
image_details.class,
R.layout.individual_row,
BlogViewHolder.class //see this is missing so you should add it
myRef) {
This is the right way :
FirebaseRecyclerOptions<image_details> options =
new FirebaseRecyclerOptions.Builder<image_details>()
.setQuery(myRef, image_details.class)
.build();
FirebaseRecyclerAdapter adapter = new FirebaseRecyclerAdapter<image_details, BlogViewHolder>(options) {
#Override
public BlogViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.individual_row, parent, false);
return new BlogViewHolder(view);
}
#Override
protected void onBindViewHolder(BlogViewHolder holder, int position, image_details model) {
// Bind the image_details object to the BlogViewHolder
// ...
}
};
I had the same error and this worked very well for me. Hope it helps someone
//loads data into recycler view onstart up
#Override
protected void onStart() {
super.onStart();
FirebaseRecyclerOptions<Data> options =
new FirebaseRecyclerOptions.Builder<Data>()
.setQuery(dbReference, Data.class)
.build();
FirebaseRecyclerAdapter firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Data, DataViewHolder>(options) {
#Override
public DataViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.row, parent, false);
return new DataViewHolder(view);
}
#Override
protected void onBindViewHolder(#NonNull DataViewHolder holder, int position, #NonNull Data model) {
// Bind the image_details object to the BlogViewHolder
// ...
holder.setDetails(getApplicationContext(), model.getName(), model.getDepartment(), model.getDescription());
}
};
firebaseRecyclerAdapter.startListening();
recyclerView.setAdapter(firebaseRecyclerAdapter);
}
Related
I am a beginner at android development. I tried to display data on Recycle View using MVVM. When I opened the fragment the first time it is empty, the second time it showed data. Adapter not called the first time. How to fix this.
Fragment class
public class fragmentEmployee extends Fragment {
private FragmentEmployeeBinding binding;
private ViewGroup viewGroup;
private View view;
private RecyclerView mRecycleView;
private UserAdapter mAdapter;
private EmployeeRegisterViewModel viewModel;
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
binding = DataBindingUtil.inflate(inflater, R.layout.fragment_employee, viewGroup, false);
view = binding.getRoot();
return view;
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Toast.makeText(getContext(), "onViewCreated", Toast.LENGTH_SHORT).show();
mRecycleView = binding.recyleViewEmployee;
viewModel = new ViewModelProvider(this).get(EmployeeRegisterViewModel.class);
viewModel.init();
initRecycleView();
viewModel.getEmployee().observe(getViewLifecycleOwner(), new Observer<List<employeeModel>>() {
#Override
public void onChanged(List<employeeModel> employeeModels) {
mAdapter.notifyDataSetChanged();
}
});
}
private void initRecycleView() {
mAdapter = new UserAdapter(getContext(), viewModel.getEmployee().getValue());
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext());
mRecycleView.setLayoutManager(layoutManager);
mRecycleView.setAdapter(mAdapter);
}
}
UserAdapter class
public class UserAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
public List<employeeModel> employeeModelList= new ArrayList<>();
public Context context1;
public UserAdapter(Context context,List<employeeModel> employeeModels){
employeeModelList = employeeModels;
context1 = context;
}
#NonNull
#Override
public RecyclerView.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_listitem,parent,false);
ViewHolder viewHolder=new ViewHolder(view);
return viewHolder;
}
#Override
public void onBindViewHolder(#NonNull RecyclerView.ViewHolder holder, int position) {
(ViewHolder)holder).username.setText(employeeModelList.get(position).getName());
}
#Override
public int getItemCount() {
return employeeModelList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
TextView username;
public ViewHolder(#NonNull View itemView) {
super(itemView);
username=itemView.findViewById(R.id.userText);
}
}
}
Try reordering like this.
Remove
mAdapter = new UserAdapter(getContext(), viewModel.getEmployee().getValue());
from initRecycleView
mAdapter = new UserAdapter(getContext(), viewModel.getEmployee().getValue());
viewModel.getEmployee().observe(getViewLifecycleOwner(), new Observer<List<employeeModel>>() {
#Override
public void onChanged(List<employeeModel> employeeModels) {
mAdapter.notifyDataSetChanged();
}
});
initRecycleView();
mAdapter.notifyDataSetChanged();
Above method will work if the list declared inside adapter has any change. So, calling above method without changing adapter's list is useless.
Add a method to UserAdapter class to update list data. Like:
public void updateData(List<employeeModel> updatedList){
employeeModelList.clear();
employeeModelList.addAll(updatedList);
notifyDataSetChanged();
}
And call this method inside observer. Like:
viewModel.getEmployee().observe(getViewLifecycleOwner(), new
Observer<List<employeeModel>>() {
#Override
public void onChanged(List<employeeModel> employeeModels) {
mAdapter.updateData(employeeModels);
}
});
where are you get your network data?
If your get in here -> viewModel.init();
I think it just timing problem.
Just change code sequence
initRecycleView();
viewModel.getEmployee().observe(getViewLifecycleOwner(), new Observer<List<employeeModel>>() {
#Override
public void onChanged(List<employeeModel> employeeModels) {
mAdapter.notifyDataSetChanged();
}
});
viewModel.init();
I just implemented the new architecture of Android components, Room, ViewModel, LiveData, Retrofit and RecyclerView, so all right, the problem is that the list I show in recyclerview is exponentially large, since it is a prototype for a chat , and the application could freeze a little.
Searching the internet I found documentation about PagedList Adapter, and it may be a possible solution, the problem is that in the mainActivity, when I call the observer, where I should put adapter.submitList(message), (inside the onchange function), I do not shows the data
MainActivity.class
public class MainActivity extends AppCompatActivity {
public static final int NEW_WORD_ACTIVITY_REQUEST_CODE = 1;
private MenViewModel mWordViewModel;
MensajesAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView recyclerView = findViewById(R.id.recyclerview);
adapter = new MensajesAdapter( getApplicationContext());
mWordViewModel =
ViewModelProviders.of(this).get(MenViewModel.class);
recyclerView.setHasFixedSize(true);
mWordViewModel.usersList.observe(this, mensajes -> {
adapter.submitList(mensajes);
});
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);
The strange thing is that if I modify the submitList code by adapter.setwords(parameter) the list if it is shown but of course, it would be using notifydatasetchange() and it would not be efficient
PagedListAdapter-->MensajesAdapter.class
import android.arch.paging.PagedListAdapter;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.util.DiffUtil;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.List;
public class MensajesAdapter extends PagedListAdapter<Mensajes,MensajesAdapter.MensajesViewHolder> {
private Context mCtx;
private List<Mensajes> heroList;
MensajesAdapter( Context context) {
super(DIFF_CALLBACK);
this.mCtx=context;
}
**void setWords(List<Mensajes> words){
heroList = words;
notifyDataSetChanged();
}**
#NonNull
#Override
public MensajesViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mCtx).inflate(R.layout.recyclerview_layout, parent, false);
return new MensajesViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull MensajesViewHolder mensajesViewHolder, int i) {
Mensajes men =getItem(i);
if(men!=null) {
mensajesViewHolder.textView.setText(men.getMensaje());
}else{
mensajesViewHolder.clear();
}
}
#Override
public int getItemCount() {
if (heroList != null)
return heroList.size();
else return 0;
}
public static DiffUtil.ItemCallback<Mensajes> DIFF_CALLBACK = new DiffUtil.ItemCallback<Mensajes>() {
#Override
public boolean areItemsTheSame(#NonNull Mensajes oldItem, #NonNull Mensajes newItem) {
Log.e("areITems",""+(oldItem.getId() == newItem.getId()));
return oldItem.getId() == newItem.getId();
}
#Override
public boolean areContentsTheSame(#NonNull Mensajes oldItem, #NonNull Mensajes newItem) {
Log.e("areITems",""+(oldItem.getMensaje().equals(newItem.getMensaje())));
return oldItem.getMensaje().equals(newItem.getMensaje());
}
};
class MensajesViewHolder extends RecyclerView.ViewHolder {
TextView textView;
public MensajesViewHolder(View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.mensaje);
}
void clear()
{
textView.invalidate();
}
}
}
ViewModel-->MenViewModel.class
public class MenViewModel extends AndroidViewModel {
public MensajesDao countryDao;
private MenRepository mRepository;
// Using LiveData and caching what getAlphabetizedWords returns has several benefits:
// - We can put an observer on the data (instead of polling for changes) and only update the
// the UI when the data actually changes.
// - Repository is completely separated from the UI through the ViewModel.
public LiveData<PagedList<Mensajes>> mAllWords;
public LivePagedListBuilder livePlyerList;
public final LiveData<PagedList<Mensajes>> usersList;
public MenViewModel(Application application) {
super(application);
mRepository = new MenRepository(application);
mAllWords = mRepository.getAllWords();
MenRoomDatabase countryDatabase = MenRoomDatabase.getDatabase(getApplication());
countryDao = countryDatabase.mensajeDao();
//mRepository.recargar();
usersList =new LivePagedListBuilder<>(
countryDao.getAllMensajes(), /* page size */ 20).build();
livePlyerList=new LivePagedListBuilder<>(countryDatabase.mensajeDao().getAllMensajes(),new PagedList.Config.Builder()
.setPageSize(20)
.setPrefetchDistance(10)
.setPageSize(20)
.build());
}
LiveData<PagedList<Mensajes>> getAllWords() { return mAllWords; }
public void insert(Mensajes mensaje) { mRepository.insert(mensaje); }
Would someone know to tell me what is wrong? What do I lack or do I have?
Make sure countryDao.getAllMensajes() is wrapped with DataSource.Factory<Int, Mensajes> and in you Adapter you should not be managing the list manually since you are using PagedListAdapter.
In your #Override public int getItemCount() {} do:
#Override
public int getItemCount() {
return super.getItemCount()
}
I am having a problem with my GridView wherein the images loads in different places when scrolling. I know this has something to do with recycling but as I am new to Android, I have no idea about this.
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
final Listing listing = getItem(position);
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.listing_items, parent, false);
}
final ImageView imageView = (ImageView) convertView.findViewById(R.id.imageView);
FirebaseStorage.getInstance().getReference().child(listing.authorUid).child(listing.adId).child(listing.images.get(0)).getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
#Override
public void onSuccess(Uri uri) {
try {
GlideApp.with(getContext())
.load(new URL(uri.toString()))
.into(imageView);
} catch(Exception e) {
e.printStackTrace();
}
}
});
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getContext(), ItemInfoActivity.class);
intent.putExtra("title", listing.title);
intent.putExtra("location", listing.city);
intent.putExtra("price", listing.price);
intent.putExtra("desc", listing.description);
intent.putExtra("category", listing.category);
intent.putExtra("author", listing.authorUid);
intent.putExtra("adId", listing.adId);
intent.putExtra("authorId", listing.authorUid);
startActivity(intent);
}
});
TextView price = (TextView)convertView.findViewById(R.id.price_txt);
TextView title = (TextView)convertView.findViewById(R.id.title_txt);
title.setText(listing.title);
price.setText(listing.price +"DH");
return convertView;
}
Please advise on what to do.
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
public class CustomAdapter extends RecyclerView.Adapter {
ArrayList personNames;
ArrayList personImages;
Context context;
public CustomAdapter(Context context, ArrayList personNames, ArrayList personImages) {
this.context = context;
this.personNames = personNames;
this.personImages = personImages;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// infalte the item Layout
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.rowlayout, parent, false);
// set the view's size, margins, paddings and layout parameters
MyViewHolder vh = new MyViewHolder(v); // pass the view to View Holder
return vh;
}
#Override
public void onBindViewHolder(MyViewHolder holder, final int position) {
// set the data in items
holder.name.setText(personNames.get(position));
holder.image.setImageResource(personImages.get(position));
// implement setOnClickListener event on item view.
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// open another activity on item click
Intent intent = new Intent(context, SecondActivity.class);
intent.putExtra("image", personImages.get(position)); // put image data in Intent
context.startActivity(intent); // start Intent
}
});
}
#Override
public int getItemCount() {
return personNames.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
// init the item view's
TextView name;
ImageView image;
public MyViewHolder(View itemView) {
super(itemView);
// get the reference of item view's
name = (TextView) itemView.findViewById(R.id.name);
image = (ImageView) itemView.findViewById(R.id.image);
}
}
}
Use this adapter structure.
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
public class CustomAdapter extends RecyclerView.Adapter {
ArrayList personNames;
ArrayList personImages;
Context context;
public CustomAdapter(Context context, ArrayList personNames, ArrayList personImages) {
this.context = context;
this.personNames = personNames;
this.personImages = personImages;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// infalte the item Layout
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.rowlayout, parent, false);
// set the view's size, margins, paddings and layout parameters
MyViewHolder vh = new MyViewHolder(v); // pass the view to View Holder
return vh;
}
#Override
public void onBindViewHolder(MyViewHolder holder, final int position) {
// set the data in items
holder.name.setText(personNames.get(position));
holder.image.setImageResource(personImages.get(position));
// add your firebase code here
// implement setOnClickListener event on item view.
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// open another activity on item click
Intent intent = new Intent(context, SecondActivity.class);
intent.putExtra("image", personImages.get(position)); // put image data in Intent
context.startActivity(intent); // start Intent
}
});
}
#Override
public int getItemCount() {
return personNames.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
// init the item view's
TextView name;
ImageView image;
public MyViewHolder(View itemView) {
super(itemView);
// get the reference of item view's
name = (TextView) itemView.findViewById(R.id.name);
image = (ImageView) itemView.findViewById(R.id.image);
}
}
}
Here is the updated answer of above answer.
if you are using recyclerview you can not override getView() method.
i would suggest you to use recyclerview.
when I dynamic remove an item, or when I refresh the adapter(swipe to fresh) i get these erros:
recyclerview onclicklistener java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
Really can't figure out why this is happening, Really Appreciate any feed back.
MainActivity:
adapter.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(View view, int position) {
GroupModel selectedList = mGroupModels.get(position);
if (selectedList != null) {
Log.d(TAG, "setAdpterListner > view.getId: " + view.getId() +
" | P: " + position +
" | data ID: " + selectedList.getGroupName()
//" | viewID: " + viewID
);
Intent intent = new Intent(this, DetailsActivity.class);
String listId = selectedList.getGroupID();
String listName = selectedList.getGroupName();
intent.putExtra(Constant.KEY_LIST_ID, listId);
intent.putExtra(Constant.KEY_LIST_NAME, listName);
startActivity(intent);
}
}
});
Adapter:
public class GroupListAdapter extends RecyclerView.Adapter<GroupListAdapter.StatusViewHolder> {
Context context;
private List<GroupModel> mGroupModels;
private static OnItemClickListener listener;
public GroupListAdapter(Context context, List<GroupModel> groupList) {
this.context = context;
this.mGroupModels = groupList;
}
public void setOnItemClickListener(OnItemClickListener listener) {
GroupListAdapter.listener = listener;
}
#Override
public StatusViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.single_group_list, parent, false);
return new StatusViewHolder(view);
}
#Override
public void onBindViewHolder(StatusViewHolder holder, int position) {
final GroupModel data = mGroupModels.get(position);
holder.text_view_list_name.setText(data.getGroupName());
}//end onBindViewHolder
#Override
public int getItemCount() {
return mGroupModels.size();
}
public class StatusViewHolder extends RecyclerView.ViewHolder {
public TextView text_view_list_name;
public TextView created_by;
public StatusViewHolder(final View itemView) {
super(itemView);
text_view_list_name = (TextView) itemView.findViewById(R.id.text_view_list_name);
created_by = (TextView) itemView.findViewById(R.id.created_by);
// Setup the click listener
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (listener != null)
listener.onItemClick(itemView, getLayoutPosition());
}
});
}
}//end StatusViewHolder
}//end GroupListAdapter
UPDATE:
I taken into consideration Yurii Tsap Feedback. I check the code again. I think the problem is somewhere below:
Whenever I swipe to Fresh, and If i click on the list straight way, the app Crash with the error from above.
mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
// Refresh items
mRecyclerView.invalidate();
adapter = null;
initGetGroupList();
mSwipeRefreshLayout.setRefreshing(false);
}
});
In the API call
...
GroupModel groupModel = new GroupModel(groupID, groupName, groupCreatedBy);
mGroupModels.add(groupModel);
}
updateUI(true);
if (adapter == null) {
adapter = new GroupListAdapter(MainActivity.this, mGroupModels);
mRecyclerView.setAdapter(adapter);
setAdapterListener(adapter);
}
The problem is definitely not in the static listener. And also the listener is just an interface callback(related to the comment above) not a AdapterView.OnItemClickListener(). I think the problem is somewhere behind this code, maybe you are clearing the item list somewhere else or something like that?
And also as for me in your case it's better to use getAdapterPosition() instead of getLayoutPosition().
As mentioned in docs :
If LayoutManager needs to call an external method that requires the adapter position of the item, it can use getAdapterPosition() or RecyclerView.Recycler.convertPreLayoutPositionToPostLayout(int).
Make OnItemClickListener non static .Like
private OnItemClickListener listener;
your code have alot of problem
i will explain my way to handle recycle view
very simple adapter
package com.pentavalue.ongo.transportway.adapter;
import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import com.pentavalue.ongo.R;
import com.pentavalue.ongo.Utilts.ImageLoaderHelper;
import com.pentavalue.ongo.register.model.Vechiles;
import com.pentavalue.ongo.widget.ArabicTextView;
import java.util.ArrayList;
/**
* Created by hamada on 19/09/2015.
*/
public class TransportTypeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
public LayoutInflater inflater = null;
Activity activity;
ArrayList<Vechiles> vechileList;
ImageLoaderHelper imageLoaderHelper;
public TransportTypeAdapter(Activity activity, ArrayList<Vechiles> vechileList) {
this.activity = activity;
this.vechileList = vechileList;
imageLoaderHelper = new ImageLoaderHelper(activity, null);
inflater = LayoutInflater.from(activity);
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View vi = inflater.inflate(R.layout.single_transport_item, parent, false);
RecyclerView.ViewHolder vh = new VechileListHolder(vi);
return vh;
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int pos) {
VechileListHolder mHolder = (VechileListHolder) viewHolder;
mHolder.renderDate(vechileList.get(pos));
}
#Override
public int getItemCount() {
if (vechileList != null)
return this.vechileList.size();
else
return 0;
}
public class VechileListHolder extends RecyclerView.ViewHolder implements
View.OnClickListener {
ArabicTextView vecName;
RelativeLayout img_layout;
ProgressBar progress;
ImageView imgView;
public VechileListHolder(View vi) {
super(vi);
vi.setOnClickListener(this);
img_layout = (RelativeLayout) vi.findViewById(R.id.img_layout);
vecName = (ArabicTextView) vi.findViewById(R.id.TVVecName);
progress = (ProgressBar) vi.findViewById(R.id.progress);
imgView = (ImageView) vi.findViewById(R.id.imgView);
}
public void renderDate(Vechiles item) {
imageLoaderHelper.loadImage(imgView, progress, item.getImg());
vecName.setText(item.getVecName());
}
#Override
public void onClick(View v) {
if (mItemClickListener != null)
mItemClickListener.onItemClickListener(getPosition(), v);
}
}
public void setOnItemClickListener(ItemClickListener itemClick) {
this.mItemClickListener = itemClick;
}
public ItemClickListener mItemClickListener;
public interface ItemClickListener {
public void onItemClickListener(int pos, View v);
}
public void updateList(ArrayList<Vechiles> vechileList) {
this.vechileList = vechileList;
notifyDataSetChanged();
}
public void removeItem(int pos){
this.vechileList.remove(pos);
notifyItemRemoved(pos);
}
}
and in Fragment or activity can
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
initListView();
}
private void initRecycleView(){
listView = (RecyclerView) v.findViewById(R.id.listView);
listView.getItemAnimator().setAddDuration(1000);
listView.getItemAnimator().setChangeDuration(1000);
listView.getItemAnimator().setMoveDuration(1000);
listView.getItemAnimator().setRemoveDuration(1000);
listView.setLayoutManager(new GridLayoutManager(getActivity(), 3));
listView.setAdapter(new TransportTypeAdapter(getActivity(), vecList));
}
then after data back from API can update adapter with new data by use method update
((VechileListHolder ) listView.getAdapter).updateList( vechileList);
in case went to remove any item by using Pos can do call method that in adapter removeItem(pos)
((VechileListHolder ) listView.getAdapter).removeItem(1);
hope this code help you
This question already has answers here:
RecyclerView onClick
(49 answers)
Closed 7 years ago.
What i'm basically trying to do is make the objects that show up in the recycler view clickable to a certain TextView id because i'm making a program that shows an album cover and its title next to it in a list. I need to be able to click on each one of the boxes that the recycler view makes and have a TextView pop up with the other information (author, published date, hit songs, etc) when its clicked on and then a back button (if possible) to go back to the album list. I've been looking at this for hours and cant figure out how to make an OnclickListener work for it. If you know how or have any suggestions id be glad to hear them. Thank you!
package com.albumlist.albumlist;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private AlbumData[] itemsData;
public MyAdapter(AlbumData[] itemsData){
this.itemsData = itemsData;
}
public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private TextView txtViewTitle;
private ImageView imgViewIcon;
public ViewHolder(View itemLayoutView) {
super(itemLayoutView);
itemLayoutView.setOnClickListener(this);
txtViewTitle = (TextView) itemLayoutView.findViewById(R.id.album_title);
imgViewIcon = (ImageView) itemLayoutView.findViewById(R.id.album_icon);
}
#Override
public void onClick(View v) {
}
}
#Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,int viewType) {
View itemLayoutView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.data_layout, null);
ViewHolder viewHolder = new ViewHolder(itemLayoutView);
return viewHolder;
}
#Override
public void onBindViewHolder(ViewHolder viewHolder, int position) {
viewHolder.txtViewTitle.setText(itemsData[position].getTitle());
viewHolder.imgViewIcon.setImageResource(itemsData[position].getImageUrl());
}
#Override
public int getItemCount() {
return itemsData.length;
}
}
The concept is well summed up by Xaver Kapeller in the comments. If you are looking for a simple way to manage your RecyclerView interaction similar to the traditional interactions of a ListView, check out BigNerdRanch's recyclerview-multiselect library on GitHub. They have a sample app that you can explore, which implements series of OnClickListeners and OnLongClickListeners with added capabilities of multi-selection.
Here's a snippet of how BigNerdRanch implements listeners in a Fragment across an Adapter and a ViewHolder, which in this case is actually an extension of the library's own SwappingHolder.
public CrimeHolder(View itemView) {
super(itemView, mMultiSelector);
mTitleTextView = (TextView) itemView.findViewById(R.id.crime_list_item_titleTextView);
mDateTextView = (TextView) itemView.findViewById(R.id.crime_list_item_dateTextView);
mSolvedCheckBox = (CheckBox) itemView.findViewById(R.id.crime_list_item_solvedCheckBox);
itemView.setOnClickListener(this);
itemView.setLongClickable(true);
itemView.setOnLongClickListener(this);
}
public void bindCrime(Crime crime) {
mCrime = crime;
mTitleTextView.setText(crime.getTitle());
mDateTextView.setText(crime.getDate().toString());
mSolvedCheckBox.setChecked(crime.isSolved());
}
#Override
public void onClick(View v) {
if (mCrime == null) {
return;
}
if (!mMultiSelector.tapSelection(this)) {
selectCrime(mCrime);
}
}
#Override
public boolean onLongClick(View v) {
((AppCompatActivity) getActivity()).startSupportActionMode(mDeleteMode);
mMultiSelector.setSelected(this, true);
return true;
}
}
private class CrimeAdapter extends RecyclerView.Adapter<CrimeHolder> {
#Override
public CrimeHolder onCreateViewHolder(ViewGroup parent, int pos) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_item_crime, parent, false);
return new CrimeHolder(view);
}
#Override
public void onBindViewHolder(CrimeHolder holder, int pos) {
Crime crime = mCrimes.get(pos);
holder.bindCrime(crime);
Log.d(TAG, "binding crime" + crime + "at position" + pos);
}
#Override
public int getItemCount() {
return mCrimes.size();
}
}