android没有在内部类的运行时获取editText值

时间:2012-03-27 05:22:11

标签: android

我有一定的问题。我正在使用自定义tableLayout创建一个可消耗的List作为consumeableList的子项。表格输出每行有1个textView和3个EditText。

现在,问题是TableLayout中的editTexts没有检测到在运行时输入的文本。它从editText中提供空白值。请查找附带的代码并建议我克服这个问题的建议。

主要问题在于gettabledata()中的editText方法显示isInEditMode“False”。你能帮忙我们如何在运行时将IsInEditMode设置为“true”。

package dus.data;

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

import android.app.Activity;
import android.app.ExpandableListActivity;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputType;
import android.text.Layout;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.SimpleExpandableListAdapter;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

public class VarietyDetailsActivity extends ExpandableListActivity {
    TextView tv;

    EditText r1, r2, r3;
    TableLayout table;
    String[] States;
    String year;
    String[] listval = { "Candidate Variety", "Referencer variety 1",
            "Reference Variety 2", "Reference Variety 3" };

    @Override
    public void onCreate(Bundle icicle) {

        super.onCreate(icicle);

        year = getIntent().getStringExtra("year").toString();
        System.out.println("Year in details activity:::::::" + year);
        States = getIntent().getStringArrayExtra("States");

        setContentView(R.layout.variety_details);

        List groupList = new ArrayList();
        Map groupData = new HashMap();
        System.out.println("listval[i]:::::" + "Candidate Variety");
        groupData.put("text2", "Candidate Variety");

        groupList.add(groupData);

        Map groupData1 = new HashMap();
        System.out.println("listval[i]:::::" + "Reference Variety 1");
        groupData1.put("text2", "Reference Variety 1");

        groupList.add(groupData1);

        Map groupData2 = new HashMap();
        System.out.println("listval[i]:::::" + "Reference Variety 2");
        groupData2.put("text2", "Reference Variety 2");

        groupList.add(groupData2);

        Map groupData3 = new HashMap();
        System.out.println("listval[i]:::::" + "Reference Variety 3");
        groupData3.put("text2", "Reference Variety 3");

        groupList.add(groupData3);

        List childList = new ArrayList();
        List childList1 = new ArrayList();
        Map childMap = new HashMap();
        childMap.put("States", "States");
        childMap.put("R1", "R1");
        childMap.put("R2", "R2");
        childMap.put("R3", "R3");
        for (int i = 0; i < States.length; i++) {
            childMap.put(States[i], States[i]);
            // childMap.put("Green", "Green");
            // childMap.put("Purple", "Purple");
        }

        childList1.add(childMap);

        childList.add(childList1);
        childList.add(childList1);
        childList.add(childList1);
        childList.add(childList1);

        String[] str = new String[States.length + 4];
        str[0] = "States";
        str[1] = "R1";
        str[2] = "R2";
        str[3] = "R3";
        for (int j = 0; j < States.length; j++) {
            str[j + 4] = States[j];
        }

        int[] ids = new int[States.length + 4];
        ids[0] = R.id.States;
        ids[1] = R.id.r1;
        ids[2] = R.id.r2;
        ids[3] = R.id.r3;
        for (int j = 0; j < States.length; j++) {
            ids[j + 4] = j;
        }

        for (int i = 0; i < listval.length; i++) {
            System.out.println("listval[i]:::::" + listval[i]);

            // setListAdapter(new ExampleAdapter(this));

            /*
             * SimpleExpandableListAdapter listAdapter = new
             * SimpleExpandableListAdapter (this.getApplicationContext(),
             * groupList,R.layout.grouplayout_collapsed, new String[] {"text2"},
             * new int[] {R.id.text2}, childList,R.layout.childlayout, str,
             * getValue());
             */

            MyAdapter ma = new MyAdapter(this.getApplicationContext(),
                    groupList, R.layout.grouplayout_collapsed,
                    new String[] { "text2" }, new int[] { R.id.text2 },
                    childList, 0, str, ids);
            setListAdapter(ma);

        }

    }

    private class MyAdapter extends SimpleExpandableListAdapter {
        LayoutInflater layoutInflater;
        Context context;

        public MyAdapter(Context context,
                List<? extends Map<String, ?>> groupData, int groupLayout,
                String[] groupFrom, int[] groupTo,
                List<? extends List<? extends Map<String, ?>>> childData,
                int childLayout, String[] childFrom, int[] childTo) {
            super(context, groupData, groupLayout, groupFrom, groupTo,
                    childData, childLayout, childFrom, childTo);
            this.context = getApplicationContext();
            layoutInflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

        @Override
        public View getChildView(int groupPosition, int childPosition,
                boolean isLastChild, View convertView, ViewGroup parent)

        {
            final int selecetdPos = groupPosition;
            System.out.println("111111111111111111111111111111111111111111");

            convertView = layoutInflater.inflate(R.layout.childlayout, null);
            convertView.setId(R.layout.childlayout);
            table = (TableLayout) convertView.findViewById(R.id.chart);

            System.out.println("TABLe:::::::::::::::" + table);

            TableRow tabrow1 = (TableRow) LayoutInflater.from(
                    VarietyDetailsActivity.this).inflate(
                    R.layout.expandablelist_table_head, null);
            ((TextView) tabrow1.findViewById(R.id.States)).setText("States");
            ((TextView) tabrow1.findViewById(R.id.r1)).setText("R1");
            ((TextView) tabrow1.findViewById(R.id.r2)).setText("R2");
            ((TextView) tabrow1.findViewById(R.id.r3)).setText("R3");
            tabrow1.setVisibility(0);
            System.out.println("TABROW:::::::::::::::" + tabrow1);
            System.out.println("TABLe:::::::::::::::" + table);
            table.addView(tabrow1);

            for (int k = 0; k < States.length; k++) {
                // Inflate your row "template" and fill out the fields.
                TableRow tabrow = (TableRow) LayoutInflater.from(
                        VarietyDetailsActivity.this).inflate(
                        R.layout.expandabletable_data, null);
                System.out.println("TABLe ROW:::::::::::::::" + tabrow);

                ((TextView) tabrow.findViewById(R.id.state1))
                        .setText(States[k]);
                ((TextView) tabrow.findViewById(R.id.state1)).setId(k);

                ((EditText) tabrow.findViewById(R.id.r1_r1val)).setInputType(0);
                ((EditText) tabrow.findViewById(R.id.r1_r1val))
                        .setEditableFactory(new Editable.Factory());

                ((EditText) tabrow.findViewById(R.id.r1_r2val)).setInputType(0);
                ((EditText) tabrow.findViewById(R.id.r1_r2val))
                        .setEditableFactory(new Editable.Factory());

                ((EditText) tabrow.findViewById(R.id.r1_r3val)).setInputType(0);
                ((EditText) tabrow.findViewById(R.id.r1_r3val))
                        .setEditableFactory(new Editable.Factory());

                tabrow.setVisibility(0);
                table.addView(tabrow);

            }

            System.out.println("TABLe ROW COUNTTTTTT:::::::::::::::"
                    + table.getChildCount());

            table.requestLayout();
            table.setVisibility(TableLayout.VISIBLE);

            Button ok = (Button) convertView.findViewById(R.id.ok);
            System.out.println("Butttooonnn:::::" + ok);
            ok.setTextColor(Color.BLACK);
            ok.setTextSize(20);
            ok.setOnClickListener(new OnClickListener() {

                public void onClick(View arg0) {
                    System.out.println("INSIDE BUTTON ON CLICK:::::");
                    System.out.println("Selecetd POs:::::" + selecetdPos);

                    String[] charValues = getTableData();
                    for (int i = 0; i < charValues.length; i++) {
                        String[] rowValues = charValues[i].split(",");
                        System.out.println("ROW VALUES Length:::::"
                                + rowValues.length);
                        if (selecetdPos == 0)
                            insertinDB(year, rowValues[0],
                                    Constants.characterstic, "candidate",
                                    rowValues[1], rowValues[2], rowValues[3]);
                        else if (selecetdPos == 1)
                            insertinDB(year, rowValues[0],
                                    Constants.characterstic, "referenceVar1",
                                    rowValues[1], rowValues[2], rowValues[3]);
                        else if (selecetdPos == 2)
                            insertinDB(year, rowValues[0],
                                    Constants.characterstic, "referenceVar2",
                                    rowValues[1], rowValues[2], rowValues[3]);
                        else if (selecetdPos == 3)
                            insertinDB(year, rowValues[0],
                                    Constants.characterstic, "referenceVar3",
                                    rowValues[1], rowValues[2], rowValues[3]);
                    }

                }
            });

            return convertView;
        }

        void insertinDB(String year, String state, String characterstic,
                String variety, String r1, String r2, String r3) {
            final DBAdapter dbAdapter = DBAdapter
                    .getDBAdapterInstance(getApplicationContext());
            dbAdapter.openDataBase();
            ContentValues initialValues = new ContentValues();

            if (variety.equals("candidate")) {

                initialValues.put("candidate_varR1", r1);
                initialValues.put("candidate_varR2", r2);
                initialValues.put("candidate_varR3", r3);
            } else if (variety.equals("referenceVar1")) {

                initialValues.put("reference_var1R1", r1);
                initialValues.put("reference_var1R2", r2);
                initialValues.put("reference_var1R3", r3);
            } else if (variety.equals("referenceVar2")) {

                initialValues.put("reference_var2R1", r1);
                initialValues.put("reference_var2R2", r2);
                initialValues.put("reference_var2R3", r3);
            } else if (variety.equals("referenceVar3")) {

                initialValues.put("reference_var3R1", r1);
                initialValues.put("reference_var3R2", r2);
                initialValues.put("reference_var3R3", r3);
            }
            dbAdapter.updateRecordRValuesInDB(initialValues, year,
                    characterstic, state);
            System.out.println("DB Updated");

            dbAdapter.close();
        }

    }

    public String[] getTableData() {
        String[] charValues = new String[States.length];

        for (int j = 0; j < States.length; j++) {

            System.out.println("TABLe:::::::::::::::" + table);

            TableRow tr = (TableRow) table.getChildAt(j + 1);
            tr.setEnabled(true);
            System.out.println("Tablerow:::::" + tr);

            System.out.println("TableRow Count:::::" + tr.getChildCount());
            TextView state = (TextView) tr.getChildAt(0);
            r1 = (EditText) tr.getChildAt(1);
            /*
             * r1.requestFocus(); r1 .setEnabled(true); r1.setFocusable(true);
             * r1.setFocusableInTouchMode(true); r1.setClickable(true);
             * r1.setInputType(InputType.TYPE_CLASS_TEXT);
             * r1.setText(r1.getText(), TextView.BufferType.EDITABLE);
             */
            System.out.println("EditText:::::" + r1);

            System.out.println("EditText enabled :::::" + r1.isEnabled());

            System.out.println("EditText Edit Mode :::::" + r1.requestFocus());
            System.out.println("EditText Edit Mode :::::" + r1.isInEditMode());
            System.out.println("EditText Text :::::" + r1.getEditableText());

            /*
             * r1.setOnEditorActionListener(new
             * EditText.OnEditorActionListener() {
             * 
             * @Override public boolean onEditorAction(TextView arg0, int arg1,
             * KeyEvent arg2) { // TODO Auto-generated method stub if (arg1 ==
             * EditorInfo.TYPE_CLASS_TEXT) { System.out.println("TRUEEEEEEEE");
             * return true;
             * 
             * } System.out.println("FALSEEEEE"); return false;
             * 
             * } }
             * 
             * );
             * 
             * r1.addTextChangedListener(new TextWatcher() {
             * 
             * 
             * @Override public void onTextChanged(CharSequence s, int start,
             * int before, int count) {
             * 
             * // TODO Auto-generated method stub
             * System.out.println("OnText Changed EditText r1 Value:::::::::::::::"
             * +r1.getText().toString());
             * 
             * 
             * }
             * 
             * @Override public void beforeTextChanged(CharSequence s, int
             * start, int count, int after) { // TODO Auto-generated method stub
             * System
             * .out.println("Before   EditText r1 Value:::::::::::::::"+r1.
             * getText().toString()); }
             * 
             * @Override public void afterTextChanged(Editable s) { // TODO
             * Auto-generated method stub
             * System.out.println("After   EditText r1 Value:::::::::::::::"
             * +r1.getText().toString()); } });
             */

            System.out.println("EditText r1 Value:::::::::::::::"
                    + r1.getText().toString());
            r2 = (EditText) tr.getChildAt(2);
            System.out.println("EditText r2 Value:::::::::::::::"
                    + r2.getText().toString());
            r3 = (EditText) tr.getChildAt(3);
            System.out.println("EditText r3 Value:::::::::::::::"
                    + r3.getText().toString());

            charValues[j] = state.getText().toString() + ","
                    + r1.getText().toString() + "," + r2.getText().toString()
                    + "," + r3.getText().toString();

        }
        for (int i = 0; i < charValues.length; i++) {
            System.out.println("charvalue  :::::" + charValues[i]);
        }
        return charValues;

    }

}





thanks and regards
Kajal

2 个答案:

答案 0 :(得分:0)

在每个EditText上设置一个TextWatcher,当editText中的文本被更改时将调用它。

答案 1 :(得分:0)

实际上,问题通过在getChildView()中的每个EditText上设置TextWatcher而不是getTableData()方法来解决,同时初始化EditText.Fetch数据并且每次在任何其他变量中存储在edittext中输入的数据。因为getChildView()在单击每个editText时会重新加载。因此,editText值也需要存储在任何其他变量中,以便检索后面的数据。

示例:

@Override public void beforeTextChanged(CharSequence s,int                       start,int count,int after){// TODO自动生成的方法存根                       }

                  public void afterTextChanged(Editable s) { // TODO
                      if((s!=null&&s.length()>0)){

                        v2 = s.toString();
                        Pattern p = Pattern.compile( "([0-9]*)" );

                        Matcher m = p.matcher(v2);
                          System.out.println("v2 :::::" + v2);

                          if(stateCount>=States.length)
                                stateCount =0;

                                else

                                stateCount = stateCount+1;
                          System.out.println("SateCount in v2:::::::::::::::" + stateCount);
                                insertInV2Array(stateCount,v2);


                        if(!m.matches())
                        {

                                Toast.makeText(VarietyDetailsActivity.this, "Please enter between 1- 9 only",8 )
                                .show();



                        }


                      }