从ListView中的EditText获取更新值

时间:2016-04-21 12:16:00

标签: java android listview android-edittext baseadapter

我有一个DialogFragment,其中有ListView,每个ListItem有三个TextView和一个EdiText,EditText允许整数值,可以根据用户要求进行更改。因此onClick对话框的正按钮我想将更新的Integer值发送给服务器。 在获取EditText的文本时,它会获取旧值而不是更新的值。

我写的代码

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

//对话框

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

//适配器

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

2 个答案:

答案 0 :(得分:1)

尝试将adapter.getView(i)替换为yourListView.getChildAt(i) 我想它会有所帮助。

答案 1 :(得分:0)

在适配器中添加文本更改侦听器,如

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

});

在正面按钮上点击从适配器获取值

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