I have an Activity and an Adapter Class. In my Adapter class, I have an ArrayList that I want to use in the Activity class. And Recycler view of Activity class is attached to the adapter class So, I cannot use intent to send it. So, Is there any other way I can access the ArrayList?
You can create your adapter as follow -
public class RecycleViewAdapter extends RecyclerView.Adapter<RecycleViewAdapter.CustomViewModel> {
private Context context;
private List<String> stringList;
public RecycleViewAdapter(Context context, List<String> stringList) {
this.context = context;
this.stringList = stringList;
}
#NonNull
#Override
public CustomViewModel onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
return new CustomViewModel(LayoutInflater.from(context).inflate(R.layout.recycle_view_item, parent, false));
}
#Override
public void onBindViewHolder(#NonNull CustomViewModel holder, int position) {
//put your code of binding of code
}
#Override
public int getItemCount() {
if (stringList!= null)
return stringList.size();
else
return 0;
}
//getter method of Adpater to return string
public List<String> getStringList() {
return stringList;
}
public class CustomViewModel extends RecyclerView.ViewHolder {
//your inner class code here
}
}
Now you can get List from your adapter as follows-
recycleViewAdapter.getStringList();
Happy coding !
In my opinion, you have three ways to do it:
move your ArrayList to ViewModel, then pass it to your adapter or use it in the activity.
place ArrayList just in the activity and pass it to the adapter.
create a getter in your adapter and use it in the activity.
Related
I want to pass data from recyclerview to another both in fragment, first adapter
for display item, and second adapter for basket fragment that want to put selected item in.
Adapter I want to take data from:
public class FruitItemAdapter extends RecyclerView.Adapter<FruitItemAdapter.viewHolder> {
ArrayList<FruitItem> fruitItems = new ArrayList<>();
private Context context;
public FruitItemAdapter(ArrayList<FruitItem> fruitItems, Context context) {
this.fruitItems = fruitItems;
this.context = context;
notifyDataSetChanged();
}
public FruitItemAdapter() {
}
#NonNull
#Override
public viewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view =
LayoutInflater.from(parent.getContext()).inflate(R.layout.fruits_item,parent,false);
viewHolder viewHolder = new viewHolder(view);
return viewHolder;
}
#Override
public void onBindViewHolder(#NonNull viewHolder holder, int position) {
final FruitItem data_position = fruitItems.get(position);
holder.fruit_img.setImageResource(fruitItems.get(position).getFruit_img());
holder.fruit_name.setText(fruitItems.get(position).getFruit_name());
holder.fruit_price.setText(fruitItems.get(position).getFruit_price());
}
#Override
public int getItemCount() {
return fruitItems.size();
}
public void setfruitItem(ArrayList<FruitItem> fruitItems) {
this.fruitItems = fruitItems;
}
public static class viewHolder extends RecyclerView.ViewHolder {
private ImageView fruit_img;
private TextView fruit_price, fruit_name;
public viewHolder(#NonNull View itemView) {
super(itemView);
fruit_img = itemView.findViewById(R.id.fruit_img);
fruit_price = itemView.findViewById(R.id.fruit_price);
fruit_name = itemView.findViewById(R.id.fruit_name)
}
}
}
this is adapter for basket fragment that I want to put the data in
public class Basket_Adapter extends RecyclerView.Adapter<Basket_Adapter.viewHolder> {
private Context context;
ArrayList<FruitItem> fruitItems = new ArrayList<>();
public Basket_Adapter(Context context, ArrayList<FruitItem> fruitItems) {
this.context = context;
this.fruitItems = fruitItems;
notifyDataSetChanged();
}
public Basket_Adapter(){
}
#NonNull
#Override
public Basket_Adapter.viewHolder onCreateViewHolder(#NonNull ViewGroup parent, int
viewType) {
View view=LayoutInflater.from(parent.getContext()).inflate(R.layout.fruits_item,parent,false);
viewHolder viewHolder = new viewHolder(view);
return viewHolder;
}
#Override
public void onBindViewHolder(#NonNull Basket_Adapter.viewHolder holder, int position) {
holder.fruit_img.setImageResource(fruitItems.get(position).getFruit_img());
holder.fruit_name.setText(fruitItems.get(position).getFruit_name());
holder.fruit_price.setText(fruitItems.get(position).getFruit_price());
}
#Override
public int getItemCount() {
return fruitItems.size();
}
public void setfruitItem(ArrayList<FruitItem> fruitItems) {
this.fruitItems = fruitItems;
}
public class viewHolder extends RecyclerView.ViewHolder {
private ImageView fruit_img;
private TextView fruit_name;
private TextView fruit_price;
public viewHolder(#NonNull View itemView) {
super(itemView);
fruit_img = itemView.findViewById(R.id.fruit_img);
fruit_name = itemView.findViewById(R.id.fruit_name);
fruit_price = itemView.findViewById(R.id.fruit_price);
}
}
Now, what I can use to pass data between them.
You can achieve this by using the delegation pattern. Basically you create an interface relative to the first adapter (you can put it inside the adapter class or outside depending on your coding style) and you require it as an argument inside the adapter constructor like this:
public class FruitItemAdapter extends RecyclerView.Adapter<FruitItemAdapter.viewHolder> {
private Delegate delegate;
ArrayList<FruitItem> fruitItems = new ArrayList<>();
private Context context;
public FruitItemAdapter(Delegate delegate, ArrayList<FruitItem> fruitItems, Context context) {
this.delegate = delegate;
this.fruitItems = fruitItems;
this.context = context;
notifyDataSetChanged();
}
...
interface Delegate {
public void passItem(FruitItem item);
}
}
As you can see the interface has the method you need, but there's no implementation yet.
In this class you can just pretend that your delegate works and do the magic for you, for example by setting a click listener on the root view of your item in onBindViewHolder that will call delegate.passItem(fruitItems.get(position)) on each click.
Let's move on to the fragment.
Here is the key part. The fragment must implement the interface we just created by overriding its methods. Like so:
class ExampleFragment extends Fragment implements FruitItemAdapter.Delegate {
...
#Override
public void passItem(FruitItem item) {
// here you pass the item in a list inside
// the shared preferences.
}
}
For your case the best way is to store your items in a database or in the shared preferences. We go with the shared preferences because is simpler, but keep in mind that shared preferences have limited memory capacity and you should use a database like Room instead.
Inside the override method you pass your item to a list stored in the shared preferences. Since your item is not a primitive object i suggest you to look at this answer that show how to store complex object as a string:
https://stackoverflow.com/a/18463758/18740763.
In your case the object that needs to be serialized is an Array or a List of objects.
Every time you need to put a new object in the list you need to follow these steps:
get the list from shared preferences
deserialize it
add the new item
serialize it again
put it back in the shared preferences under the same key
If you stored your items correctly now you should be able to access the shared list in every fragment or activity of your application. So simply access your list from the fragment that implements the second adapter, deserialize it, just addAll() the items the the adapter list and notifyDataSetChanged().
I have multiple array lists inside an arraylist :
[[Object1, Object2],[Object3, Object4],[Object5, Object6]]
I display the first array list in a recyclerview that displays one arraylist at a time:
myViewHolder.bindTo(cities.get(0).get(i));
I want to click a button in another class that would show change the array list shown. How would I achieve this?
Recycler View Adapter Class:
private List<List<Country>> cities;
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
PlannerItemBinding plannerItemBinding =
DataBindingUtil.inflate(LayoutInflater.from(viewGroup.getContext()), R.layout.planner_item, viewGroup, false);
return new MyViewHolder(plannerItemBinding);
}
#Override
public void onBindViewHolder(#NonNull MyViewHolder myViewHolder, int i) {
myViewHolder.bindTo(cities.get(0).get(i));
}
#Override
public int getItemCount() {
if (cities != null) {
return cities.size();
} else {
return 0;
}
}
public void setCityList(List<List<Country>> cities) {
this.cities = cities;
notifyDataSetChanged();
}
class MyViewHolder extends RecyclerView.ViewHolder {
private PlannerItemBinding plannerItemBinding;
public MyViewHolder(#NonNull PlannerItemBinding plannerItemBinding) {
super(plannerItemBinding.getRoot());
this.plannerItemBinding = plannerItemBinding;
}
void bindTo(Country country) {
plannerItemBinding.setVariable(com.example.planner.BR.city, country);
plannerItemBinding.setVariable(com.example.planner.BR.adapterPosition, getLayoutPosition());
plannerItemBinding.setVariable(com.example.planner.BR.countryImageMedium, country.getImages().get(0).getSizes().getMedium());
plannerItemBinding.executePendingBindings();
}
}
}
Set a setter in your RecyclerView.Adapter:
public updateCities(List<List<Coutry>> cities) {
this.cities = cities;
notifydatasetchanged();
}
so that you can call it in onClick() of that button with the new data.
This will update the model of your adapter with the passed data and notify the recyclerview that its data has changed.
You create an onClickListener on the object of your choice, and then when it is clicked you just setCityList to the new data and use notifydatasetchanged on your adapter
I am using custom recycler view and in adapter class i have implemented interface which is always null on button click. Here is my adapter class.
public class FeedListAdapter extends
RecyclerView.Adapter<AddtoCartHolder> {
private OnFeedItemClickListener onFeedItemClickListener;
public FeedListAdapter(Activity activity, ArrayList<CartItem> feedItems) {
this.activity = activity;
this.feedItems = feedItems;
this.filteredfeedItems = feedItems;
inflater = LayoutInflater.from(activity);
}
public void setOnFeedItemClickListener(OnFeedItemClickListener onFeedItemClickListener) {
this.onFeedItemClickListener = onFeedItemClickListener;
}
#Override
public AddtoCartHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(
R.layout.feed_item, parent, false);
AddtoCartHolder viewHolder = new AddtoCartHolder(v);
setupClickableViews(v, viewHolder);
return viewHolder;
}
#Override
public void onBindViewHolder(final AddtoCartHolder holder, int position) {
CartItem item = (CartItem) filteredfeedItems.get(position);
holder.price.setText((String.valueOf(item.getProductName()) + ""));
holder.location.setText((String.valueOf(item.getQuantity())) + "");
}
private void setupClickableViews(final View view, final AddtoCartHolder cellFeedViewHolder) {
cellFeedViewHolder.plus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(onFeedItemClickListener !=null){
onFeedItemClickListener.onAddClick(v, cellFeedViewHolder.getAdapterPosition());
}
else{
Toast.makeText(activity, "Data is null", Toast.LENGTH_LONG).show();
}
}
});
}
public interface OnFeedItemClickListener {
void onAddClick(View v, int position);
}
#Override
public int getItemCount() {
return filteredfeedItems.size();
}
I am always getting null whenever clicking on button really down know why it is coming null...
Here is my fragment class which have implemented interface.
public class MyFragment extends Fragment implements FeedListAdapter.OnFeedItemClickListener{
// the method
#Override
public void onAddClick(View v, int position) {
// TODO Auto-generated method stub
Snackbar.make(clContent, "Product removed from cart!",
Snackbar.LENGTH_SHORT).show();
}
You must be instantiating a FeedListAdapter in your fragment correct buddy ???
Like using statement :
FeedListAdapter adapter = new FeedListAdapter(this.getActivity(),your_array_list)
After instantiating your adapter just call your adapter's setOnFeedItemClickListener with 'this' as argument :) That's all :)
adapter.setOnFeedItemClickListener(this)
Hope my answer helped you :) Happy coding buddy :)
By the looks of things your aren't setting your listener. Thus, onFeedItemClickListener is always null.
Also MyFragment isn't actually doing anything, you haven't inflated a view, overridden onCreateView(...), etc.
There's a few things that you could definitely change to improve your implementation. But to get your listener working:
Just get rid of MyFragment you don't appear to be using it properly.
Move your implements FeedListAdapter.OnFeedItemClickListener to your Activity. i.e. Make your Activity implement your OnFeedItemClickListener interface rather than MyFragment (which doesn't appear to be doing anything).
Make FeedListAdapter set the listener in its constructor:
public FeedListAdapter(Activity activity, ArrayList<CartItem> feedItems)
{
this.activity = activity;
// Assume we the activity implements OnFeedItemClickListener
setOnFeedItemClickListener((OnFeedItemClickListener)activity);
this.feedItems = feedItems;
this.filteredfeedItems = feedItems;
inflater = LayoutInflater.from(activity);
}
Please keep in mind that this is a pretty bad implementation and you can definitely improve on it but for the purpose of the question, it's sufficient.
public class AdapterQuestion extends RecyclerView.Adapter<AdapterQuestion.ViewQuestion>{
private LayoutInflater mLayoutInflater;
//this is an arrayList of questionData objects
private ArrayList<QuestionData> data =new ArrayList<>();
//Created the layoutInflator
public AdapterQuestion(Context context){
//get from context
mLayoutInflater=LayoutInflater.from(context);
}
public void setBloglist(ArrayList<QuestionData> data){
this.data =data;
notifyItemRangeChanged(0, data.size());
System.out.print("Size of the array "+data.size());
}
#Override
public ViewQuestion onCreateViewHolder(ViewGroup parent, int viewType) {
//inflates the customQuestion view or converts it to java code
View view= mLayoutInflater.inflate(R.layout.customquestion, null);
//We now want to convert the View into a ViewQuestion, view Question takes
//a view so we pass the view into view question and then return it.
ViewQuestion holder=new ViewQuestion(view);
return holder;
}
//ViewGroup parent and ViewType are not being assigned.
#Override
public void onBindViewHolder(ViewQuestion holder, int position) {
//here we need to bind the data to our view, there is currently no Data!
//We need to get the data from our JSON
//Parameters is a ViewHolder and a Position
QuestionData currentBlog= data.get(position);
holder.answerText.setText(currentBlog.getMtext());
holder.answerId.setText(currentBlog.getId());
holder.mVotes.setText(currentBlog.getVotes());
holder.mLikeButton.setTag(currentBlog);
}
#Override
public int getItemCount() {
return data.size();
}
public class ViewQuestion extends RecyclerView.ViewHolder{
//once we create it once the reclycer view will automatically recycle it
private TextView answerText;
private TextView answerId;
private TextView mVotes;
private LikeButton mLikeButton;
public ViewQuestion (View itemView){
super(itemView);
//here we are finding the views by their ID
answerText=(TextView)itemView.findViewById(R.id.answerText);
answerId=(TextView)itemView.findViewById(R.id.answerId);
mVotes=(TextView)itemView.findViewById(R.id.VoteTextView);
mLikeButton=(LikeButton)itemView.findViewById(R.id.heart_buttons);
mLikeButton.setOnLikeListener(new OnLikeListener() {
#Override
public void liked(LikeButton likeButton) {
Voting vote = new Voting();
vote.onUpVote(convertToString(),getAdapterPosition(),ViewQuestion.this);
System.out.print("Adapter Position"+getAdapterPosition());
}
#Override
public void unLiked(LikeButton likeButton) {
Voting onDown=new Voting();
onDown.onDownVote(convertToString(), getAdapterPosition(), ViewQuestion.this);
}
});
}
public String getVoteView(){
String voteView=mVotes.getText().toString();
return voteView;
}
public String convertToString(){
String converted=answerId.getText().toString();
return converted;
}
public int convertToInt(){
String converted=answerId.getText().toString();
int ConvertedInt=Integer.parseInt(converted);
return ConvertedInt;
}
}
}
1.First off, why do we extend RecyclerView.Adapter as an arrayList?
2.public void setBloglist(ArrayList<QuestionData> data){
this.data =data;
notifyItemRangeChanged(0, data.size());
System.out.print("Size of the array "+data.size());
}
In the method above, we passed in an ArrayList, and we set the data of the Arraylist to the data passed in but dont we have to set the data in the ViewHolder?
3. super(itemView); Why does the ViewHolder extend super? isnt that itself?
It is not ArrayList it is generics
this keyword refers to the current object so we are setting the value of the data
instance variable in the AdapterQuestion's object to the value passed as method arguement
the ViewHolder does'nt extend a class named super but your ViewQuestion class extends RecyclerView.ViewHolder class ,the super(itemView);is used to call the RecyclerView.ViewHolder constructor
I have an Aync task
public class XmlNetwork extends AsyncTask< String, Void, List<PEMenuItem>
which essentially loads up an xml file into some objects that I want to use to change my navigation drawer menu items. it has an override function
protected void onPostExecute(List<PEMenuItem> result)
and so result is now a list of 3 objects i loaded from the xml.
at this point, i would like to make a call within my class
public class NavigationDrawerFragment extends Fragment
to build out the menu, add the onclicklisteners, etc.
i've previously learned how to add some callbacks, to call from the fragment out to my main activity, but how would i go about doing the inverse, calling from my Async class out to the fragment? i want to send the list along with it, something along the lines of
#Override
protected void onPostExecute(List<PEMenuItem> result) {
NavigationDrawerFragment fragment = (NavigationDrawerFragment) getFragment().modifyMenu(result);
}
The menu items in your NavigationDrawerFragment are usually just items in a ListView. As such, they are backed by an Adapter. Set your new items as that adapter's content and call notifyOnDatasetChanged on the adapter. This will cause the list to re-obtain the items from that adapter. For example you could use something like this as an adapter to your menu list (simplified):
public class MenuItemsAdapter extends BaseAdapter {
private List<String> menuItems;
public MenuItemsAdapter(Context context) {
this.context = context;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//Empty for now
menuItems = new ArrayList<>();
}
public void setNewMenuItems(List<String> newMenuItems) {
this.menuItems = newMenuItems;
notifyDataSetChanged();
}
#Override
public int getCount() {
return menuItems.size();
}
#Override
public String getItem(int i) {
return menuItems.get(i);
}
#Override
public long getItemId(int i) {
return i;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
if(view == null) {
view = inflater.inflate(R.layout.your_menu_item_layout, viewGroup, false);
}
TextView menuItemText = view.findViewByIt(R.id.your_menu_item_text);
menuItemText = menuItems.get(position);
return view;
}
}