2012-03-27 26 views
1

我有一定的問題。我正在創建一個帶有自定義tableLayout作爲消耗列表子項的消耗性列表。 Table Layput在每行中有1個textView和3個EditText。android在內部類運行時沒有得到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 

回答

0

實際上,通過在初始化EditText時將getTextData()方法設置爲getChildView()中的每個EditText上的TextWatcher解決了問題,並且每次都在其他變量中存儲輸入到edittext中的數據。因爲getChildView()會在單擊每個editText時重新加載。因此,editText值需要存儲在任何其他變量中,以檢索後續數據。

例如:

@覆蓋公共無效beforeTextChanged(CharSequence中,整數 開始,詮釋計數,INT後){// 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(); 



         } 


         } 
0

將設置在每一個的EditText時TextWatcher在EDITTEXT文本已經被改變了就會被調用。

+0

我已嘗試但是控件不會在TextWatcher中輸入...請檢查getTableData()中的註釋代碼 – user1294668 2012-03-27 08:51:09