Why the second RecyclerView is not working? - java

I'm using two firebase UI Recycler views in a single activity. The first recycler view is working fine but I don't know why the second Recycler view is not populating. One thing I noticed that in the second recycler view 'OnBind' and 'OnCreate' methods are not called up?
Basically, I'm trying to use two horizontal recycler views within a single activity.
public class MainActivity extends AppCompatActivity {
private FirebaseRecyclerOptions<Video> options;
private FirebaseRecyclerAdapter<Video, MyViewHolder> adapter;
private RecyclerView recyclerViewVideo;
private FirebaseRecyclerOptions<Meme> optionsForHindiJokes;
private FirebaseRecyclerAdapter<Meme, MyViewHolder> adapterForHindiJokes;
private RecyclerView recyclerViewForHindiJokes;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerViewVideo = findViewById(R.id.recycler_view_video);
recyclerViewVideo.setHasFixedSize(true);
recyclerViewVideo.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false));
recyclerViewForHindiJokes = findViewById(R.id.recycler_view_hindi_jokes);
recyclerViewForHindiJokes.setHasFixedSize(true);
recyclerViewForHindiJokes.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
/* **/
final FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference refToVideo1 = database.getReferenceFromUrl("https://memlo-a9291-default-rtdb.firebaseio.com/AppStructure/Videos/Video");
refToVideo1.keepSynced(true);
Query refToVideo = database.getReferenceFromUrl("https://memlo-a9291-default-rtdb.firebaseio.com/AppStructure/Videos/Video").limitToLast(20);
options = new FirebaseRecyclerOptions.Builder<Video>().setQuery(refToVideo, Video.class).build();
adapter= new FirebaseRecyclerAdapter<Video, MyViewHolder>(options) {
#Override
protected void onBindViewHolder(#NonNull MyViewHolder holder, int position, #NonNull final Video model) {
RequestOptions requestOptions = new RequestOptions();
requestOptions.placeholder(R.drawable.ic_memlo_without_background);
requestOptions.error(R.drawable.ic_memlo_without_background);
Glide.with(MainActivity.this)
.load(model.getVideoUrl())
.apply(requestOptions)
.thumbnail(Glide.with(MainActivity.this).load(model.getVideoUrl()))
// .centerCrop()
.into(holder.imageOnCard);
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent1 = new Intent(MainActivity.this, VideoPlayer.class);
intent1.putExtra("VIDEO_URL", model.getVideoUrl());
startActivity(intent1);
}
});
}
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.single_cards_layout, parent, false);
return new MyViewHolder(v);
}
};
adapter.startListening();
recyclerViewVideo.setAdapter(adapter);
////////////////////////////////////////////////
Query refToHindiJokes = database.getReferenceFromUrl("https://memlo-a9291-default-rtdb.firebaseio.com/AppStructure/Memes/Category/Hindi%20Jokes");
optionsForHindiJokes = new FirebaseRecyclerOptions.Builder<Meme>().setQuery(refToHindiJokes, Meme.class).build();
adapterForHindiJokes = new FirebaseRecyclerAdapter<Meme, MyViewHolder>(optionsForHindiJokes) {
#Override
protected void onBindViewHolder(#NonNull MyViewHolder holder, int position, #NonNull Meme model) {
Toast.makeText(getApplicationContext(),"Calling", Toast.LENGTH_LONG).show();
Glide.with(getApplicationContext())
.load(model.getImageUrl())
.into(holder.imageOnCard);
}
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v1=LayoutInflater.from(getApplicationContext()).inflate(R.layout.single_cards_layout, parent,false);
return new MyViewHolder(v1);
}
};
adapterForHindiJokes.startListening();
adapterForHindiJokes.notifyDataSetChanged();
recyclerViewForHindiJokes.setAdapter(adapterForHindiJokes);
}
}

Related

My recycler view not shown for the first time in fragment

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();

Shuffle items from Firebase Database

How to shuffle items from Firebase Database?
Let's say my DB contains 4 items [1,2,3,4]. In my RecyclerView, I want to display shuffled, something like [2,3,1,4]. How can I do that?
This is the code in my Fragment. So it works retrieving data but I need to shuffle it before displaying.
RecyclerView mRecyclerView;
FirebaseDatabase mFirebaseDatabase;
DatabaseReference mRef;
#Override
public View onCreateView(#NotNull LayoutInflater inflater, ViewGroup viewGroup, Bundle savedInstanceState) {
if (rootView == null) {
rootView = inflater.inflate(R.layout.fragment_store, viewGroup, false);
setHasOptionsMenu(false);
mRecyclerView = rootView.findViewById(R.id.recycler_view);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 3));
mFirebaseDatabase = FirebaseDatabase.getInstance();
mRef = mFirebaseDatabase.getReference("Store");
}
return rootView;
}
For retrieving data from Firebase Database I am using the following code:
#Override
public void onStart() {
super.onStart();
FirebaseRecyclerOptions<Store2> options =
new FirebaseRecyclerOptions.Builder<Store2>()
.setQuery(mRef, Store2.class)
.build();
FirebaseRecyclerAdapter<Store2, StoreHolder> firebaseRecyclerAdapter =
new FirebaseRecyclerAdapter<Store2, StoreHolder>(options) {
#NonNull
#Override
public StoreHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.store_gridview, parent, false);
return new StoreHolder(view);
}
#Override
protected void onBindViewHolder(#NonNull final StoreHolder holder, int position, #NonNull final Store2 model) {
holder.setDetails(getContext(), model.getImage(), model.getName(), model.getPrice());
}
};
firebaseRecyclerAdapter.startListening();
mRecyclerView.setAdapter(firebaseRecyclerAdapter);
}
There are no option available for shuffling data in firebase.
But you can do it locally .
Pass your list in this.
Collections.shuffle(arrayList);
When you are using the following reference:
mRef = mFirebaseDatabase.getReference("Store");
It means that you are trying to display all children that exist under Store node. If under this node you store children that are added using the push() method, the default ordering is chronological, since every pushed id contains a time component. If you want to change this behaviour, you need to get all the items and shuffle them manually.
As #DougStevenson mentioned in his comment, you have to use the same reference, get all items, add them to a list, shuffle them and then use a ListAdapter for that.
Please also note, that when you pass a DocumentReference or a Query object to the setQuery() method, where is no further mechanism that can help you shuffle your items. Unfortunately, you'll lose the benefits of the Firebase-UI library but you'll get your work done.
As #DougStevenson and #frankenstein mentioned, I added all items into a list, then I was able the shuffle it using Collections.shuffle(arrayList);
I couldn't do it using FirebaseRecyclerAdapter, and I needed a custom Adapter for my RecyclerView.ViewHolder.
What is the downside of not using FirebaseRecyclerAdapter? Or is it any? Maybe someone with experience can answer this question.
My fragment:
private View rootView;
private RecyclerView recyclerView;
private StoreAdapter storeAdapter;
private ArrayList<Store> arrayStore;
#Override
public View onCreateView(#NotNull LayoutInflater inflater, ViewGroup viewGroup, Bundle savedInstanceState) {
if (rootView == null) {
rootView = inflater.inflate(R.layout.fragment_store, viewGroup, false);
setHasOptionsMenu(false);
recyclerView = rootView.findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 3));
DatabaseReference reference = FirebaseDatabase.getInstance().getReference().child("Data");
reference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
arrayStore = new ArrayList<>();
for(DataSnapshot snapshot: dataSnapshot.getChildren()) {
Store store = snapshot.getValue(Store.class);
arrayStore.add(store);
}
storeAdapter = new StoreAdapter(getActivity() ,arrayStore);
recyclerView.setAdapter(storeAdapter);
Collections.shuffle(arrayStore, new Random(3));
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
return rootView;
}
My adapter:
private final Context context;
private ArrayList<Store> arrayStore;
public StoreAdapter(Context context, ArrayList<Store> arrayStore) {
this.context = context;
this.arrayStore = arrayStore;
}
#NonNull
#Override
public StoreAdapter.GameViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
return new StoreAdapter.GameViewHolder(LayoutInflater.from(context).inflate(
R.layout.store_gridview, viewGroup, false));
}
#Override
public void onBindViewHolder(#NonNull final StoreAdapter.GameViewHolder viewHolder, final int position) {
setColor(viewHolder);
viewHolder.itemName.setText(arrayStore.get(position).getName());
Glide.with(viewHolder.view.getContext())
.load(arrayStore.get(position).getImage())
.thumbnail(0.75f)
.into(viewHolder.itemImage);
viewHolder.view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
sendData(viewHolder, position);
}
});
}
#Override
public int getItemCount() {
return arrayStore.size();
}
class GameViewHolder extends RecyclerView.ViewHolder {
private final View view;
private final ImageView itemImage, itemRarity;
private final TextView itemName;
GameViewHolder(#NonNull View itemView) {
super(itemView);
this.view = itemView;
itemRarity = view.findViewById(R.id.itemRarity);
itemImage = view.findViewById(R.id.itemImage);
itemName = view.findViewById(R.id.itemName);
}
}
private void sendData(#NonNull final StoreAdapter.GameViewHolder viewHolder, int position) {
if (viewHolder.getAdapterPosition() != RecyclerView.NO_POSITION) {
Intent intent = new Intent(context, DetailsActivity.class);
intent.putExtra("name", arrayStore.get(position).getName());
intent.putExtra("rarity", arrayStore.get(position).getRarity());
intent.putExtra("item", arrayStore.get(position).getImage());
intent.putExtra("price", arrayStore.get(position).getPrice());
intent.putExtra("key", "Store");
context.startActivity(intent);
}
}
private void setColor(#NonNull final StoreAdapter.GameViewHolder viewHolder) {
String rarity = arrayStore.get(viewHolder.getAdapterPosition()).getRarity();
if (rarity.contains("legendary")) {
viewHolder.itemRarity.setImageResource(R.drawable.color_legendary_gradient);
} else if (rarity.contains("classic")) {
viewHolder.itemRarity.setImageResource(R.drawable.color_classic_gradient);
} else if (rarity.contains("epic")) {
viewHolder.itemRarity.setImageResource(R.drawable.color_epic_gradient);
} else if (rarity.contains("elite")) {
viewHolder.itemRarity.setImageResource(R.drawable.color_elite_gradient);
} else if (rarity.contains("rare")) {
viewHolder.itemRarity.setImageResource(R.drawable.color_rare_gradient);
} else if (rarity.contains("special")) {
viewHolder.itemRarity.setImageResource(R.drawable.color_special_gradient);
}
}
My bean class:
private String image, name, price, rarity;
public Store() {
}
public String getImage() {
return image;
}
public String getName() {
return name;
}
public String getPrice() {
return price;
}
public String getRarity() {
return rarity;
}

Android RecyclerView display issue in adapter

This is my code.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_all_user);
// getting id
UserList=findViewById(R.id.ChatUserRecycle);
toolbar=findViewById(R.id.mainPageBar);
databaseReference=FirebaseDatabase.getInstance().getReference().child("Student");
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Users"); //setting action bar title
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
UserList.setHasFixedSize(true);
UserList.setLayoutManager(new LinearLayoutManager(this));
}
#Override
protected void onStart() {
super.onStart();
FirebaseRecyclerOptions<ChatUsers> options=
new FirebaseRecyclerOptions.Builder<ChatUsers>()
.setQuery(databaseReference,ChatUsers.class)
.setLifecycleOwner(this)
.build();
firebaseRecyclerAdapter=new FirebaseRecyclerAdapter<ChatUsers,UsersViewHolder>(options) {
#NonNull
#Override
public UsersViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
return new UsersViewHolder(LayoutInflater.from(parent.getContext())
.inflate(R.layout.user_layout, parent, false));
}
#Override
protected void onBindViewHolder(#NonNull UsersViewHolder holder, int position, #NonNull ChatUsers model) {
holder.setName(model.getName());
}
};
UserList.setAdapter(firebaseRecyclerAdapter);
}
public static class UsersViewHolder extends RecyclerView.ViewHolder{
View mview;
public UsersViewHolder(#NonNull View itemView) {
super(itemView);
mview=itemView;
}
public void setName(String name){
Toast.makeText(itemView.getContext(),"2",Toast.LENGTH_SHORT).show();
TextView name1=(TextView)mview.findViewById(R.id.SingleUsername);
name1.setText(name);
}
}
}
When I run my code nothing show just empty layout without recycleview and layout which want to I inflate. And after few seconds app.
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;
}
};
First I use this method but it also not works.
When I run my code nothing show just empty layout
This is because you're not creating a view for your holder at all. To solve this, please add the following lines of code:
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.individual_row, parent, false);
return new BlogViewHolder(view);
Inside your onCreateViewHolder() method and remove return null;.

Firebase Recycler Adapter is not populated

I am having trouble with populating my Firebase recycler adapter. What I want to do in the end is that a user searches for a name and the result from the database will populate the recyclerView.
The database structure looks like this:
The code to populate looks like this, this is all in onCreate():
Query mQuery = FirebaseDatabase.getInstance().getReference().orderByChild("users");
FirebaseRecyclerOptions<Users> options =
new FirebaseRecyclerOptions.Builder<Users>()
.setQuery(mQuery, Users.class)
.build();
mAdapter = new FirebaseRecyclerAdapter<Users, UsersViewHolder>(options) {
#NonNull
#Override
public UsersViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.user_single_layout, parent, false);
return new UsersViewHolder(view);
}
#Override
protected void onBindViewHolder(#NonNull UsersViewHolder holder, int position, #NonNull Users model) {
holder.setName(model.getFirstName());
}
};
mUserList.setAdapter(mAdapter);
Right now I just want it to populate with all the users in the database but nothing seems to be populated.
mUserList is a recyclerView.
This is the UsersViewHolder class:
public static class UsersViewHolder extends RecyclerView.ViewHolder {
View mView;
TextView mUsername;
UsersViewHolder(View itemView) {
super(itemView);
mView = itemView;
mUsername = mView.findViewById(R.id.user_single_name);
}
public void setName(String name) {
mUsername.setText(name);
}
}
To solve this, change the following line of code:
Query mQuery = FirebaseDatabase.getInstance().getReference().orderByChild("users");
with:
Query mQuery = FirebaseDatabase.getInstance().getReference()
.child("users")
.orderByChild("first_name");
You are missing a child. Don't also forget to start listening for changes:
#Override
protected void onStart() {
super.onStart();
mAdapter.startListening();
}
And also to stop listening when not needed.
#Override
protected void onStop() {
super.onStop();
if( mAdapter!= null) {
mAdapter.stopListening();
}
}
Use that way. Follow my code for yourself.
FirebaseRecyclerOptions<Data> options=
new FirebaseRecyclerOptions.Builder<Data>()
.setQuery(mIncomeDatabase,Data.class)
.setLifecycleOwner(this)
.build();
FirebaseRecyclerAdapter<Data, MyViewHolder> firebaseRecyclelerAdapter = new FirebaseRecyclerAdapter<Data, MyViewHolder>(options) {
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
return new MyViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.income_recycler_data,parent,false));
}
#Override
protected void onBindViewHolder(#NonNull MyViewHolder holder, int position, #NonNull Data model) {
holder.setName(model.getName());
holder.setAmount(model.getAmount());
holder.setType(model.getType());
holder.setNote(model.getNote());
holder.setAmount(model.getAmount());
holder.setDate(model.getDate());
}
};
recyclerView.setAdapter(firebaseRecyclelerAdapter);

How to create an Intent within a fragment class

I have managed to create a tabbed activity using recycle view and have made a list-view in each fragment, now I have the lists I need to make the cards in the fragment clickable and create an intent to start a new activity. I have tried on-click method and onListItemClick even though I knew that would not work.
Basically I need to create one more layer of lists for each fragment which will lead to a list of actions (webviews, Maps, and information).
Example Code is below
ublic class LOneFragment extends Fragment {
public LOneFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_blank, container, false);
RecyclerView rv = (RecyclerView) rootView.findViewById(R.id.rv_recycler_view);
rv.setHasFixedSize(true);
MyAdapter adapter = new MyAdapter(new String[]{"League One News", "AFC Wimbledon", "Blackburn Rovers", "Blackpool", "Bradford City", "Bristol Rovers", "Bury", "Charlton Athletic", "Doncaster Rovers", "Fleetwood Town",
"Gillingham", "Milton Keynes Dons", "Northampton Town", "Oldham Athletic", "Oxford United", "Peterborough United", "Plymouth Argyle", "Portsmouth", "Rochdale", "Rotherham United",
"Scunthorpe United", "Shrewsbury Town", "Southend United", "Walsall", "Wigan Athletic"});
rv.setAdapter(adapter);
LinearLayoutManager llm = new LinearLayoutManager(getActivity());
rv.setLayoutManager(llm);
return rootView;
}
public void onListItemClick(ListView l, View v, int position, long id) {
if (position == 3) {
Intent intent = new Intent(getContext(), Bradford.class);
startActivity(intent);
}
My adaptor class
public class MyAdapter extends RecyclerView.Adapter {
private String[] mDataset;
public static class MyViewHolder extends RecyclerView.ViewHolder{
public CardView mCardView;
public TextView mTextView;
public MyViewHolder(View v){
super(v);
mCardView = (CardView) v.findViewById(R.id.card_view);
mTextView = (TextView) v.findViewById(R.id.tv_text);
}
}
public MyAdapter(String[] myDataset){
mDataset = myDataset;
}
#Override
public MyAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_item, parent, false);
MyViewHolder vh = new MyViewHolder(v);
return vh;
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position){
holder.mTextView.setText(mDataset[position]);
}
#Override
public int getItemCount() { return mDataset.length; }
}
This works perfect for me.
1) Create method in viewholder class which will take position, get array item from position and use the way you like.
2) Access it in onBindViewHolder method with itemView instance which is the full row at given position,
public class MyAdapter extends RecyclerView.Adapter {
private String[] mDataset;
public MyAdapter(String[] myDataset){
mDataset = myDataset;
}
#Override
public MyAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_item, parent, false);
MyViewHolder vh = new MyViewHolder(v);
return vh;
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position){
holder.mTextView.setText(mDataset[position]);
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
holder.bindCliks(groupItem);
}
});
#Override
public int getItemCount() { return mDataset.length; }
}
public static class MyViewHolder extends RecyclerView.ViewHolder{
public CardView mCardView;
public TextView mTextView;
public MyViewHolder(View v){
super(v);
mCardView = (CardView) v.findViewById(R.id.card_view);
mTextView = (TextView) v.findViewById(R.id.tv_text);
}
public void bindCliks(int position) {
Intent intent = new Intent(context,
OtherActivity.class);
//use number here to get your string item and use it to other activity.
context.startActivity(intent);
}
}

Categories

Resources