Search code examples
androidandroid-tablelayout

Getting data from TableLayout?


When I'm trying to get the values to calculate the total I'm unable to get the values, instead getting android.widget.TableRow@4103c468. How can I overcome this situation and more I need to pass those values to another Activity. My code:

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;

    }
}

LAYOUT

<?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>

Solution

  • i solved the above issue in the below shown way.. with some modifications to the above code

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