从TableLayout获取数据? [英] Getting data from TableLayout?
本文介绍了从TableLayout获取数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我试图获取值来计算总,我无法得到的值,而不是获取的 android.widget.TableRow@4103c468 的。我怎样才能克服这种情况多,我需要这些值传递给另一个活动
。我的code:
公共类ProvisionActivity延伸活动{ 私人TableLayout mTable;
私有静态诠释SCOUNT = 0;
按钮btnAdd;
串[] pnames = {provision1,provision2,provision3,provision4,
provision5};
的String [] = pprice {45,85,125,15,198};
StringBuffer的xpenses; @覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main); btnAdd =(按钮)findViewById(R.id.button1);
mTable =(TableLayout)findViewById(R.id.tableprovision);
xpenses =新的StringBuffer();
btnAdd.setOnClickListener(新OnClickListener(){ 公共无效的onClick(视图v){ mTable.addView(addRow(pnames,pprice)); 的for(int i = 0; I< mTable.getChildCount();我++){
mTable.getChildAt(ⅰ); 的System.out.println(表行值
+ mTable.getChildAt(I)); xpenses = xpenses.append(mTable.getChildAt(ⅰ)的ToString()); System.out的
.println(费用表值是一个字符串变量保存完后
+ xpenses); }
}
}); } 私人的TableRow addRow(字符串[] SNAME,最终的String [] sprice){ TR的TableRow =新的TableRow(本);
tr.setId(1000 + SCOUNT);
tr.setLayoutParams(新TableLayout.LayoutParams(
TableLayout.LayoutParams.FILL_PARENT,
TableLayout.LayoutParams.WRAP_CONTENT));
TableRow.LayoutParams blparams =新TableRow.LayoutParams(150,35);
最后的微调微调=新的微调(本);
spinner.setLayoutParams(blparams);
spinner.setBackgroundColor(Color.DKGRAY);
ArrayAdapter<串GT; xtypeadapter =新ArrayAdapter<串GT;(这一点,
android.R.layout.simple_spinner_dropdown_item,SNAME); spinner.setAdapter(xtypeadapter);
tr.addView(微调);
TableRow.LayoutParams tlparams =新TableRow.LayoutParams(55,
TableLayout.LayoutParams.WRAP_CONTENT);
最终的TextView的TextView =新的TextView(本);
textView.setLayoutParams(tlparams);
textView.setTextColor(Color.BLACK); spinner.setOnItemSelectedListener(新OnItemSelectedListener(){ 公共无效onItemSelected(适配器视图<>为arg0,ARG1查看,
INT ARG2,长ARG3){
textView.setText(
+ sprice [spinner.getSelectedItemPosition()]); } 公共无效onNothingSelected(适配器视图<>为arg0){ }
});
tr.addView(TextView的);
TableRow.LayoutParams blparams1 =新TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT);
最后Button按钮=新按钮(本);
button.setLayoutParams(blparams1);
button.setBackgroundColor(Color.LTGRAY);
button.setText( - );
button.setId(2000 + SCOUNT);
button.setOnClickListener(新OnClickListener(){ 公共无效的onClick(视图v){
mTable.removeView(findViewById(v.getId() - 1000));
}
});
tr.addView(按钮);
SCOUNT ++;
返回TR; }
}
布局
块引用><?XML版本=1.0编码=UTF-8&GT?;
<的RelativeLayout的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:背景=#FFFFFF> <的TextView
机器人:ID =@ + ID / textView1
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignParentBottom =真
机器人:layout_alignParentLeft =真
机器人:layout_marginBottom =154dp
机器人:layout_marginLeft =42dp
机器人:文字=总计
机器人:文字颜色=#000000/> <的TextView
机器人:ID =@ + ID / textView2
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignBaseline =@ + ID / textView1
机器人:layout_alignBottom =@ + ID / textView1
机器人:layout_alignParentRight =真
机器人:layout_marginRight =80dp
机器人:文字=TextView的
机器人:文字颜色=#000000/> < TableLayout
机器人:ID =@ + ID / tableprovision
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignParentLeft =真
机器人:layout_alignParentTop =真
机器人:layout_marginTop =74dp>
< / TableLayout> <的TextView
机器人:ID =@ + ID / textView3
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignParentTop =真
机器人:layout_marginTop =33dp
机器人:文字=费用
机器人:文字颜色=#000000
机器人:TEXTSIZE =18sp/> <按钮
机器人:ID =@ + ID /按钮1
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignParentRight =真
机器人:layout_alignTop =@ + ID / textView3
机器人:layout_marginRight =30dp
机器人:文字=添加/>< / RelativeLayout的>
解决方案我解决了如下图所示的上述问题的方式..一些修改上述code
公共类ProvisionActivity延伸活动{私人TableLayout mTable;
私有静态诠释SCOUNT = 0;
按钮btnAdd,btntot;
TextView的TOT;
INT总和= 0;
串[] pnames = {provision1,provision2,provision3,provision4,
provision5};
的String [] = pprice {45,85,125,15,195};@覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main); btnAdd =(按钮)findViewById(R.id.button1);
mTable =(TableLayout)findViewById(R.id.tableprovision);
TOT =(的TextView)findViewById(R.id.textViewsum);
btntot =(按钮)findViewById(R.id.buttontotal); btnAdd.setOnClickListener(新OnClickListener(){ 公共无效的onClick(视图v){
mTable.addView(addRow(pnames,pprice));
}
}); btntot.setOnClickListener(新OnClickListener(){ 公共无效的onClick(视图v){ INT总= 0;
的for(int i = 0; I< mTable.getChildCount();我++){
MROW的TableRow =(的TableRow)mTable.getChildAt(I)
微调mspinner =(微调)mRow.getChildAt(0);
TextView的mTextView =(TextView中)mRow.getChildAt(1);
Log.i(mspinner,+ mspinner.getSelectedItem());
总=总
+的Integer.parseInt(mTextView.getText()的toString());
} 的System.out.println(提供之和+总);
tot.setText(+总);
} });}私人的TableRow addRow(字符串[] SNAME,最终的String [] sprice){ TR的TableRow =新的TableRow(本);
tr.setId(1000 + SCOUNT);
tr.setLayoutParams(新TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.WRAP_CONTENT));
TableRow.LayoutParams blparams =新TableRow.LayoutParams(150,35);
最后的微调微调=新的微调(本);
spinner.setLayoutParams(blparams);
spinner.setBackgroundColor(Color.DKGRAY);
ArrayAdapter<串GT; xtypeadapter =新ArrayAdapter<串GT;(这一点,
android.R.layout.simple_spinner_dropdown_item,SNAME); spinner.setAdapter(xtypeadapter);
tr.addView(微调);
TableRow.LayoutParams tlparams =新TableRow.LayoutParams(55,
TableLayout.LayoutParams.WRAP_CONTENT);
最终的TextView的TextView =新的TextView(本);
textView.setLayoutParams(tlparams);
textView.setTextColor(Color.BLACK); spinner.setOnItemSelectedListener(新OnItemSelectedListener(){ 私人布尔标志= FALSE; 公共无效onItemSelected(适配器视图<>旋转,观ARG1,
INT位置,长ARG3){
如果(标志){
标志=真实的;
返回;
} textView.setText(sprice [位置]); } 公共无效onNothingSelected(适配器视图<>为arg0){
// TODO自动生成方法存根 } });
tr.addView(TextView的);
TableRow.LayoutParams blparams1 =新TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT); 最后Button按钮=新按钮(本);
button.setLayoutParams(blparams1);
button.setBackgroundColor(Color.LTGRAY);
button.setText( - );
button.setId(2000 + SCOUNT);
button.setOnClickListener(新OnClickListener(){ 公共无效的onClick(视图v){ sCount--;
mTable.removeView(findViewById(v.getId() - 1000)); }
});
tr.addView(按钮);
SCOUNT ++;
返回TR;}
}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>
解决方案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; } }
这篇关于从TableLayout获取数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文