2012-10-19 49 views
0

我有一對動態創建表中的TableRows ...每行都有3個Textviews和一個圖像按鈕來刪除行,如果用戶願意...創建行已成功實現,但現在當用戶點擊該行上的刪除按鈕時會出現問題。該行不會按預期方式刪除,我該如何實現?從Android動態創建TableRow表

我的代碼如下

 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

沒有error.Its根本不被刪除..我想簡單地刪除表中的行 –

回答

0

通過查看您的視圖層次結構,刪除代碼sho ULD是:

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

我也建議你不要包裝每個您像你一樣添加到TableRow在另一個無用LinearLayout的意見。