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

标签 java android

如何获取我正在单击的 TextView 的所有名称并将其值保存在数组中以用作其他类中的共享首选项?

这是我的代码:

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

第二类:

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

如您所见,我正在使用 RecyclerView 类。

还有其他方法可以保存所有内容以供其他类使用吗?

用户可以选择多个文本,共享首选项仅保存一个。

最佳答案

您可以将共享首选项中的所有内容保存为 JSONObject

为该类创建一个 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();

在 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));
    }

当您想将项目添加到共享首选项时,只需这样做

jsonObject.toString()

关于java - 检索 TextView 的名称并在其他类之间共享它们的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57178469/

相关文章:

java - 正则表达式 - 捕获组混淆

java - 将 "rgb (x, x, x)"字符串解析为颜色对象

android - 检测相对布局上的水平滑动,并在不拦截的情况下正确处理相对布局上的滚动事件

java - 什么时候安装 Guice 模块?

java - byte, short, char 是否在 switch 语句中自动提升?

java - 将不同长度的列表添加到数组中

android - 当我两次启动闹钟时会发生什么?

android - MapBox SDK如何获取marker的点击事件?

java - 构建第一个应用程序教程按钮不起作用

android - 扫描后 ZXing 双像/叠加