动态创建动态文本视图和网格视图或列表视图

时间:2014-04-24 05:20:37

标签: java android gridview textview

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

    lv =getListView();
    category_main = "";

    new Getitems().execute(category_main);

    sub = (Button) findViewById(R.id.submit);
    sub.setOnClickListener(this);

    b =(Button) findViewById(R.id.start);
    b.setOnClickListener(this);

    itemList = new ArrayList<HashMap<String, String>>();
}

@Override
public void onClick(View arg0) {

    switch(arg0.getId()){

    case R.id.start: try {
        Log.d("start","click");
                            onbuttonclick();
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    break;

    case R.id.submit:
        Log.d("submi","click");
        onsubmitclick();
        break;
}
}


    private void onsubmitclick() {
    Log.d("submi inside","click");
    EditText et = (EditText) findViewById(R.id.EditText05);
    EditText et1 = (EditText) findViewById(R.id.EditText06);
    TextView tx= (TextView) findViewById(R.id.name);

                String json = "{'Order':[";

                if(!et.equals(str)){
                    Log.d("json", "json");

                    String stra = "{'Table_id':"+Second.a+",'item_name':'"+tx.getText()+"','Item_quantity':'"+et.getText()+"','Item_additionaldetails':'"+et1.getText()+"'}";

                    json = json+stra;

                }
                json = json + "]}";

                Log.d("json", json);

}




private void onbuttonclick() throws JSONException {

    TableRow[] tr = new TableRow[arry1.length];     

    final TextView[] tx = new TextView[arry1.length];
    GridLayout tl = (GridLayout) findViewById(R.id.tablelayout1);
    tl.setRowCount(arry1.length*2);
    tl.setColumnCount(1);
    final Button but = new Button(S1.this);
    b.setText("hi");

    gl = new GridLayout[arry1.length];

    for (i = 0; i < arry1.length; i++) {

        final String cat = arry1[i].toString();

        tx[i] = new TextView(S1.this);
        tx[i].setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
        tx[i].setAllCaps(true);
        tx[i].setTextSize(15);
        tx[i].setText(cat);

        tr[i] = new TableRow(S1.this);
        tr[i].setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
        tr[i].addView(tx[i],new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));            

        tl.addView(tr[i],new GridLayout.LayoutParams());    
        tl.addView(gl[i], new GridLayout.LayoutParams());

    }

    b.setVisibility(View.GONE);     

}

以下是我动态创建textviews的代码。 我在这方面很成功。

现在我想在每个文本视图下面添加网格视图或列表视图,如下图所示:

我希望在点击时显示文本视图和显示网格或列表视图的方式相同,这一切都应该基于arry1命名数组的长度。

我想要的是动态创建网格或列表视图。

0 个答案:

没有答案