简体   繁体   中英

Get updated value from EditText in a ListView

I have a DialogFragment in which there is ListView, each ListItem has three TextViews and one EdiText, EditText allows integer values which can be changed as per the user requirement. So onClick of positive button of dialog I want to send thoes updated Integer value to the server. While fetching EditText's text it fetches the old values not the updated one.

The code I have written

 View v;

    ArrayList<String> edValues = new ArrayList<String>();

    EditText et;

                            for (int i = 0; i < alObj.size(); i++) {
                                v = adapter.getView(i, null, null);
                                et = (EditText) v.findViewById(R.id.user_bill_edit);

//Dialog

public class CaseUserList extends DialogFragment  {


    private Context context;
    private String pipeSeparatedUserLists;
    Boolean isOperationEdit = false;
    String strCaseId = null;
    private UserBillRateAdapter adapter;
    ArrayList<Object> alObj;

    public Dialog onCreateDialog(Bundle savedInstanceState) {

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

        context = getActivity();
        strCaseId = getArguments().getString(CaseConstants.TABLE_CASE_LIST_CASE_ID);
        //isOperationEdit = getArguments().getBoolean("isEdit");

        //Send network request to get the list
        new userBillRateList().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);

        //Prepare the list layout
        View view = getActivity().getLayoutInflater().inflate(R.layout.layout_case_user_list, null);
        builder .setView(view)
                .setPositiveButton("Update", new Dialog.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int id) {
                        View v;
                        ArrayList<String> edValues = new ArrayList<String>();
                        EditText et;

                        for (int i = 0; i < alObj.size(); i++) {
                            v = adapter.getView(i, null, null);
                            et = (EditText) v.findViewById(R.id.user_bill_edit);
                            edValues.add(et.getText().toString());
                        }
                    }
                })

                .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        CaseUserList.this.getDialog().cancel();
                    }
                });
        return builder.create();
    }

    /**
     * Async task to fetch list of all possible Witness
     */
    private class userBillRateList extends AsyncTask<Void, Void, ArrayList<Object>> {

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

        @Override
        protected ArrayList<Object> doInBackground(Void... voids) {
            Network billRateList = new Network(getActivity(),CaseConstants.SERVICE_CASE_USER_BILL_RATE
                    + Constants.CONSTANT_CONTACT_ID + ","
                    + strCaseId);

            return HelperFunctions.convertJsonToDataModel(billRateList.getJsa_Output(),
                    UserBillingRateModel.class);
        }

        @Override
        protected void onPostExecute(final ArrayList<Object> arrayList) {
            super.onPostExecute(arrayList);
            alObj = arrayList;
            ListView allListsView = (ListView) getDialog().findViewById(R.id.list_view_case_user_list);

            if(arrayList.isEmpty()) {
                ((TextView) getDialog().findViewById(R.id.user_list_empty_text_view)).setVisibility(View.VISIBLE);
            } else {
                ((EditText)getDialog().findViewById(R.id.user_list_search)).setVisibility(View.VISIBLE);
                adapter = new UserBillRateAdapter(getActivity(),arrayList);

                allListsView.setAdapter(adapter);
                allListsView.setTextFilterEnabled(true);
            }
            getDialog().show();
        }
    }
}
                                    edValues.add(et.getText().toString());
                                }

//Adapter

public class UserBillRateAdapter extends BaseAdapter {
    /**
     *  The following variable contains the File list in the CloudFileList bean format.
     */
    private ArrayList<Object> al_List;
    /**
     * The context of the activity being worked on
     */
    private Context context;
    ViewHolder viewContainer = null;
    ArrayList<Object> alDataObj;

    public UserBillRateAdapter(Context context, ArrayList<Object> al_List){
        this.al_List = al_List;
        this.context = context;
        alDataObj = new ArrayList<>();
    }

    @Override
    public int getCount() {
        if(al_List != null)
            return al_List.size();
        return 0;
    }

    @Override
    public Object getItem(int position) {
        return al_List.get(position);
    }

    @Override
    public long getItemId(int position) {
        return Long.valueOf(((UserBillingRateModel) al_List.get(position)).ContactId);
    }

    @Override
    public View getView(int position,View viewConvert, ViewGroup parent) {
        LayoutInflater mInflater = (LayoutInflater)
                context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        viewContainer = null;
        if(viewConvert == null) {
            viewConvert = mInflater.inflate(R.layout.layout_user_bill_list, null);
            viewContainer = new ViewHolder();
            viewContainer.txtUserName = (TextView) viewConvert.findViewById(R.id.user_name_txt);
            viewContainer.txtUserRole = (TextView) viewConvert.findViewById(R.id.user_role);
            viewContainer.txtUserRate = (TextView) viewConvert.findViewById(R.id.user_bill_txt);
            viewContainer.edUserRate = (EditText) viewConvert.findViewById(R.id.user_bill_edit);
            viewConvert.setTag(viewContainer);
        } else
            viewContainer = (ViewHolder) viewConvert.getTag();

        final UserBillingRateModel beanListItem = (UserBillingRateModel) al_List.get(position);
        viewContainer.txtUserName.setText(beanListItem.FullName);
        viewContainer.txtUserRate.setText(beanListItem.BillingRate);
        viewContainer.txtUserRole.setText(beanListItem.RoleName);
        viewContainer.edUserRate.setText(beanListItem.BillingRate);
        return viewConvert;
    }

    public class ViewHolder{
        TextView txtUserName;
        TextView txtUserRole;
        TextView txtUserRate;
        EditText edUserRate;
    }
}

Try to replace adapter.getView(i) with yourListView.getChildAt(i) .
I suppose it will help.

In your adapter add text change listener like

EditText searchTo = (EditText)findViewById(R.id.medittext);
searchTo.addTextChangedListener(new TextWatcher() {

    @Override
    public void afterTextChanged(Editable s) {
        // TODO Auto-generated method stub

    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        // TODO Auto-generated method stub

    }

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

        //store value in your POJO class
        // like getItem(position).setUserEnterValue(s.toString());
    } 

});

on positive button click get value from adapter like

 for (int i = 0; i < alObj.size(); i++) {
   edValues.add(list.getAdapter().getItem(i).getUserEnterValue());
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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