简体   繁体   English

检索TextView的名称并在其他类之间共享它们的值

[英]Retrieving names of TextViews and sharing their values across other classes

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? 如何获取我单击的所有TextView的名称,并将它们的值保存在Array中以用作其他类中的共享首选项?

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. 如您所见,我正在使用RecyclerView类。

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. 用户可以选择多个文本,并且共享首选项仅保存一个文本。

You can save everything in shared preferences as a JSONObject 您可以将共享首选项中的所有内容另存为JSONObject

create a JSONObject for the class 为该类创建一个JSONObject

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. 在OnClick方法中,您将获得当前的布局位置,并将所有内容保存到JSONObjet;如果您取消选中某个项目,则将其删除。

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

In the onClick event you are creating a new ArrayList and adding the clicked item text to it. 在onClick事件中,您将创建一个新的ArrayList并将单击的项目文本添加到其中。

@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. 您应该将ArrayList声明为一个字段,并在单击它们时将其添加到其中。

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

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM