How to retrive all views data from Recyclerview Android at once - java

I have a recyclerview in my android project and in the recyclerview im populating the name of the any items and it have EditText corresponding to every name view inside recyclerview, which means the person can edit the price of each item inside recyclerview.
Now outside recyclerview i have a button which when clicked a POST request is called and the names and their corresponding prices are updated.
I can't find a way by which i get all the data from recyclerview back to my fragment.
My sample code to understand the problem
public class TestsListAdapterLabsInside extends RecyclerView.Adapter<TestsListAdapterLabs.MyHolder> {
Context context;
ArrayList<TestListLabRetro> arrayList;
double[] TOTAL;
public TestsListAdapterLabsInside(Context context, ArrayList<TestListLabRetro> arrayList) {
this.context = context;
this.arrayList = arrayList;
this.TOTAL = new double[arrayList.size()];
}
#NonNull
#Override
public TestsListAdapterLabs.MyHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.recyclerview_test_labs2, viewGroup, false);
return new TestsListAdapterLabs.MyHolder(v);
}
#Override
public void onBindViewHolder(#NonNull app.dezignoo.knockadoc.Adapters.TestsListAdapterLabs.MyHolder myHolder, final int i) {
myHolder.name.setText(arrayList.get(i).getName());
myHolder.slno.setText(String.valueOf(i + 1));
myHolder.price.setText(arrayList.get(i).getPrice());
}
#Override
public int getItemCount() {
return arrayList.size();
}
public class MyHolder extends RecyclerView.ViewHolder {
TextView slno, name;
TextView price;
public MyHolder(View itemView) {
super(itemView);
name = itemView.findViewById(R.id.testName);
slno = itemView.findViewById(R.id.slno);
price = itemView.findViewById(R.id.testPrice);
}
}
}
this Adapter is defined below my fragment.
I want a final list of all the <name, price> from the recyclerview in my fragment to update to the server.
Thankyou in advance.

Step 1) First you need to add a textChangeListner on your editText and update your arrayList in it
myHolder.editText.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void afterTextChanged(Editable editable) {
TestListLabRetro testListLabRetro = arrayList.get(i);
testListLabRetro.setPrice(editable.toString());
arrayList.set(i, testListLabRetro);
}
});
Step 2) Create a getData Method
public ArrayList<TestListLabRetro> getData(){
return this.arrayList;
}
Step 3) call getData method from your adapter where you are making a POST request to get updated arrayList

After your POST request, add the data to the ArrayList list and call adapter.notifydatasetchaged()

Related

get int data from firestore

im trying to get int data from firestore but before that app crashes i dont know why when i try it as string it work i get it and i can list it but when i try it int app crashes even before get data
here is my code second part is my adapter logchat said 2 line is wrong i mark it with stairs for u
FirebaseFirestore firestore;
ArrayList<String> useremaillist;
ArrayList<Integer> userpointlist;
SkorAdapter skorAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_skor_board);
firestore=FirebaseFirestore.getInstance();
useremaillist=new ArrayList<>();
userpointlist=new ArrayList<>();
getscore();
RecyclerView skorboardrecyclerView=findViewById(R.id.recyclerView);
skorboardrecyclerView.setLayoutManager(new LinearLayoutManager(this));
skorAdapter=new SkorAdapter(useremaillist,userpointlist);
skorboardrecyclerView.setAdapter(skorAdapter);
}
public void getscore(){
CollectionReference collectionReference=firestore.collection("Kullanıcılar ve Skorlar");
collectionReference.orderBy("skor", Query.Direction.DESCENDING).addSnapshotListener(new EventListener<QuerySnapshot>() {
#Override
public void onEvent(#Nullable QuerySnapshot value, #Nullable FirebaseFirestoreException error) {
userpointlist.clear();
useremaillist.clear();
if(error!=null){
Log.e("tag",error.getLocalizedMessage());
}
if(value!=null){
for(DocumentSnapshot snapshot:value.getDocuments()){
Map<String,Object> userdatat=snapshot.getData();
String emailt=(String) userdatat.get("email");
int skor=(int) userdatat.get("skor");
useremaillist.add(emailt);
userpointlist.add(skor);
skorAdapter.notifyDataSetChanged();
}
}
}
});
}
** public class SkorAdapter extends RecyclerView.Adapter<SkorAdapter.Holder> {
private ArrayList<String> emaillistforadapter;
private ArrayList<Integer> pointlistforadapter;
public SkorAdapter(ArrayList<String> emaillistforadapter,ArrayList<Integer> pointlistforadapter ) {
this.emaillistforadapter = emaillistforadapter;
this.pointlistforadapter = pointlistforadapter;
}
#NonNull
#Override
public Holder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
LayoutInflater layoutInflater=LayoutInflater.from(parent.getContext());
View view=layoutInflater.inflate(R.layout.skorboard_row,parent,false);
return new Holder(view);
}
#Override
public void onBindViewHolder(#NonNull SkorAdapter.Holder holder, int position) {
holder.useremailtext.setText(emaillistforadapter.get(position));
** holder.userpointtext.setText(pointlistforadapter.get(position));
}
#Override
public int getItemCount() {
return emaillistforadapter.size();
}
public class Holder extends RecyclerView.ViewHolder {
TextView useremailtext;
TextView userpointtext;
public Holder(#NonNull View itemView) {
super(itemView);
useremailtext=itemView.findViewById(R.id.kullanıcımailtext);
userpointtext=itemView.findViewById(R.id.kullanıcıpointtext);
}
}
The problem is in the adapter then.
You have a private ArrayList<Integer> pointlistforadapter; and the problem is that you are doing holder.userpointtext.setText(pointlistforadapter.get(position)); and setText() accepts a CharSeq and it lets you add an Int in paramter because it also has this signature :
And since it's not a resid it crashes.
What you can do is :
holder.userpointtext.setText(pointlistforadapter.get(position).toString());

Setter in RecyclerViewAdapter not working?

I have a boolean in my onBindViewHolder which decides whether a button is clickable or not.
In the constructor I set this value to false, later when the user unlocks something I want to set it to true with a setter.
Here is my RecyclerViewAdapter.java:
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
private ArrayList<Items> items;
Context mContext;
private boolean unlocked;
public RecyclerViewAdapter(Context context, ArrayList<Items> items, boolean unlocked) {
mContext = context;
this.items = items;
this.unlocked = tabUnlocked;
}
public void setUnlocked(boolean unlocked) {
this.unlocked = unlocked;
this.notifyDataSetChanged();
}
#NonNull
#Override
public RecyclerViewAdapter.iewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(mContext).inflate(R.layout.item, parent, false);
RecyclerView.LayoutParams lp = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
v.setLayoutParams(lp);
return new RecyclerViewAdapter.ViewHolder(v);
}
#Override
public void onBindViewHolder(#NonNull final RecyclerViewAdapter.ViewHolder holder, int position) {
final Items currentItem = items.get(position);
final String name = currentItem.getName();
holder.itemTextView.setText(name);
holder.itemTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (unlocked) {
//do something
}
}
});
}
#Override
public int getItemCount() {
return item.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView itemTextView;
public ViewHolder(#NonNull View itemView) {
super(itemView);
itemTextView = itemView.findViewById(R.id.textViewItem);
}
}
}
And here is how I call the Setter to change it:
I make the RecyclerView public:
public RecyclerViewAdapter adapter;
Then I create an instance in my onCreate:
recyclerView = rootView.findViewById(R.id.recyclerView);
adapter = new RecyclerViewAdapter(items);
recyclerView.setAdapter(adapter);
And when the user unlocks a certain thing I call this method of the current Class:
public void unlockTab() {
adapter.setUnlocked(true);
}
When I log the boolean inside of that setter it tells me it got changed.
But when I log the boolean inside the onBindViewHolder it still remains false.
Why is the setter method not setting the boolean to true in the whole RecyclerViewAdapter.java class?
Hope someone can help me!
EDIT
I already tried adding "this.notifyDataSetChanged();" to the Setter Method (Thanks to #a_local_nobody)
Adapters are responsible for changes to recyclerviews. but if you don't tell them something changed, they won't know to do so.
call notifyDataSetChanged();
public void unlockTab() {
adapter.setUnlocked(true);
adapter.notifyDataSetChanged();
}
what does it do ?
It tells your recyclerview to bind all the data again, so then your changes will apply

while trying to use recyclerview, I can't get content in object in view holder

I'm trying to use recyclerview and I'm trying to design 'setText' method in viewholder use number in item parameter.
The problem is that when I put "item.getNumber()" in setText method app doesn't work. I don't know why when I put just string in method have no problem and put "item.getNumber()" have problem without error message.
adapter:
public class adapter extends RecyclerView.Adapter<adapter.ViewHolder> {
ArrayList<data> items=new ArrayList<data>();
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater=LayoutInflater.from(parent.getContext());
View itemView= inflater.inflate(R.layout.itemlayout, parent,false);
return new ViewHolder(itemView);
}
public void additem(data item){
items.add(item);
}
#Override
public void onBindViewHolder(#NonNull adapter.ViewHolder holder, int position) {
data item=items.get(position);
holder.setText(item);
}
#Override
public int getItemCount() {
return items.size();
}
static class ViewHolder extends RecyclerView.ViewHolder {
TextView textView;
EditText editText;
public ViewHolder(#NonNull View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.textView);
editText = itemView.findViewById(R.id.editText);
}
public void setText(data item) {
textView.setText(item.getNumber());
editText.setHint(item.getSentences());
}
}
}
mainActivity:
public class MainActivity extends AppCompatActivity {
static int num = 6;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView recyclerView = findViewById(R.id.recyclerview);
adapter adapter1 = new adapter();
for (int i = 0; i < num; i++) {
adapter1.additem(new data("sentences", i + 1));
}
recyclerView.setAdapter(adapter1);
LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
recyclerView.setLayoutManager(layoutManager);
}
}
data.java:
public class data {
String sentences = null;
int number;
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public data(String sentences, int number) {
this.sentences = sentences;
this.number = number;
}
public String getSentences() {
return sentences;
}
public void setSentences(String sentences) {
this.sentences = sentences;
}
}
The problem is that your getNumber() method returns int:
public int getNumber() {
return number;
}
There are two different versions of TextView.setText(), one that accepts a String and one that accepts an int. The version that accepts an int is expecting a string resource id (like R.string.foo), and it can't tell that you're just trying to display a number.
Change your setText() call so that it looks like this instead:
textView.setText("" + item.getNumber());
By using "" + item.getNumber(), you'll make sure that you're calling the version of setText() that accepts a String, and that will mean that you get to display the number you're expecting.

Can we get two return types from getItemCount() in RecyclerView?

This is my code. when i say return data1(); it runs fine but when i try return data1() + data3(); it gives me indexOutofBound Excception and i don't know the reason. Can someone help me please to find the reason of this error??
This is my MainActivity class code
String[] data={"ok","this"};
int []images_id = new int[2];
for(int i=0; i<2; i++){
images_id[i] = getResources().getIdentifier("drawable/"+"candy_"+i, null, getPackageName());
}
ArrayList ok=new ArrayList();
for (int y=0;y<data.length;y++)
{
ok.add(data[y]);
}
recyclerView.setLayoutManager(new LinearLayoutManager(getParent()));
RecyclerWorkclass recyclerWorkclass=new RecyclerWorkclass(this,data,images_id);
recyclerView.setAdapter(recyclerWorkclass);
And now this iss my RecyclerView class code
ArrayList data1,data3;
Context context;
LayoutInflater inflater;
public RecyclerWorkclass(Context context,String[] dataoftext,int[] data2)
{
inflater= LayoutInflater.from(context);
this.context=context;
data1=new ArrayList();
data3=new ArrayList();
for (int y=0;y<data2.length;y++)
{
this.data3.add(data2[y]);
}
for (int i=0;i<dataoftext.length;i++)
{
data1.add(dataoftext[i]);
}
Toast.makeText(context,"size is"+data3.size(),Toast.LENGTH_LONG).show();
}
#Override
public MyAdapter onCreateViewHolder(ViewGroup parent, int viewType) {
View view=inflater.inflate(R.layout.recyclerwork_txt,parent,false);
MyAdapter myAdapter=new MyAdapter(view);
Toast.makeText(context,"view is "+viewType,Toast.LENGTH_LONG).show();
return myAdapter;
}
#Override
public void onBindViewHolder(MyAdapter holder, int position) {
holder.imageView.setImageResource((Integer) data3.get(position));
holder.textView.setText((String)data1.get(position));
}
#Override
public int getItemCount() {
return data1.size();
}
public class MyAdapter extends RecyclerView.ViewHolder {
TextView textView;
ImageView imageView;
public MyAdapter(View itemView) {
super(itemView);
textView=itemView.findViewById(R.id.textview_phonecompany);
imageView=itemView.findViewById(R.id.imageView2);}}
}
this code works fine but when i say
return data1.size() + data3.size();
it shows index out of bound error. why is that?
Is because when you put data1.size() + data3.size() in getItemCount() you not relative with your onBindViewHolder() and this is can be call anytime RecyclerView.Adapter Doc.
When your adapter call the onBindViewHolder() he throw this exception because this must call getItemCount() method and your two list as not the size declared in getItemCount().
Explaination
#Override
public void onBindViewHolder(MyAdapter holder, int position) {
holder.imageView.setImageResource((Integer) data3.get(position));
holder.textView.setText((String)data1.get(position));
}
In this the position will more than your list because you double the size. So when he try to do data3.get(position) this exception append.

Sorting in edittext

I want to search for actors but I'm getting this error. I made it before but, now I'm trying a different adapter. It's probably so basic, but I couldnt fixed it. Please Help. Thanks.
public class Actors extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.movie_list);
ArrayList<Actor> actors = new ArrayList<Actor>();
actors.add(new Actor("jacknicholson",1,R.drawable.jacknicholson));
actors.add(new Actor("Marlon Brando",2,R.drawable.marlonbrando));
actors.add(new Actor("Robert De Niro",3,R.drawable.robertdeniro));
actors.add(new Actor("Al Pacino",4,R.drawable.alpacino));
actors.add(new Actor("Danel Day-Lewis",5,R.drawable.danielday));
actors.add(new Actor("Dustin Hoffman",6,R.drawable.dustinhoffman));
final MovieAdapter itemsAdapter = new MovieAdapter(this, actors);
ListView listView = (ListView) findViewById(R.id.list);
listView.setAdapter(itemsAdapter);
EditText theFilter =(EditText) findViewById(R.id.editt);
theFilter.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
//Here I'm getting an error
(Actors.this).itemsAdapter.getFilter().filter(charSequence);
}
#Override
public void afterTextChanged(Editable editable) {
}
});
}
}
Here is my movie adapter. In Actors class I can't use (Actors.this). I can't use it as itemsAdapter.getFilter().filter(char), but with this way it doesn't find anything.
public class MovieAdapter extends ArrayAdapter<Actor> {
public MovieAdapter(Activity context, ArrayList<Actor> words) {
super(context, 0, words);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View listItemView = convertView;
if (listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.list_item, parent, false);
}
Actor currentWord = getItem(position);
TextView songTextView = (TextView) listItemView.findViewById(R.id.Name_text_view);
songTextView.setText((currentWord.getmPopularity()));
final TextView singerTextView = (TextView) listItemView.findViewById(R.id.popularity_text_view);
singerTextView.setText(currentWord.getmActor());
ImageView iconView = (ImageView) listItemView.findViewById(R.id.list_item_icon);
iconView.setImageResource(currentWord.getImageResourceId());
return listItemView;
}
}
And here is the Actor Class
package com.example.aylin.movieapp;
import java.util.Comparator;
/**
* Created by Aylin on 2.04.2018.
*/
public class Actor {
private String mActor;
private double mPopularity;
private int mImageResourceId;
public Actor(String mActor, double mPopularity, int imageResourceId){
this.mActor = mActor;
this.mPopularity = mPopularity;
mImageResourceId = imageResourceId;
}
public String getmPopularity() {
String pop = Double.toString(mPopularity);
return pop; }
public String getmActor() { return mActor; }
public int getImageResourceId() { return mImageResourceId; }
/*Comparator for sorting the list by roll no*/
}
Ignore:afdsdgjhjgkıfgdsgjhsfdhfhafdgfhkjhılşdsfhljkipşdjlhişkdsfl
Just omit (Actors.this). entirely.
An anonymous inner class can reference all the final variables of the containing method.
I think the problem is that Actor doesn't override toString(). Of course, with the sh*tty documentation of ArrayAdapter.getFilter(), you'd either have to guess what the returned Filter does, or look at the source, which is what I did, and it calls toString() of the items the ArrayAdapter is responsible for. So overriding toString() in Actor so that it returns mActor might solve the problem.

Categories

Resources