在动态按钮单击事件上删除动态生成的文本视图

时间:2014-04-26 06:43:28

标签: android

我需要在单个按钮点击事件中删除每个Table Rows。

按钮是动态生成的。

我将如何实现这一目标?

以下是我的代码:

   public class S2 extends Modify implements OnClickListener {
   Button b;
   String arry1[], category_main,str = null, catarray[];
   int i,key = 0;
   private static String url = "http://ashapurasoftech.com/train/test.json";
   private static final String TAG_a = "menu",TAG_Name = "Name",TAG_Cat = "Category";
   JSONArray items = null;
   ArrayList<HashMap<String, String>> itemList;

@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.modify);
    category_main = "";

    new Getitems().execute(category_main);

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

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

@Override
public void onClick(View arg0) {

    switch(arg0.getId()){

    case R.id.Order:try{ onbuttonclick();} catch(JSONException e){}

                    break;

    }
}

    private void onbuttonclick() throws JSONException {

    TableRow[] tr = new TableRow[arry1.length];
    final TextView[] tx = new TextView[arry1.length];
    GridLayout gl = new GridLayout(S2.this);
    gl.setRowCount(arry1.length);
    gl.setColumnCount(1);
    final TableLayout tl = (TableLayout) findViewById(R.id.tb1);
    TableRow row = null;

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

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

        tx[i] = new TextView(S2.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(S2.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 TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));   

        int buttonsInRow = 0;
        int numRows = tl.getChildCount();

        if( numRows > 0 ){
            row = (TableRow) tl.getChildAt( numRows - 1 );
            buttonsInRow = row.getChildCount();         
        }

        if( numRows == 0 || buttonsInRow == 3 ){
            row = new TableRow( this );
            tl.addView( row );
            buttonsInRow = 0;
        }
        if( buttonsInRow < 3 ){
            Button b = new Button( this );
            b.setText("Cancel");
            b.setId(i);
            row.addView( b, 100, 50 );


        }
        b.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                b = (Button) findViewById(i);
                tl.removeView(tx[i]);
            }
        });


    }
}

enter image description here

这是我的屏幕,当我点击旁边的按钮时,我想要清除每个项目。

2 个答案:

答案 0 :(得分:0)

你可以在

中实现你所需要的
 onClick(View arg0)

获取arg0父级。检查它是

 instanceof

表格行或您设置的标记,然后将其可见性设置为GONE

答案 1 :(得分:0)

您可以使用与removeView类似的方式使用addView方法:

tr[i].removeView(<YourTextView>;