如何将EditText值从RecyclerView适配器传递到Android中的活动

时间:2019-07-03 07:02:19

标签: android android-recyclerview android-edittext

I have added the page我有两个详细信息页面用于选择法院类型。在第一个详细信息页面中,我必须选中复选框并将值传递到下一个详细信息页面,因为我正在recyclerviewv中获取传递的值,其中包含textview和edittext。现在,我必须在edittext中输入每个法院的费率,并将值从适配器传递到必须在活动按钮单击中设置的活动。我无法传递edittext值。所以请帮我。

DetailsActivity:

 public class Details1Activity extends AppCompatActivity implements RecyclerViewItemInterface {

    RecyclerView rv, rt;
    private List<RegCompleteModel> details2Lists;
    private List<FacilityModel> lists;
    private ProgressDialog pDialog;
    APIService service;
    private FacilityAdapter adapter;
    List<Details1PageModel> list;
    private Button mNextButton;
    List<String> ls;
    ArrayList<String> mRateList;
    List<String> mIds;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_details1);

        Bundle extras = getIntent().getExtras();

        if (extras != null) {

            ls = new ArrayList<>();
            mIds = new ArrayList<>();
            ls = extras.getStringArrayList("types");
            mIds = extras.getStringArrayList("Ids");

            mRateList = new ArrayList<>();

            rt = findViewById(R.id.list_rateTypes);
            Details1Adapter dadapter = new Details1Adapter(ls,Details1Activity.this);
            rt.setLayoutManager(new LinearLayoutManager(Details1Activity.this, LinearLayoutManager.VERTICAL, true));
            rt.setAdapter(dadapter);
            Log.d("List", ls.toString());

        }

        pDialog = new ProgressDialog(Details1Activity.this);
        pDialog.setMessage("Please wait...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();

        service = Apicall.createService(APIService.class, this);

        Call<FacilityList> call = service.getFacilities();
        call.enqueue(new Callback<FacilityList>() {
            @Override
            public void onResponse(Call<FacilityList> call, Response<FacilityList> response) {

                if (response.isSuccessful()) {
                    pDialog.dismiss();
                    lists = response.body().getFacilities();
                    rv = findViewById(R.id.list_facilities);
                    adapter = new FacilityAdapter(lists, Details1Activity.this);
                    FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(Details1Activity.this);
                    layoutManager.setFlexDirection(FlexDirection.ROW);
                    rv.setLayoutManager(layoutManager);
                    rv.setAdapter(adapter);
                }

            }

            @Override
            public void onFailure(Call<FacilityList> call, Throwable t) {
                pDialog.dismiss();
                Toast.makeText(getApplicationContext(), t.getMessage(), Toast.LENGTH_LONG).show();
            }

        });

        mNextButton=findViewById(R.id.details1Next_button);
        mNextButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });
    }
}

DetailsAdapter:

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

     private Context cntx;
    private List<String> intent;
    private static ArrayList<String> rating;

    public Details1Adapter(List<String> intent Context cntx){
        this.intent=intent;
        this.cntx=cntx;

    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View v= LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_typeitems,viewGroup,false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder viewHolder,final int i) {

//        String typename = intent.get(i);
//            Log.d("Value", typename);
      //  Details2List mDetail=intent.get(i);

        viewHolder.rateType.setText(intent.get(i));

        viewHolder.editRate.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

                String rate=viewHolder.editRate.getText().toString();
                rating.add(rate);

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

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

    public class ViewHolder extends RecyclerView.ViewHolder{

        private TextView rateType,editRate;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);

            rateType=itemView.findViewById(R.id.rateText1);
            editRate=itemView.findViewById(R.id.rateText_edit);

        }
    }
}

2 个答案:

答案 0 :(得分:0)

使用界面解决此问题。

创建界面

interface MyListnear
{
   void addValue(String s);
}

adapter = new FacilityAdapter(lists, Details1Activity.this,new MyListnear(){

     public void addValue(String s){
     // Add value inside array
     }
});

并在adapter

中定义接口
MyListnear lisner;

public Details1Adapter(List<String> intent Context cntx,MyListnear lisner)
   {

        this.intent=intent;
        this.cntx=cntx;
        this.lisner = lisner;
    }

实施interface单击按钮

like btn.setOnclickListnear(new .....) 
{
   lisner.addValue(viewHolder.editRate.getText().tostring())
}

答案 1 :(得分:0)

first


=====

> public class Facility implements Parcelable {
>     Integer id;
>     String name;
> 
>     protected Facility(Parcel in) {
>         if (in.readByte() == 0) {
>             id = null;
>         } else {
>             id = in.readInt();
>         }
>         name = in.readString();
>     }
> 
>     public static final Creator<Facility> CREATOR = new Creator<Facility>() {
>         @Override
>         public Facility createFromParcel(Parcel in) {
>             return new Facility(in);
>         }
> 
>         @Override
>         public Facility[] newArray(int size) {
>             return new Facility[size];
>         }
>     };
> 
>     public Integer getId() {
>         return id;
>     }
> 
>     public void setId(Integer id) {
>         this.id = id;
>     }
> 
>     public String getName() {
>         return name;
>     }
> 
>     public void setName(String name) {
>         this.name = name;
>     }
> 
>     @Override
>     public int describeContents() {
>         return 0;
>     }
> 
>     @Override
>     public void writeToParcel(Parcel dest, int flags) {
>         if (id == null) {
>             dest.writeByte((byte) 0);
>         } else {
>             dest.writeByte((byte) 1);
>             dest.writeInt(id);
>         }
>         dest.writeString(name);
>     } }

=======================================


> public class SportzData implements Parcelable {
>     private String sevens;
>     private String fives;
>     private List<Facility> selectedFacilityList;
> 
>     public SportzData(String sevens, String fives, List<Facility> selectedFacilityList) {
>         this.sevens = sevens;
>         this.fives = fives;
>         this.selectedFacilityList = selectedFacilityList;
>     }
> 
>     protected SportzData(Parcel in) {
>         sevens = in.readString();
>         fives = in.readString();
>         selectedFacilityList = in.createTypedArrayList(Facility.CREATOR);
>     }
> 
>     public static final Creator<SportzData> CREATOR = new Creator<SportzData>() {
>         @Override
>         public SportzData createFromParcel(Parcel in) {
>             return new SportzData(in);
>         }
> 
>         @Override
>         public SportzData[] newArray(int size) {
>             return new SportzData[size];
>         }
>     };
> 
>     public String getSevens() {
>         return sevens;
>     }
> 
>     public void setSevens(String sevens) {
>         this.sevens = sevens;
>     }
> 
>     public String getFives() {
>         return fives;
>     }
> 
>     public void setFives(String fives) {
>         this.fives = fives;
>     }
> 
>     public List<Facility> getFacilityList() {
>         return selectedFacilityList;
>     }
> 
>     public void setFacilityList(List<Facility> facilityList) {
>         this.selectedFacilityList = facilityList;
>     }
> 
>     @Override
>     public int describeContents() {
>         return 0;
>     }
> 
>     @Override
>     public void writeToParcel(Parcel dest, int flags) {
>         dest.writeString(sevens);
>         dest.writeString(fives);
>         dest.writeTypedList(selectedFacilityList);
>     } }

================================================
and finally pass data from click on button


 Intent intent=new Intent(contacx,YourActivity.claas);

        intent.putExtra("Data",new SportzData(
                sevans.getText().toString(),
                five.getText().toString(),
                selectedArrayList));

===============================================

get data using intent

 Intent intent=getIntent();

        SportzData sportzData = intent.getParcelableExtra("Data");

        Log.d("<><>",sportzData.getFives());

        Log.d("<><>",sportzData.getSevens());

        List<Facility> facilityList = sportzData.getFacilityList();