在编辑Edittext

时间:2019-02-05 14:45:13

标签: android android-recyclerview

...     我正在做一个项目。我正在从服务器获取物品清单,并显示在recyclerview上。     到目前为止一切都很好,我在每个项目中都有一个edittext。每当我在edittext中进行编辑时,它都会在不同位置的多个edittext中自动更改(值)。请帮助我解决这个问题。

I 

//This is adapter

public class SampleListAdapter extends RecyclerView.Adapter<SampleListAdapter.MySampleHolder> {
    private Context context;
    private View mView;
    private List<Brands> brandsList;
    private List<InputOrders> sampleListSelected;
    public PreferenceConnector connector;
    private InputOrders orders;
    private DataItem dataItemForUpdate;
    private static final String NULL_STRING = "";
    private static final String SAMPLE_PRODUCT = "1";




    public SampleListAdapter(){}

    public SampleListAdapter(Context context, List<Brands> brandsList, DataItem dataItemForUpdate) {
        this.context = context;
        this.brandsList = brandsList;
        this.dataItemForUpdate = dataItemForUpdate;

    }

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

        mView = LayoutInflater.from(context).inflate(R.layout.item_sample_view, parent, false);
        return new MySampleHolder(mView);
    }

    @Override
    public void onBindViewHolder(@NonNull MySampleHolder holder, int position) {
        sampleListSelected = new ArrayList<>();
        connector = new PreferenceConnector(context);
        bindItemsWithView(holder);
    }



    @Override
    public int getItemCount() {
        if (brandsList.size() > 0) {
            return brandsList.size();
        } else {
            return 0;
        }
    }


    private void bindItemsWithView(MySampleHolder holder) {

        if(brandsList.size()!=0) {
            if (brandsList.get(holder.getAdapterPosition()).getIsBrand().equals(AppConstants.BRAND + NULL_STRING)) {
                holder.textBrandSampleName.setText(brandsList.get(holder.getAdapterPosition()).getName());

            }

    }



    public List<InputOrders> getSampleListSelected() {
        return DataController.getmInstance().getSampleListSelected();
    }

    public void setSampleListSelected(List<InputOrders> sampleListSelected) {
        DataController.getmInstance().setSampleListSelected(sampleListSelected);
    }

    class MySampleHolder extends RecyclerView.ViewHolder {
        private TextView textBrandSampleName;
        private EditText editBrandSampleValue;

        MySampleHolder(View itemView) {
            super(itemView);

            textBrandSampleName = itemView.findViewById(R.id.text_brand_sampel_name);
            editBrandSampleValue = itemView.findViewById(R.id.edit_brand_sample_value);
        }

    }

    public interface OnItemFoundListener{
        void onItemFound(List<InputOrders> listOfOrders);
    }
}



//This is Activity.


public class ProductCategoryActivity extends BaseActivity implements View.OnClickListener {
    private String INPUT_ID = null;
    private DoctorTabsPagerAdapter doctorTabsPagerAdapter;
    private ChemistTabsPagerAdapter chemistTabsPagerAdapter;
    private FragmentManager fragmentManager;
    private TabLayout tabLayout;
    private ViewPager pager;
    private Toolbar toolbarProduct;
    private Button buttonConfirm;
    private Input input;
    DataReceivedListener listener;
    private boolean isDoctor;
    public static final String TAG = ProductCategoryActivity.class.getSimpleName();
    private boolean isUpdateInput;
    private DataItem dataForUpdate;

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_product_category);
        initUi();
        initData();
    }

    @Override
    public void initData() {
        super.initData();
        isDoctor = connector.getBoolean(AppConstants.KEY_ROLE);
        fragmentManager = getSupportFragmentManager();
        String getInput = getIntent().getStringExtra(AppConstants.PASS_INPUT);
        input = new Gson().fromJson(getInput, Input.class);
        isUpdateInput = getIntent().getBooleanExtra(AppConstants.IS_INPUT_UPDATE, false);


        if (isUpdateInput) {
            String strData = getIntent().getStringExtra(AppConstants.PASS_UPDATE_INPUT);
            dataForUpdate = new Gson().fromJson(strData, DataItem.class);
            Bundle bundle = new Bundle();
            bundle.putString(AppConstants.PASS_UPDATE_INPUT, strData);

            if (dataForUpdate.getChemistsId().equals("0")) {
                doctorTabsPagerAdapter = new DoctorTabsPagerAdapter(fragmentManager, ProductCategoryActivity.this, bundle);
                pager.setAdapter(doctorTabsPagerAdapter);
            } else {
                chemistTabsPagerAdapter = new ChemistTabsPagerAdapter(fragmentManager, ProductCategoryActivity.this, bundle);
                pager.setAdapter(chemistTabsPagerAdapter);
            }

            tabLayout.setupWithViewPager(pager);
        } else {

            if (isDoctor) {
                doctorTabsPagerAdapter = new DoctorTabsPagerAdapter(fragmentManager, ProductCategoryActivity.this);
                pager.setAdapter(doctorTabsPagerAdapter);
            } else {
                chemistTabsPagerAdapter = new ChemistTabsPagerAdapter(fragmentManager, ProductCategoryActivity.this);
                pager.setAdapter(chemistTabsPagerAdapter);
            }

            tabLayout.setupWithViewPager(pager);
        }

    }

    @Override
    public void initUi() {
        super.initUi();

        toolbarProduct = findViewById(R.id.toolbarProduct);
        buttonConfirm = findViewById(R.id.button_confirm_product);
        tabLayout = findViewById(R.id.tabLayout);
        pager = findViewById(R.id.swiper);
        setSupportActionBar(toolbarProduct);
        toolbarProduct.setNavigationIcon(R.drawable.back_arrow_whit);

        toolbarProduct.setNavigationOnClickListener(v -> onBackPressed());
        buttonConfirm.setOnClickListener(this);


    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button_confirm_product:
                if (isUpdateInput){
                    input.setGiftDetails(new GiftsAdapter().getInputGiftList());
                    if (new PODAdapter().getPOBOrdersList() != null){
                        input.setProductDetalis(new PODAdapter().getPOBOrdersList());
                        for( int i=0; i<input.getProductDetalis().size(); i++)  {
                            String inputId = input.getInputId();
                            input.getProductDetalis().get(i).setInputId(inputId);
                        }
                    }
                    if (new SampleListAdapter().getSampleListSelected()!= null){
                        input.setProductDetalis(new SampleListAdapter().getSampleListSelected());
                        for( int i=0; i<input.getProductDetalis().size(); i++)  {
                            String inputId = input.getInputId();
                            input.getProductDetalis().get(i).setInputId(inputId);
                        }
                    }
                    apiUpdateMrInput(token, input);

                }
                else if (InternetConnection.isNetworkAvailable(ProductCategoryActivity.this)) {
                    addInputApi(token, input);
                } else {
                    Snackbar.make(rootView, getString(R.string.no_internet), Snackbar.LENGTH_SHORT).show();
                }

                break;
        }

    }


    public void addInputGiftApi(String token, List<InputGift> inputGifts) {
        processDialog.showDialog(ProductCategoryActivity.this, false);
        Call<InputGiftResponce> call = apiInterface.addInputGift(token, inputGifts);
        call.enqueue(new Callback<InputGiftResponce>() {
            @Override
            public void onResponse(@NonNull Call<InputGiftResponce> call, @NonNull Response<InputGiftResponce> response) {
                processDialog.dismissDialog();
                if (response.isSuccessful()) {
                    if (response.body() != null) {
                        if (Objects.requireNonNull(response.body()).getStatusCode() == AppConstants.RESULT_OK) {
                            // Set Array Lis to null
                            //----------
                            new GiftsAdapter().setInputGiftList(null);
                            DataController.getmInstance().setInputGiftList(null);


                            Intent startMainActivity = new Intent(ProductCategoryActivity.this, MainActivity.class);
                            startActivity(startMainActivity);

                            Toast.makeText(ProductCategoryActivity.this, Objects.requireNonNull(response.body()).getMessage(), Snackbar.LENGTH_LONG).show();
                        } else {
                            Toast.makeText(ProductCategoryActivity.this, Objects.requireNonNull(response.body()).getMessage(), Snackbar.LENGTH_LONG).show();

                        }
                    }
                }
            }

            @Override
            public void onFailure(Call<InputGiftResponce> call, Throwable t) {
                processDialog.dismissDialog();
                Snackbar.make(rootView, t.getMessage(), Snackbar.LENGTH_LONG).show();

            }
        });
    }


    public void apiAddInputSamples(String token, List<InputOrders> inputOrders) {


        processDialog.showDialog(ProductCategoryActivity.this, false);
        Call<MainResponse> call = apiInterface.addInputProductList(token, inputOrders);

        call.enqueue(new Callback<MainResponse>() {
            @Override
            public void onRespon`enter code here`se(@NonNull Call<MainResponse> call, Response<MainResponse> response) {
                processDialog.dismissDialog();
                if (response.body().getStatusCode() == AppConstants.RESULT_OK) {
                    Snackbar.make(rootView, response.body().getMessage(), Snackbar.LENGTH_LONG).show();
                    // Set Array Lis to null
                    //----------
                    new SampleListAdapter().setSampleListSelected(null);
                    DataController.getmInstance().setOrderListSelected(null);

                    // On Response of Input Brand Calling add input Gift Api
                    //---------------
                    Intent startMainActivity = new Intent(ProductCategoryActivity.this, MainActivity.class);
                    startActivity(startMainActivity);

                } else {
                    Snackbar.make(rootView, response.body().getMessage(), Snackbar.LENGTH_LONG).show();
                }
            }

            @Override
            public void onFailure(@NonNull Call<MainResponse> call, @NonNull Throwable t) {
                processDialog.dismissDialog();
                Snackbar.make(rootView, t.getMessage(), Snackbar.LENGTH_LONG).show();
            }
        });
    }



    public void apiUpdateMrInput(String token, Input input){
        processDialog.showDialog(ProductCategoryActivity.this, false);
        Call<MainResponse> call = apiInterface.updateMrInput(token, input);
        call.enqueue(new Callback<MainResponse>() {
            @Override
            public void onResponse(@NonNull Call<MainResponse> call, @NonNull Response<MainResponse> response) {
                processDialog.dismissDialog();
                if (response.isSuccessful()){
                    if (Objects.requireNonNull(response.body()).getStatusCode() == AppConstants.RESULT_OK){
                        Toasty.success(ProductCategoryActivity.this, Objects.requireNonNull(response.body()).getMessage(), Toast.LENGTH_LONG).show();
                        new SampleListAdapter().setSampleListSelected(null);
                        new PODAdapter().setPOBOrdersList(null);
                        new GiftsAdapter().setInputGiftList(null);
                        Intent inUserReportList = new Intent(ProductCategoryActivity.this, SearchDateWiseInputActivity.class);
                        inUserReportList.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                        startActivity(inUserReportList);
                        finish();
                    }
                    else{
                        Toasty.error(ProductCategoryActivity.this, Objects.requireNonNull(response.body()).getMessage(), Toast.LENGTH_LONG).show();
                    }
                }
            }

            @Override
            public void onFailure(@NonNull Call<MainResponse> call, @NonNull Throwable t) {
                processDialog.dismissDialog();
                Toasty.error(ProductCategoryActivity.this, t.getMessage(), Toast.LENGTH_SHORT).show();

            }
        });
    }


    public void apiAddInputBrands(String token, List<InputOrders> inputOrders) {
        processDialog.showDialog(ProductCategoryActivity.this, false);
        Call<MainResponse> call = apiInterface.addInputProductList(token, inputOrders);

        call.enqueue(new Callback<MainResponse>() {
            @Override
            public void onResponse(@NonNull Call<MainResponse> call, @NonNull Response<MainResponse> response) {
                processDialog.dismissDialog();
                if (response.body().getStatusCode() == AppConstants.RESULT_OK) {
                    Snackbar.make(rootView, response.body().getMessage(), Snackbar.LENGTH_LONG).show();
                    // Set Array List to null
                    //----------
                    new BrandsAdapter().setOrderListSelected(null);
                    DataController.getmInstance().setOrderListSelected(null);

                    Intent startMainActivity = new Intent(ProductCategoryActivity.this, MainActivity.class);
                    startActivity(startMainActivity);
                    //---------------

                } else {
                    Snackbar.make(rootView, response.body().getMessage(), Snackbar.LENGTH_LONG).show();
                }
            }

            @Override
            public void onFailure(Call<MainResponse> call, Throwable t) {
                processDialog.dismissDialog();
                Snackbar.make(rootView, t.getMessage(), Snackbar.LENGTH_LONG).show();
            }
        });
    }


    public void callingAllApi() {

        if (isDoctor) {

                if (new BrandsAdapter().getOrderListSelected() != null) {
                    List<InputOrders> inputB = new BrandsAdapter().getOrderListSelected();
                    if (inputB.size() > 0) {
                        for (int i = 0; i < inputB.size(); i++) {
                            inputB.get(i).setInputId(INPUT_ID);
                        }
                        apiAddInputBrands(token, inputB);
                    }
                }


                if (new GiftsAdapter().getInputGiftList() != null) {
                    List<InputGift> inputGifts = new GiftsAdapter().getInputGiftList();
                    if (inputGifts.size() > 0) {
                        for (int i = 0; i < inputGifts.size(); i++) {
                            inputGifts.get(i).setInputId(INPUT_ID);
                        }
                        addInputGiftApi(token, inputGifts);
                    }
                }

                if (new SampleListAdapter().getSampleListSelected()!= null) {
                    List<InputOrders> inputSamples = new SampleListAdapter().getSampleListSelected();
                    if (inputSamples.size() > 0) {
                        for (int i = 0; i < inputSamples.size(); i++) {
                            inputSamples.get(i).setInputId(INPUT_ID);
                        }
                        apiAddInputSamples(token, inputSamples);
                    }
                }

        }
        else{
            if (new PODAdapter().getPOBOrdersList() != null) {
                List<InputOrders> inputOrders = new PODAdapter().getPOBOrdersList();
                if (inputOrders.size() > 0) {
                    for (int i = 0; i < inputOrders.size(); i++) {
                        inputOrders.get(i).setInputId(INPUT_ID);
                    }
                    apiAddInputBrands(token, inputOrders);
                }
            }
        }
    }

    public void addInputApi(String token, Input input) {
        processDialog.showDialog(ProductCategoryActivity.this, false);
        Call<InputResponce> call = apiInterface.addInput(token, input);
        call.enqueue(new Callback<InputResponce>() {
            @Override
            public void onResponse(@NonNull Call<InputResponce> call, @NonNull Response<InputResponce> response) {
                processDialog.dismissDialog();
                if (response.isSuccessful()) {

                    if (response.code() == 200) {
                        if (Objects.requireNonNull(response.body()).getStatusCode() == AppConstants.RESULT_OK) {
                            // Calling all api, product, gift, and Pod
                            INPUT_ID = response.body().getData().getInputId();
                            callingAllApi();
                        } else {
                            Snackbar.make(rootView, response.body().getMessage(), Snackbar.LENGTH_INDEFINITE).show();

                        }
                    }
                }
            }

            @Override
            public void onFailure(@NonNull Call<InputResponce> call, @NonNull Throwable t) {
                processDialog.dismissDialog();
                Snackbar.make(rootView, t.getMessage(), Snackbar.LENGTH_INDEFINITE).show();

            }
        });
    }


    public void setDataReceivedListener(DataReceivedListener listener) {
        this.listener = listener;
    }

    public interface DataReceivedListener {
        void onReceived();
    }


}

1 个答案:

答案 0 :(得分:0)

我认为您可以尝试通过Android适配器使用setHasStableIds方法。它指示是否可以使用Long类型的唯一标识符表示数据集中的每个项目。因此,在构建适配器时,只需添加

    init { setHasStableIds(true) }