Android从自定义列表视图

时间:2015-12-29 08:33:17

标签: android listview

我是Android新手。我在listview组件中填充数据,之后也进行一些编辑,然后从适配器edittext数据中获取。我得到的是它以前的数据不是当前编辑的数据。

我的Java代码是,

save.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Handler handler = new Handler();
            final Runnable r = new Runnable() {
                public void run() {
                    Cursor c = db.getData();
                    List<ClientDetail> arrayList = new ArrayList<ClientDetail>();
                    while (c.moveToNext()) {
                        ClientDetail cd = new ClientDetail();
                        cd.setContactID(c.getString(1));
                        cd.setContactname(c.getString(2));
                        cd.setRefCompanyId_EntityMaster(c.getString(3));
                        arrayList.add(cd);
                    }
                    List<Worklog> wl = RSMIT.getInstance().getWorklogList();
                    List<Worklog> wolo = new ArrayList<Worklog>();
                    View v;
                    EditText et;
                    Worklog worklog = null;

                    for (int i = 0; i < listView.getCount(); i++) {
                        v = listView.getAdapter().getView(i, null, listView);
                        et = (EditText) v.findViewById(R.id.txtwldes);
                        chkwl = (CheckBox) v.findViewById(R.id.chkwl);
                        spinner = (NiceSpinner) v.findViewById(R.id.spinner);
                        spinner1 = (NiceSpinner) v.findViewById(R.id.spinner1);
                        worklog = new Worklog();
                        if (wl.get(i).getIsTaskEnable() == null) {
                            worklog.setIsTaskEnable(null);
                        } else {
                            worklog.setIsTaskEnable(wl.get(i).getIsTaskEnable());
                        }

                        if (wl.get(i).getTaskStatusId() == null) {
                            worklog.setTaskStatusId(null);
                        } else {
                            worklog.setTaskStatusId(wl.get(i).getTaskStatusId());
                        }

                        worklog.setIsWorkLogEnable(wl.get(i).getIsWorkLogEnable());
                        worklog.setColor(wl.get(i).getColor());
                        worklog.setIsCheckTickTicked(wl.get(i).getIsCheckTickTicked());
                        worklog.setTaskType(wl.get(i).getTaskType());
                        if (et.isEnabled() == true) {
                            worklog.setWorklogDescription(et.getText().toString().trim());
                        } else {
                            worklog.setWorklogDescription("");
                        }

                        Log.e("edt txt", et.getText().toString());
                        if (chkwl.isChecked()) {
                            worklog.setIsCopy("true");
                        } else {
                            worklog.setIsCopy("false");
                        }
                        for (int j = 0; j < arrayList.size(); j++) {
                            if (spinner1.getItem(0).equalsIgnoreCase("Select")) {
                                worklog.setClientID(null);
                            } else {
                                worklog.setClientID(spinner1.getItem(0));
                            }
                        }

                        if (wl.get(i).getTimeTrack().equalsIgnoreCase("09:00-09:30")) {
                            worklog.setTimeTrack("chkbox0900");
                        } else if (wl.get(i).getTimeTrack().equalsIgnoreCase("09:30-10:00")) {
                            worklog.setTimeTrack("chkbox0930");
                        } else if (wl.get(i).getTimeTrack().equalsIgnoreCase("10:00-10:30")) {
                            worklog.setTimeTrack("chkbox1000");
                        } else if (wl.get(i).getTimeTrack().equalsIgnoreCase("10:30-11:00")) {
                            worklog.setTimeTrack("chkbox1030");
                        } else if (wl.get(i).getTimeTrack().equalsIgnoreCase("11:00-11:30")) {
                            worklog.setTimeTrack("chkbox1100");
                        } else if (wl.get(i).getTimeTrack().equalsIgnoreCase("11:30-12:00")) {
                            worklog.setTimeTrack("chkbox1130");
                        } else if (wl.get(i).getTimeTrack().equalsIgnoreCase("12:00-12:30")) {
                            worklog.setTimeTrack("chkbox1200");
                        } else if (wl.get(i).getTimeTrack().equalsIgnoreCase("12:30-13:00")) {
                            worklog.setTimeTrack("chkbox1230");
                        } else if (wl.get(i).getTimeTrack().equalsIgnoreCase("13:00-13:30")) {
                            worklog.setTimeTrack("chkbox0100");
                        } else if (wl.get(i).getTimeTrack().equalsIgnoreCase("13:30-14:00")) {
                            worklog.setTimeTrack("chkbox0130");
                        } else if (wl.get(i).getTimeTrack().equalsIgnoreCase("14:00-14:30")) {
                            worklog.setTimeTrack("chkbox0200");
                        } else if (wl.get(i).getTimeTrack().equalsIgnoreCase("14:30-15:00")) {
                            worklog.setTimeTrack("chkbox0230");
                        } else if (wl.get(i).getTimeTrack().equalsIgnoreCase("15:00-15:30")) {
                            worklog.setTimeTrack("chkbox0300");
                        } else if (wl.get(i).getTimeTrack().equalsIgnoreCase("15:30-16:00")) {
                            worklog.setTimeTrack("chkbox0330");
                        } else if (wl.get(i).getTimeTrack().equalsIgnoreCase("16:00-16:30")) {
                            worklog.setTimeTrack("chkbox0400");
                        } else if (wl.get(i).getTimeTrack().equalsIgnoreCase("16:30-17:00")) {
                            worklog.setTimeTrack("chkbox0430");
                        } else if (wl.get(i).getTimeTrack().equalsIgnoreCase("17:00-17:30")) {
                            worklog.setTimeTrack("chkbox0500");
                        } else if (wl.get(i).getTimeTrack().equalsIgnoreCase("17:30-18:00")) {
                            worklog.setTimeTrack("chkbox0530");
                        } else if (wl.get(i).getTimeTrack().equalsIgnoreCase("18:00-18:30")) {
                            worklog.setTimeTrack("chkbox0600");
                        } else if (wl.get(i).getTimeTrack().equalsIgnoreCase("18:30-19:00")) {
                            worklog.setTimeTrack("chkbox0630");
                        } else if (wl.get(i).getTimeTrack().equalsIgnoreCase("19:00-19:30")) {
                            worklog.setTimeTrack("chkbox0700");
                        } else if (wl.get(i).getTimeTrack().equalsIgnoreCase("19:30-20:00")) {
                            worklog.setTimeTrack("chkbox0730");
                        } else if (wl.get(i).getTimeTrack().equalsIgnoreCase("20:00-20:30")) {
                            worklog.setTimeTrack("chkbox0800");
                        } else if (wl.get(i).getTimeTrack().equalsIgnoreCase("20:30-21:00")) {
                            worklog.setTimeTrack("chkbox0830");
                        }
                        wolo.add(worklog);
                    }


                    Gson gson = new Gson();
                    JsonElement element = gson.toJsonTree(wolo,
                            new TypeToken<List<Worklog>>() {
                            }.getType());

                    Log.e("Element Value", element.getAsJsonArray().toString());
                }
            };
            handler.postDelayed(r, 1000);
        }
    });

所以,我的问题是如何从listview edittext中获取编辑后的值?谢谢。

2 个答案:

答案 0 :(得分:1)

  

您应该使用TextWatcher并在文本更改后使用方法更改文本之前请检查此链接http://developer.android.com/reference/android/text/TextWatcher.html

     

这是示例代码,仅为您提供指导。但请先阅读文档。

//suppose this is your edit text 
EditText et;
//addTextChange listener
et.addTextChangedListener(editTextTextWatcher);

private TextWatcher editTextTextWatcher = 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) {

    }

    @Override
    public void afterTextChanged(Editable s) {

        //Write your code to handel text changes here
    }
};

答案 1 :(得分:0)

试试这个:

EG:

ObjectEntity.class:

public class ObjectEntity {
String name;
EditText editText;

public ObjectEntity() {
}

public ObjectEntity(String name, EditText editText) {
    this.name = name;
    this.editText = editText;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public EditText getEditText() {
    return editText;
}

public void setEditText(EditText editText) {
    this.editText = editText;
}}

在适配器中:

 ArrayList<ObjectEntity>arrayList= new ArrayList<ObjectEntity>();
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        LayoutInflater inflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (convertView == null) {

            convertView = inflater.inflate(R.layout.item_listview, parent,
                    false);
        }
        EditText mEdt=(EditText )convertView.findViewById(R.id.mEdext);
        arrayList.get(position).setEditText(mEdt);

        return convertView;

    }

在代码活动中:

 save.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            for (int i = 0; i < arrayList.size(); i++) {
                EditText editText = arrayList.get(i).getEditText();

                String edtContent = editText.getText().toString();
                //check vule here

            }

        }
    });