2012-06-07 21 views
0

當我試圖獲取值來計算總數我無法獲得值,而是得到[email protected]。我如何克服這種情況,我需要將這些值傳遞給另一個Activity。我的代碼:從TableLayout獲取數據?

public class ProvisionActivity extends Activity { 

    private TableLayout mTable; 
    private static int sCount = 0; 
    Button btnAdd; 
    String[] pnames = { "provision1", "provision2", "provision3", "provision4", 
      "provision5" }; 
    String[] pprice = { "45", "85", "125", "15", "198" }; 
    StringBuffer xpenses; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     btnAdd = (Button) findViewById(R.id.button1); 
     mTable = (TableLayout) findViewById(R.id.tableprovision); 
     xpenses = new StringBuffer(); 
     btnAdd.setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 

       mTable.addView(addRow(pnames, pprice)); 

       for (int i = 0; i < mTable.getChildCount(); i++) { 
        mTable.getChildAt(i); 

        System.out.println("Table Row values are " 
          + mTable.getChildAt(i)); 

        xpenses = xpenses.append(mTable.getChildAt(i).toString()); 

        System.out 
          .println("Expense Table Values Are After Storing it in a String variable " 
            + xpenses); 

       } 
      } 
     }); 

    } 

    private TableRow addRow(String[] sname, final String[] sprice) { 

     TableRow tr = new TableRow(this); 
     tr.setId(1000 + sCount); 
     tr.setLayoutParams(new TableLayout.LayoutParams(
       TableLayout.LayoutParams.FILL_PARENT, 
       TableLayout.LayoutParams.WRAP_CONTENT)); 
     TableRow.LayoutParams blparams = new TableRow.LayoutParams(150, 35); 
     final Spinner spinner = new Spinner(this); 
     spinner.setLayoutParams(blparams); 
     spinner.setBackgroundColor(Color.DKGRAY); 
     ArrayAdapter<String> xtypeadapter = new ArrayAdapter<String>(this, 
       android.R.layout.simple_spinner_dropdown_item, sname); 

     spinner.setAdapter(xtypeadapter); 
     tr.addView(spinner); 
     TableRow.LayoutParams tlparams = new TableRow.LayoutParams(55, 
       TableLayout.LayoutParams.WRAP_CONTENT); 
     final TextView textView = new TextView(this); 
     textView.setLayoutParams(tlparams); 
     textView.setTextColor(Color.BLACK); 

     spinner.setOnItemSelectedListener(new OnItemSelectedListener() { 

      public void onItemSelected(AdapterView<?> arg0, View arg1, 
        int arg2, long arg3) { 
       textView.setText(" " 
         + sprice[spinner.getSelectedItemPosition()]); 

      } 

      public void onNothingSelected(AdapterView<?> arg0) { 

      } 
     }); 
     tr.addView(textView); 
     TableRow.LayoutParams blparams1 = new TableRow.LayoutParams(
       TableRow.LayoutParams.WRAP_CONTENT, 
       TableRow.LayoutParams.WRAP_CONTENT); 
     final Button button = new Button(this); 
     button.setLayoutParams(blparams1); 
     button.setBackgroundColor(Color.LTGRAY); 
     button.setText(" - "); 
     button.setId(2000 + sCount); 
     button.setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 
       mTable.removeView(findViewById(v.getId() - 1000)); 
      } 
     }); 
     tr.addView(button); 
     sCount++; 
     return tr; 

    } 
} 

佈局

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#ffffff" > 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentLeft="true" 
     android:layout_marginBottom="154dp" 
     android:layout_marginLeft="42dp" 
     android:text="Total" 
     android:textColor="#000000" /> 

    <TextView 
     android:id="@+id/textView2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBaseline="@+id/textView1" 
     android:layout_alignBottom="@+id/textView1" 
     android:layout_alignParentRight="true" 
     android:layout_marginRight="80dp" 
     android:text="TextView" 
     android:textColor="#000000" /> 

    <TableLayout 
     android:id="@+id/tableprovision" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:layout_marginTop="74dp" > 
    </TableLayout> 

    <TextView 
     android:id="@+id/textView3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_marginTop="33dp" 
     android:text="Expenses " 
     android:textColor="#000000" 
     android:textSize="18sp" /> 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_alignTop="@+id/textView3" 
     android:layout_marginRight="30dp" 
     android:text="Add" /> 

</RelativeLayout> 
+0

參考此以前張貼,我希望它可以幫助你http://stackoverflow.com/questions/10514097/how-to-get-values-from-a--andami-created-android-tablerow – Aerrow

+0

@Aerrow正如你在觀察有問題時,用戶添加一行,我會得到微調和一個textview和一個按鈕。然後我有一些東西在微調從該用戶將選擇他的desired.Now我需要得到在我創建的對應的東西值,所以我可以計算總數。 – Harish

回答

1

我解決了如下圖所示的方式上述問題。與一些修改上面的代碼

public class ProvisionActivity extends Activity { 

private TableLayout mTable; 
private static int sCount = 0; 
Button btnAdd, btntot; 
TextView tot; 
int sum = 0; 
String[] pnames = { "provision1", "provision2", "provision3", "provision4", 
     "provision5" }; 
String[] pprice = { "45", "85", "125", "15", "195" }; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    btnAdd = (Button) findViewById(R.id.button1); 
    mTable = (TableLayout) findViewById(R.id.tableprovision); 
    tot = (TextView) findViewById(R.id.textViewsum); 
    btntot = (Button) findViewById(R.id.buttontotal); 

    btnAdd.setOnClickListener(new OnClickListener() { 

     public void onClick(View v) { 
      mTable.addView(addRow(pnames, pprice)); 
     } 
    }); 

    btntot.setOnClickListener(new OnClickListener() { 

     public void onClick(View v) { 

      int total = 0; 
      for (int i = 0; i < mTable.getChildCount(); i++) { 
       TableRow mRow = (TableRow) mTable.getChildAt(i); 
       Spinner mspinner = (Spinner) mRow.getChildAt(0); 
       TextView mTextView = (TextView) mRow.getChildAt(1); 
       Log.i("mspinner", "" + mspinner.getSelectedItem()); 
       total = total 
         + Integer.parseInt(mTextView.getText().toString()); 
      } 

      System.out.println("Sum of the provision are " + total); 
      tot.setText("" + total); 
     } 

    }); 

} 

private TableRow addRow(String[] sname, final String[] sprice) { 

    TableRow tr = new TableRow(this); 
    tr.setId(1000 + sCount); 
    tr.setLayoutParams(new TableLayout.LayoutParams(
      TableLayout.LayoutParams.MATCH_PARENT, 
      TableLayout.LayoutParams.WRAP_CONTENT)); 
    TableRow.LayoutParams blparams = new TableRow.LayoutParams(150, 35); 
    final Spinner spinner = new Spinner(this); 
    spinner.setLayoutParams(blparams); 
    spinner.setBackgroundColor(Color.DKGRAY); 
    ArrayAdapter<String> xtypeadapter = new ArrayAdapter<String>(this, 
      android.R.layout.simple_spinner_dropdown_item, sname); 

    spinner.setAdapter(xtypeadapter); 
    tr.addView(spinner); 
    TableRow.LayoutParams tlparams = new TableRow.LayoutParams(55, 
      TableLayout.LayoutParams.WRAP_CONTENT); 
    final TextView textView = new TextView(this); 
    textView.setLayoutParams(tlparams); 
    textView.setTextColor(Color.BLACK); 

    spinner.setOnItemSelectedListener(new OnItemSelectedListener() { 

     private boolean flag = false; 

     public void onItemSelected(AdapterView<?> spin, View arg1, 
       int position, long arg3) { 
      if (flag) { 
       flag = true; 
       return; 
      } 

      textView.setText(sprice[position]); 

     } 

     public void onNothingSelected(AdapterView<?> arg0) { 
      // TODO Auto-generated method stub 

     } 

    }); 
    tr.addView(textView); 
    TableRow.LayoutParams blparams1 = new TableRow.LayoutParams(
      TableRow.LayoutParams.WRAP_CONTENT, 
      TableRow.LayoutParams.WRAP_CONTENT); 

    final Button button = new Button(this); 
    button.setLayoutParams(blparams1); 
    button.setBackgroundColor(Color.LTGRAY); 
    button.setText(" - "); 
    button.setId(2000 + sCount); 
    button.setOnClickListener(new OnClickListener() { 

     public void onClick(View v) { 

      sCount--; 
      mTable.removeView(findViewById(v.getId() - 1000)); 

     } 
    }); 
    tr.addView(button); 
    sCount++; 
    return tr; 

} 
}