2016-03-27 5 views
0

Ich muss den Wert eines EditText innerhalb einer Zeile eines Table-Layouts erhalten. Ich erstelle die Tabelle, die Zeile und den Editiertext dynamisch. Ich hatte Hilfe, die Kinderzahl zu bekommen, aber ich kann immer noch nicht scheinen, den Text, der vom Benutzer in den editedtext eingegeben wird. Hier ist der XML-Code, der das Basislayout erstellt.Wie bekomme ich den Wert des Inhalts innerhalb einer Zeile eines Tabellenlayouts

<RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android" 
      xmlns:tools = "http://schemas.android.com/tools" 
      android:layout_width = "match_parent" 
      android:layout_height = "match_parent" 
      tools:context = "com.example.neil.hvacbuilder.MainActivity" > 
     <ScrollView 
    android:layout_width = "match_parent" 
    android:layout_height = "match_parent" 
    android:id = "@+id/scrollView" 
    android:layout_alignParentLeft = "true" 
    android:layout_alignParentStart = "true" 
    android:layout_below = "@+id/imgPartPicked" > 

    <TableLayout 
     android:layout_width = "match_parent" 
     android:layout_height = "wrap_content" 
     android:stretchColumns="*" 
     android:id = "@+id/tblLayoutContent" 
     android:layout_alignParentLeft = "true" 
     android:layout_alignParentStart = "true" 
     android:layout_alignParentBottom = "true" > 
    </TableLayout > 
</ScrollView > 
</RelativeLayout > 

Hier ist der Code, der die Zeile und die Edittexte innerhalb der Zeile generiert.

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.part_detail); 
    Bundle bundle = getIntent().getExtras(); 
    String btnName = bundle.getString("btnNameStored"); 
    String btnOrig = bundle.getString("btnOrig"); 

    TextView textView = (TextView) findViewById(R.id.txtBtnPushed); 
    textView.setText(btnOrig); 
    BufferedReader reader; 
    InputStream is = null; 

    // Get the name of the part picked and then grab the dimensions that are needed for that 
    // part. 

    try { 

     is = getAssets().open(btnName); 

     reader = new BufferedReader(new InputStreamReader(is)); 

     String line = reader.readLine(); 
     int lineLength = (line.length()); 

     TableLayout table = (TableLayout) findViewById(R.id.tblLayoutContent); 

     while (line != null){ 

      TableRow tblRow = new TableRow(this); 
      tblRow.setPadding(5, 30, 5, 5); 
      table.addView(tblRow); 
      line = line.toUpperCase(); 

      // sets the max number of columns to 2 and iterates through the number of lines. 
      // filling each cell with a Text Box with the name of each dimension of the part 
      // that was picked. And a EditView for the user to put in the dimensions. 

      for (int col = 0; col < NUM_COL; col++) { 
       //This is the label of what measurement needs to be enter. 
       TextView lblName = new TextView(this); 
       // This is the number you enter for the measurement. 
       EditText txtPartMeasurement = new EditText(this); 

       txtPartMeasurement.setInputType(InputType.TYPE_CLASS_NUMBER | 
         InputType.TYPE_NUMBER_FLAG_DECIMAL); 

       // Set all the input attributes for the text boxes. 

       txtPartMeasurement.setTextSize(14); 

       if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { 
        txtPartMeasurement.setTextAlignment(View.TEXT_ALIGNMENT_TEXT_END); 
       } 
       txtPartMeasurement.setEnabled(true); 

       // Set all the input attributes for the labels of what needs to be entered. 
       if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { 
        lblName.setTextAlignment(View.TEXT_ALIGNMENT_CENTER); 
       } 
       lblName.setBackgroundColor(getResources().getColor(R.color.colorPartMeasurements)); 
       lblName.setFocusable(true); 
       lblName.setText(line); 
       lblName.setTextSize(14); 

       txtPartMeasurement.setTag(line); 

       // Add the labels and text boxes to the grid. 
       tblRow.addView(lblName); 
       tblRow.addView(txtPartMeasurement); 

       // Get the next line in the file if there is one. 
       line = reader.readLine(); 
      } 
     }; 

    } 
    catch (IOException e) { 
     e.printStackTrace(); 
    } 

} 

Hier ist der Code, der mir die von Ihnen eingegebenen Werte gibt, aber es funktioniert nicht richtig.

@Override 
public void onClick(View v) { 
    Button button = (Button) v; 
    String btnText = button.getText().toString(); 


    switch (v.getId()) { 
     //This is the Save and Continue button. It saves the info entered and goes back to 
     // pick the next part needed. 
     case R.id.btnNextPartDetail: 

      TableLayout PartDetailLayout = ((TableLayout) findViewById(R.id.tblLayoutContent)); 
      int childParts = PartDetailLayout.getChildCount(); 
      if (PartDetailLayout != null) { 
       for (int i = 0; i < PartDetailLayout.getChildCount(); i++) { 
        View viewChild = PartDetailLayout.getChildAt(i); 
        if (viewChild instanceof EditText) { 
         // get text from edit text 
         String text = ((EditText) viewChild).getText().toString(); 

        } 
        else if (viewChild instanceof TextView) { 
         // get text from text view 
         String text = ((TextView) viewChild).getText().toString(); 
         //TODO: add rest of the logic 
        } 
       } 
      } 

      Toast.makeText(getApplicationContext(), 
        "Button Save/Continue Selected", 
        Toast.LENGTH_LONG).show(); 
      break; 

Es bringt mir die Zählung, aber nicht den Wert. Ich weiß nicht, wie die ID von jedem editiertext sein wird, also füge ich ein Tag hinzu, wenn jedes erstellt wird. Aber das Tag ist auch dynamisch, da es aus einer Datei stammt.

Also, meine Frage ist, was mache ich falsch und wie bekomme ich den Wert von jedem editiertext. Wohlgemerkt, es kann 10 oder 14 oder mehr Edittexte in der Tabelle geben.

Sorry für die lange Post, aber ich wollte so vollständig wie möglich sein. Danke.

Okay, hier ist der Arbeitscode. Nochmals Danke

   TableLayout PartDetailLayout = ((TableLayout) findViewById(R.id.tblLayoutContent)); 
      int childParts = PartDetailLayout.getChildCount(); 
      if (PartDetailLayout != null) { 
       for (int i = 0; i < childParts; i++) { 
        View viewChild = PartDetailLayout.getChildAt(i); 
        if (viewChild instanceof TableRow) { 
         int rowChildParts = ((TableRow) viewChild).getChildCount(); 
         for (int j = 0; j < rowChildParts; j++) { 
          View viewChild2 = ((TableRow) viewChild).getChildAt(j); 
          if (viewChild2 instanceof EditText) { 
           // get text from edit text 
           String txtEdit = ((EditText) viewChild2).getText() 
             .toString(); 

          } else if (viewChild2 instanceof TextView) { 
           // get text from text view 
           String txttext = ((TextView) viewChild2).getText().toString(); 

           //TODO: add rest of the logic 
          } 
         } 
        } 
       } 
      } 

Antwort

0

Das mögliche Problem ist, dass Sie Childs von TableLayout überprüfen, aber Ihre EditText und TextView sind in TableRow, so dass Sie Childs von Childs müssen überprüfen.

Ich gehe davon aus, dass so sein würde:

TableLayout PartDetailLayout = ((TableLayout) findViewById(R.id.tblLayoutContent)); 
     iTableLayout PartDetailLayout = ((TableLayout) findViewById(R.id.tblLayoutContent)); 
int childParts = PartDetailLayout.getChildCount(); 
if (PartDetailLayout != null) { 
    for (int i = 0; i < childParts; i++) { 
     View viewChild = PartDetailLayout.getChildAt(i); 
     if (viewChild instanceof TableRow) { 
      int rowChildParts = ((TableRow) viewChild).getChildCount(); 
      for (int j = 0; j < rowChildParts; j++) { 
       View viewChild2 = ((TableRow) viewChild).getChildAt(j); 
       if (viewChild2 instanceof EditText) { 
        // get text from edit text 
        String text = ((EditText) viewChild2).getText().toString(); 
       } else if (viewChild2 instanceof TextView) { 
        // get text from text view 
        String text = ((TextView) viewChild2).getText().toString(); 
        //TODO: add rest of the logic 
       } 
      } 
     } 
    } 
} 
+0

Sie so viel Dank. Macht perfekt Sinn. Ich habe es ausprobiert und es funktioniert super. – vbneil54

+0

Ich musste ein paar Änderungen vornehmen, aber sehr geringfügig. Ich habe den obigen Arbeitscode gepostet. – vbneil54

+0

Danke für die Korrekturen :) –

Verwandte Themen