0

我在一个表中有几个动态创建的 TableRows ...每行有 3 个 Textview 和一个图像按钮,如果用户希望删除该行...创建行已成功实现,但现在问题出现在用户单击时该行上的删除按钮。该行未按预期删除。我该如何实现?

我的代码如下

     public void addBody(String Code,String Type,String Quantity){
    /** Create a TableRow dynamically **/
     mTblrow = new TableRow(this);

    /** Creating a TextView to add to the row **/
    TextView label = new TextView(this);
    label.setText(Code);
    label.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));          
    label.setPadding(5, 5, 5, 5);
    label.setBackgroundColor(Color.GRAY);

    LinearLayout Ll = new LinearLayout(this);
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);       
    params.setMargins(5, 5, 5, 5);

    Ll.addView(label,params);
    mTblrow.addView((View)Ll); // Adding textView to tablerow.

    /** Creating Qty Button **/
    TextView type = new TextView(this);
    type.setText(Type);
    type.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    type.setPadding(5, 5, 5, 5);
    type.setBackgroundColor(Color.GRAY);

    Ll = new LinearLayout(this);
    params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
            LayoutParams.WRAP_CONTENT);
    params.setMargins(0, 5, 5, 5);
    Ll.addView(type,params);
    mTblrow.addView((View)Ll); // Adding textview to tablerow.

    /** Creating Qty Button **/
    TextView quantity = new TextView(this);
    quantity.setText(Quantity);
    quantity.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    quantity.setPadding(5, 5, 5, 5);
    quantity.setBackgroundColor(Color.GRAY);

    Ll = new LinearLayout(this);
    params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);

    params.setMargins(0, 5, 5, 5);
    Ll.addView(quantity,params);
    mTblrow.addView((View)Ll); 
    /** Creating Qty Button **/
    ImageView delete = new ImageView(this);
    delete.setImageDrawable(drawable);
    delete.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    delete.setPadding(5, 5, 5, 5);

    Ll = new LinearLayout(this);
    params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);

    params.setMargins(0, 5, 5, 5);
    Ll.addView(delete,params);
    mTblrow.addView((View)Ll); 


     // Add the TableRow to the TableLayout

    delete.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            //This part should handle the logic to delete the row
            //This is where am hitting a snag..Please Help
            mTable.removeView(mTblrow);


        }
    });
    mTable.addView(mTblrow, new TableLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));               

}

编辑:我尝试使用下面的代码进行点击

   delete.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            ((ViewManager)mTblrow.getParent()).removeView(mTblrow);

            mTable.removeViewAt(rowIndex);          

        }
    });

但是它没有按预期工作,因为它从上到下删除行,而不管我删除的 tablerow,它总是从上到下开始

4

1 回答 1

1

通过查看您的视图层次结构,删除代码应该是:

delete.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            mTable.removeViewAt(((View) ((View) v.getParent()).getParent()));          
        }
});

我还建议您不要像您一样将添加到TableRow另一个无用的视图中的每个视图包装起来LinearLayout

于 2012-10-19T17:09:35.853 回答