生成这些菜单的做法:
package com.example.caidan;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.LogRecord;
import util.HttpUtils;
import com.example.Caigou.CaiGou1Activity;
import com.example.findcai.R;
import com.example.findcai.SecondActivity;
import com.example.findcai.R.layout;
import com.example.findcai.R.menu;
import android.opengl.Visibility;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;
public class CaigoudanActivity extends Activity {
Button submit1;
TextView t1,t2,t3,t4,t5,jian1,jia1,cha1;
EditText edit1;
int i=7,j=1;
TableRow row;
String cont;//传过来的内容
String []context;//将 cont 分割的数组
String[] cz; //再次将context分割的数组
String[] contSplit;
private int tvStory4[] = new int[100];//7减去当前库存
private int editStory[] = new int[100];
private int hang=0;
boolean isrun[]=new boolean[100];
TextView tv [] = new TextView[100];//第一列
TextView tv2 [] = new TextView[100];//第4列
TextView tv3 [] = new TextView[100];
TextView tv4 [] = new TextView[100];
TextView editText []=new TextView[100];
TextView textWeiZhi[]=new TextView[100];
int a;
int b;
private final int WC = ViewGroup.LayoutParams.MATCH_PARENT;
private final int FP = ViewGroup.LayoutParams.FILL_PARENT;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);//横屏
setContentView(R.layout.activity_caigoudan);
submit1=(Button) this.findViewById(R.id.submit1);//提交
//接受内容cont
Intent it=getIntent();
cont =it.getExtras().getString("cont");
Log.i("传过来的内容 cont", cont);
/**
* 将字符串cont用{||}进行分割后得到数组c,得到:c[1]=1|SLSC029|崇明露天青菜,c[2]=2|SLSC060|上海本地青菜.....
*/
context = cont.split("\\{\\|\\|\\}");//分割的个数就是行数
//将context变成二维数组context2
String [] []context2=new String[context.length][];
//TableLayout的实例 ,最终将所有tablerow的添加到这个布局中显示
TableLayout tableLayout = (TableLayout)findViewById(R.id.caidan_table);
//全部列自动填充空白处
tableLayout.setStretchAllColumns(true);
//36行
for(hang=0;hang<context.length;hang++){
//动态生成TableRow
final TableRow tableRow1=new TableRow(this);
tableRow1.setBackgroundColor(Color.WHITE);
TableRow.LayoutParams tabrowLayout = new TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.MATCH_PARENT);
//将每一行分割获取列数
String[] contextCol = context[hang].split("\\|");
context2[hang] = new String[contextCol.length-2];//得到列数
//位置是12345
textWeiZhi[hang]= new TextView(this);
textWeiZhi[hang].setBackgroundResource(R.drawable.topshapee);
textWeiZhi[hang].setText(""+j++);
//生成加号减号
TextView textJia[] = new TextView[context.length];//生成36行加号
textJia[hang]= new TextView(this);//加号
textJia[hang].setTextSize(30);
textJia[hang].setText("+");
textJia[hang].setTextColor(Color.BLACK);
editText[hang]=new TextView(this);//数字框
editText[hang].setTextSize(20);
editText[hang].setTextColor(Color.BLACK);
TextView textJian []= new TextView[context.length];//生成36行减号
textJian[hang]= new TextView(this);//减号
textJian[hang].setTextSize(30);
textJian[hang].setText("-");
textJian[hang].setTextColor(Color.BLACK);
TextView textCha= new TextView(this);//叉
textCha.setTextSize(30);
textCha.setText("X");
textCha.setTextColor(Color.RED);
isrun[hang]=true;
//叉
textCha.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
tableRow1.removeAllViews();
tableRow1.setBackgroundResource(0);
}
});
//6列
// for(int col=0;col<(contextCol.length)-2;col++){
// if(col==1){
//
// }
//单列的 方便去掉其他不需要的列 或者添加列
context2[hang][0] = String.valueOf(contextCol[0]);
context2[hang][2] = String.valueOf(contextCol[2]);
context2[hang][3] = String.valueOf(contextCol[3]);
context2[hang][4] = String.valueOf(contextCol[4]);
//tv用于代表每一格格子里的内容
tv[hang]=new TextView(this); //第一列
tv[hang].setBackgroundResource(R.drawable.topshapee);
// tv2 = new TextView[context.length];
tv2[hang]=new TextView(this); //第二列
tv2[hang].setBackgroundResource(R.drawable.topshapee);
// tv3= new TextView[context.length];
tv3[hang]=new TextView(this); //第三列
tv3[hang].setBackgroundResource(R.drawable.topshapee);
// tv4 = new TextView[context.length];
tv4[hang]=new TextView(this); //第四列
tv4[hang].setBackgroundResource(R.drawable.topshapee);
//为textView赋值
tv[hang].setText(context2[hang][0]);
tv2[hang].setText(context2[hang][2]);
tv3[hang].setText(context2[hang][3]);
tv4[hang].setText(context2[hang][4]);
tvStory4[hang]=Integer.parseInt(tv4[hang].getText().toString());//获取当前库存 转化为int类型
Log.e("------>diyici", ""+tvStory4[hang]);
editStory[hang]=7-tvStory4[hang];//7减去 当前库存
Log.i("当前库存 ----",""+tvStory4[hang]);
Log.i("7减去当前库存 所得 ----",""+editStory[hang]);
//为输入框附上 7减去当前库存的数
editText[hang].setText(""+editStory[hang]);
//将点击的数据附带在setTag 给一个目标1 2 3,以便好判断加减的哪一个
textJia[hang].setTag(hang);
textJian[hang].setTag(hang);
//加
textJia[hang].setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
int tag = (Integer) arg0.getTag();
if(isrun[tag]){
a =editStory[tag];
b =tvStory4[tag];
isrun[tag]=false;
}
Log.e("------->7-当前库存a", ""+a);
Log.e("------->当前库存b", ""+b);
if(a<7-b){
a+=1;
editText[tag].setText(""+a);
Log.e("------->panduan b", ""+a);
Log.e("------>tvStory4", ""+b);
}
else{
editText[tag].setText(""+a);
}
}
});
//减
textJian[hang].setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
int tag = (Integer) arg0.getTag();//点击的哪一行
if(isrun[tag]){
a =editStory[tag];
b=tvStory4[tag];
isrun[tag]=false;
}
Log.e("----->开始得到的", ""+editStory);
if(a>=1){
a-=1;
Log.e("------>减去的 a", ""+a);
editText[tag].setText(""+a);//哪一行的editText就改变
// editStory--;
}
}
});
tableRow1.addView(tv[hang]);
tableRow1.addView(tv2[hang]);
tableRow1.addView(tv3[hang]);
tableRow1.addView(textWeiZhi[hang]);
tableRow1.addView(tv4[hang]);
tableRow1.addView(textJia[hang]);
tableRow1.addView(editText[hang]);
tableRow1.addView(textJian[hang]);
tableRow1.addView(textCha);
tableRow1.setBackgroundResource(R.drawable.textshapee);
//新建的TableRow添加到TableLayout
tableLayout.addView(tableRow1, new TableLayout.LayoutParams(FP, WC));
System.out.println("序号:"+tv[hang].getText().toString()+"\n"+"菜单:"+tv2[hang].getText().toString()+"\n"+"补充:"+editText[hang].getText().toString()+"个");
}
//抬头的提交按钮// 提交到服务器
submit1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
String dopostUrl="http:";
Map<String,String> map=new HashMap<String, String>();
map.put("tv[hang]", tv[hang].getText().toString());
map.put("tv2[hang]", tv2[hang].getText().toString());
map.put("tv3[hang]", tv3[hang].getText().toString());
map.put("tv4[hang]", tv4[hang].getText().toString());
map.put("textWeizhi[hang]", textWeiZhi[hang].getText().toString());
map.put("editText[hang]", editText[hang].getText().toString());
String result=HttpUtils.sendPostMessage(map,dopostUrl);
}
});
}
}
要做提交的思路:
要做的任务是:提交
做提交的前提是:有数据传递
这些数据来源是:这个表格的:
序号(tv 1,2....),
名称(tv2 青菜),
规格(tv3 500g),
位置(textWeizhi 1,2,3...),
当前库存(tv4 数量),
需要补充的(editText 数量)
提交36行这些数据。
提交36行的话:
1. 第一步:必须将这些变成数组
总结:
首先:
将tv ,tv2 , tv3 ,textWeizhi ,tv4 定义成全局变量 并且附上初始大小
例如:TextView tv [] = new TextView[100];//第一列
其次:
需要补充的数量:editText
点击加减editText要随之减一个或者加一个,所以点击过后给editText[ ]赋值
中间的应该绑定tag 如下tag:
他表示:点击哪一行哪一行响应 ,也就是tag是目标函数