在Custom ListView中的EditText中保留和检索值

时间:2012-08-03 09:10:53

标签: android android-listview

我正在尝试创建一个自定义列表中连续ListActivityCheckBoxTextView的{​​{1}}。

我需要在EditText中获取用户的输入,并在选择该行的EditText时将该值保存在数据库中。如果未选择CheckBox,则会禁用CheckBox。后者我可以做,但我无法保留EditText的值并将其存储在数据库中。

目前,如果我在EditText中输入任何内容并按回按键以最小化键盘,则EditText会恢复到之前的值。

我已经编码了以下内容,直到现在才显示自定义EditText并选中和取消选中该复选框。我只需要帮助ListView部分。

EditExepense.Java

EditText

InteractiveArrayAdapter.Java

package h.pkg.main;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class EditExpense extends ListActivity {
    int eid, tid, mid;
    String etyp,amount;
    float eamt;
    DBAdapter db;
    ArrayList<Integer> memmid = new ArrayList<Integer>();
    ArrayList<String> memname = new ArrayList<String>();
    ArrayList<Float> memcexp = new ArrayList<Float>();  
    List<Model> list = new ArrayList<Model>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Button Save = (Button)findViewById(R.id.Save);
        //ListView l1= (ListView)findViewById(R.id.listView1);
        setContentView(R.layout.eexp);
        Bundle idata = getIntent().getExtras();
        db = new DBAdapter(this);

        if (idata !=null)
        {
            eid=idata.getInt("EID");
            tid=idata.getInt("TID");
            etyp=idata.getString("ETYP");
            eamt=idata.getFloat("EXAMT");
            amount = "" + eamt;
        }

        try {
            db.open();
            Cursor c = db.getAllmem(tid);
            float nofm = (float)(c.getCount());
            float evenshareamt = eamt/nofm;
            db.close();

            if (c != null ) {
                if  (c.moveToFirst()) {
                    do {
                        String MN = c.getString(c.getColumnIndex("NAME"));
                        mid = c.getInt(c.getColumnIndex("MID"));
                        String ceamts = String.valueOf(evenshareamt);

                        list.add(get(MN,ceamts,true));

                        memname.add(MN);
                        memmid.add(mid);
                        memcexp.add(evenshareamt);
                    } while (c.moveToNext());
                } 
            }

            c.close();
        } finally {
        }

        ArrayAdapter<Model> adapter = new InteractiveArrayAdapter(this,list);
        setListAdapter(adapter);

        RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioGroup1);
        RadioButton rbs = (RadioButton) findViewById(R.id.Shared);
        RadioButton rbc = (RadioButton) findViewById(R.id.Custom);

        radioGroup.setOnCheckedChangeListener( new OnCheckedChangeListener() {
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                RadioButton rbs = (RadioButton) findViewById(R.id.Shared);

                if (rbs.isChecked()) {
                    //DisplayToast("Option 1 checked!");
                } else {
                    //DisplayToast("Option 2 checked!");
                }
            }
        });
    }    

    public void expUpdate(View V) {
        setResult(RESULT_OK);
        finish();
    }

    private Model get(String memn, String ceam, boolean sel) {
        return new Model(memn,ceam,sel);
    }
}

Model.Java

package h.pkg.main;
import java.util.List;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;

public class InteractiveArrayAdapter extends ArrayAdapter<Model> {
    private final List<Model> list;
    private final Activity context;

    public InteractiveArrayAdapter(Activity context, List<Model> list) {
        super(context, R.layout.listview, list);
        this.context = context;
        this.list = list;
    }

    static class ViewHolder {
        protected TextView memnames;
        protected EditText ceamount;
        protected CheckBox checkbox;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = null;
        if (convertView == null) {
            LayoutInflater inflator = context.getLayoutInflater();
            view = inflator.inflate(R.layout.listview, null);
            final ViewHolder viewHolder = new ViewHolder();
            viewHolder.memnames = (TextView) view.findViewById(R.id.memname1);
            viewHolder.ceamount = (EditText) view.findViewById(R.id.expamt1);
            viewHolder.checkbox = (CheckBox) view.findViewById(R.id.chkbox1);
            viewHolder.checkbox.setOnCheckedChangeListener( new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView,
                                                            boolean isChecked) {
                    Model elementc = (Model) viewHolder.checkbox.getTag();
                    elementc.setSelected(buttonView.isChecked());
                    boolean edenable = buttonView.isChecked();
                    if(edenable)
                    {
                        viewHolder.ceamount.setEnabled(true);
                    } else {
                        viewHolder.ceamount.setText("");
                        viewHolder.ceamount.setEnabled(false);
                    }
                }
            });

            view.setTag(viewHolder);
            viewHolder.checkbox.setTag(list.get(position));
        } else {
            view = convertView;
            ((ViewHolder) view.getTag()).checkbox.setTag(list.get(position));
        }

        ViewHolder holder = (ViewHolder) view.getTag();
        holder.memnames.setText(list.get(position).getName());
        holder.ceamount.setText(list.get(position).getCeamt());
        holder.checkbox.setChecked(list.get(position).isSelected());
        return view;
    }
}

3 个答案:

答案 0 :(得分:2)

嗨所有感谢答案..但是因为我已经使用了Model类...我试图通过该示例本身完成任务...请参阅修改后的InteractiveArrayAdapter.Java这样可以保留值编辑后正确行中的edittext。

我刚刚将此方法添加到我的InterativeArrayAdapter.java

viewHolder.ceamount.setOnFocusChangeListener(new OnFocusChangeListener() {

            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                // TODO Auto-generated method stub
                 if (!hasFocus){
                     viewHolder.ceamount.setFocusable(true);
                     Model elemente = (Model) viewHolder.ceamount.getTag();
                     final EditText custamt = (EditText) v;
                     elemente.setCeamt(custamt.getText().toString());
                }
            }
        });

请参阅下面的完整代码..

package h.pkg.main;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;

public class InteractiveArrayAdapter extends ArrayAdapter<Model> {

    private final List<Model> list;
    private final Activity context;

    public InteractiveArrayAdapter(Activity context, List<Model> list) {
        super(context, R.layout.listview, list);
        this.context = context;
        this.list = list;
    }

    static class ViewHolder {
        protected TextView memnames;
        protected EditText ceamount;
        protected CheckBox checkbox;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = null;
        if (convertView == null) {
            LayoutInflater inflator = context.getLayoutInflater();
            view = inflator.inflate(R.layout.listview, null);
            final ViewHolder viewHolder = new ViewHolder();
            viewHolder.memnames = (TextView) view.findViewById(R.id.memname1);
            viewHolder.ceamount = (EditText) view.findViewById(R.id.expamt1);
            viewHolder.checkbox = (CheckBox) view.findViewById(R.id.chkbox1);
            viewHolder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                        @Override
                        public void onCheckedChanged(CompoundButton buttonView,
                                boolean isChecked) {
                            Model elementc = (Model) viewHolder.checkbox.getTag();
                            elementc.setSelected(buttonView.isChecked());
                            boolean edenable = buttonView.isChecked();
                            if(edenable)
                            {
                            viewHolder.ceamount.setEnabled(true);
                            }
                            else
                            {
                                viewHolder.ceamount.setText("");
                                viewHolder.ceamount.setEnabled(false);
                            }


                        }
                    });

            viewHolder.ceamount.setOnFocusChangeListener(new OnFocusChangeListener() {

                @Override
                public void onFocusChange(View v, boolean hasFocus) {
                    // TODO Auto-generated method stub
                     if (!hasFocus){
                         viewHolder.ceamount.setFocusable(true);
                         Model elemente = (Model) viewHolder.ceamount.getTag();
                         final EditText custamt = (EditText) v;
                         elemente.setCeamt(custamt.getText().toString());
                    }
                }
            });
            view.setTag(viewHolder);
            viewHolder.memnames.setTag(list.get(position));
            //viewHolder.memnames.setId(position);
            viewHolder.checkbox.setTag(list.get(position));
            //viewHolder.checkbox.setId(position);
            viewHolder.ceamount.setTag(list.get(position));
            //viewHolder.ceamount.setId(position);
        } else {
            view = convertView;
            ((ViewHolder) view.getTag()).checkbox.setTag(list.get(position));
            ((ViewHolder) view.getTag()).memnames.setTag(list.get(position));
            ((ViewHolder) view.getTag()).ceamount.setTag(list.get(position));
        }

        ViewHolder holder = (ViewHolder) view.getTag();
        holder.memnames.setText(list.get(position).getName());
        holder.ceamount.setText(list.get(position).getCeamt());
        holder.checkbox.setChecked(list.get(position).isSelected());
        return view;
      }
}

答案 1 :(得分:0)

在您的适配器中,使用Map<Integer,Boolean>Map<Integer,String>来存储复选框的状态和editText的值

答案 2 :(得分:0)

Map是两种数据类型之间的链接,因此从Integer到Boolean的Map是从特定整数值到相应布尔值的一对一函数(1 - &gt; true,2 - &gt; false所以这基本上允许你为每个列表位置维护一个“映射”到复选框的状态。 因此,在java中实现Map的方法是:

Map<Integer,Boolean> listMap = new HashMap<Integer,Boolean>();

当你想要从一个列表元素添加一个映射到它的布尔状态时,你必须使用这个代码:

listMap.put(yourListPositionInIntValue, statusOfCheckboxInTrueOrFalse);

当你想修改地图中的布尔值时,你所要做的就是用相同的ListPosition和更新的布尔值重复上面的代码行。