0

How do I get all the names of TextViews that I'm clicking and save their values in an Array to use as shared preferences in other classes?

This is my code:

public class ServiceAdapter extends RecyclerView.Adapter < ServiceAdapter.ViewHolder > {

 private List < ServicesObject > itemList;
 private Context context;
 private LayoutInflater mInflater;
 private ServiceAdapter.ItemClickListener mClickListener;

 int j = 0;
 String[] allQuestions;

 public ServiceAdapter(List < ServicesObject > itemList, Context context) {
  this.itemList = itemList;
  this.context = context;
 }

 @Override
 public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

  this.mInflater = LayoutInflater.from(context);

  View view = mInflater.inflate(R.layout.listofservicesindetailspage, parent, false);
  return new ViewHolder(view);

 }

 @Override
 public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
  holder.textofService.setText(itemList.get(position).getNameofService());
  Log.d("SPAA", "NAME IS" + holder.textofService);

  Picasso.get()
   .load(itemList.get(position).getImageofServie())
   .placeholder(R.drawable.bath)
   .fit()
   .centerCrop()
   .into(holder.pictureofservice);
 }

Second class:

 public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

  public ImageView pictureofservice;
  public TextView textofService;

  public ViewHolder(View itemView) {
   super(itemView);
   pictureofservice = (ImageView) itemView.findViewById(R.id.Serviceimage);
   textofService = (TextView) itemView.findViewById(R.id.ImageText);

   itemView.setOnClickListener(this);
  }

  @Override
  public void onClick(View v) {

   ArrayList < String > mylist = new ArrayList < String > ();
   mylist.add(String.valueOf(textofService));
   Log.d("SPA", "ARRAY is " + mylist);

  }
 }

 public interface ItemClickListener {
  void onItemClick(View view, int position);
 }

 @Override
 public int getItemCount() {
  return this.itemList.size();
 }
}

As you can see I'm using the RecyclerView class.

Is there any other way to save all for use in other classes?

The user can select more than one text and shared preference only saves one.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

2 Answers2

1

You can save everything in shared preferences as a JSONObject

create a JSONObject for the class

private List<ServicesObject> itemList;
private Context context;
private LayoutInflater mInflater;
private ServiceAdapter.ItemClickListener mClickListener;

int j = 0 ;
String[] allQuestions ;

private JSONObject jsonObject = new JSONObject();

In the OnClick method you get the current layoutposition and save everything to a JSONObjet or remove the entry if you uncheck an item.

int pos = getLayoutPosition();
    if (jsonObject.has("Name"+Integer.toString(pos)){
        jsonObject.remove("Name"+Integer.toString(pos));
    }
    else{
        jsonObject.put("Name"+Integer.toString(pos),String.valueOf(textofService));
    }

when you want to add the item to shared preferences just do

jsonObject.toString()
Dominik Wuttke
  • 535
  • 1
  • 4
  • 12
0

In the onClick event you are creating a new ArrayList and adding the clicked item text to it.

@Override
public void onClick(View v) {
    ArrayList<String> mylist = new ArrayList<String>();
    mylist.add(String.valueOf(textofService));
    Log.d("SPA", "ARRAY is " +mylist);
}

You should declare the ArrayList as a field and add the items to it when they are clicked.

public class ServiceAdapter  extends  RecyclerView.Adapter<ServiceAdapter.ViewHolder>  {

private List<ServicesObject> itemList;
private Context context;
private LayoutInflater mInflater;
private ServiceAdapter.ItemClickListener mClickListener;
private List<String> myList = new ArrayList<>();

}
Hamed
  • 459
  • 4
  • 20