2

我正在尝试在自定义列表中创建一个ListActivity具有CheckBox,TextViewEditText连续的。

I need to get the user's input in my EditTextand save that value in a database when the CheckBoxfor that row is selected. 如果CheckBox未选择,EditText则禁用。后者我可以做到,但我无法保留 的值EditText并将其存储在数据库中。

目前,如果我输入任何内容EditText并按下后退按钮以最小化键盘,则会EditText返回到之前的值。

到目前为止,我已经编写了以下代码以显示自定义ListView以及选择和取消选择复选框。我只需要帮助我完成这EditText部分。

编辑Exepense.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);
    }
}

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

模型.Java

package h.pkg.main;

public class Model {
    private String name;
    private String ceexpamt;
    private boolean selected;

    public Model(String name, String ceexpamt, boolean selected) {
        this.name = name;
        this.ceexpamt = ceexpamt;
        this.selected = selected;
    }

    public String getName() {
        return name;
    }

    public String getCeamt() {
        return ceexpamt;
    }

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

    public void setCeamt(String ceamt) {
        this.ceexpamt = ceamt;
    }

    public boolean isSelected() {
        return selected;
    }

    public void setSelected(boolean selected) {
        this.selected = selected;
    }

    public void enableEditText()
    {

    }
} 
4

3 回答 3

2

大家好,感谢您的回答..但是由于我已经在使用模型类...我尝试通过该示例本身完成任务...请查看修改后的 InteractiveArrayAdapter.Java 这在保留编辑文本的值方面效果很好编辑后在正确的行中。

我刚刚将此方法添加到我的 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;
      }
}
于 2012-08-03T20:11:47.987 回答
0

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

于 2012-08-03T09:19:49.600 回答
0

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

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

当您希望将一个列表元素的映射添加到它的布尔状态时,您必须使用以下代码:

listMap.put(yourListPositionInIntValue, statusOfCheckboxInTrueOrFalse);

当您希望修改地图中的布尔值时,您所要做的就是使用相同的 ListPosition 和更新的布尔值重复上述代码行。

于 2012-08-03T09:52:15.393 回答