从Android平板电脑中删除动态创建的表行

时间:2012-10-19 07:42:32

标签: android

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

我的代码如下

     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);          

        }
    });

然而它不能正常工作它会从上到下删除行,而不管我删除的表格,它总是从上到下开始

1 个答案:

答案 0 :(得分:1)

通过查看视图层次结构,删除代码应为:

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

我还建议你不要像以前那样将你添加到TableRow的每个视图包装在另一个无用的LinearLayout中。