2012-04-17 100 views
2

我使用此代碼通過buttonClick事件從我的數據庫動態打印虛值。
用於刪除數據庫條目的buttonClick事件存在於循環中。Android動態RelativeLayout彼此重疊

這裏我的代碼:

RelativeLayout rl = (RelativeLayout) findViewById(R.id.relativeLayout3); 

    final DatabaseHandler dbpin = new DatabaseHandler(this); 
    // Log.d("Reading: ", "Reading all tasks.."); 
    List<Detail> detail1 = dbpin.getAllDetail();  
    Button[] button=new Button[1000]; 
      for (Detail cn : detail1) { 
       String log = cn.getTitle(); 
      final int i = cn.getID(); 

      button[i] = new Button(this); 
     button[i].setText("Delete"); 
     button[i].setTextSize(10); 

     button[i].setId(2000 + i); 
    int width = 80; 
    int height = 60; 

    TextView textview = new TextView(this); 
    textview.setText(log); 
    textview.setWidth(200); 
    textview.setTextSize(20); 
    textview.setPadding(0, 10, 0, 0); 
    textview.setId(2000 + i); 

    if (i == 0) { 
     RelativeLayout.LayoutParams rlp2 = new RelativeLayout.LayoutParams(
       RelativeLayout.LayoutParams.FILL_PARENT, 
       RelativeLayout.LayoutParams.WRAP_CONTENT); 
     rlp2.addRule(RelativeLayout.ALIGN_PARENT_TOP); 
     rlp2.addRule(RelativeLayout.ALIGN_PARENT_LEFT); 
     textview.setLayoutParams(rlp2); 
     rl.addView(textview); 
     RelativeLayout.LayoutParams rlp1 = new RelativeLayout.LayoutParams(
       width, height); 
     rlp1.addRule(RelativeLayout.ALIGN_PARENT_TOP); 
     rlp1.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); 
     button[i].setLayoutParams(rlp1); 
     rl.addView(button[i]); 
    } else { 
     RelativeLayout.LayoutParams rlp2 = new RelativeLayout.LayoutParams(
       RelativeLayout.LayoutParams.FILL_PARENT, 
       RelativeLayout.LayoutParams.WRAP_CONTENT); 
     rlp2.addRule(RelativeLayout.ALIGN_PARENT_LEFT); 
     rlp2.addRule(RelativeLayout.BELOW, button[i].getId() - 1); 
     textview.setLayoutParams(rlp2); 
     rl.addView(textview); 
     RelativeLayout.LayoutParams rlp1 = new RelativeLayout.LayoutParams(
       width, height); 
     rlp1.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); 
     rlp1.addRule(RelativeLayout.BELOW, button[i].getId() - 1); 
     button[i].setLayoutParams(rlp1); 
     rl.addView(button[i]); 
    } 


    button[i].setOnClickListener(new Button.OnClickListener() { 
     public void onClick(View v) { 


      Intent myIntent = new Intent(v.getContext(), details.class); 
      Detail detail = new Detail(); 
      detail.setID(i); 
      dbpin.deleteDetail(detail); 
      startActivityForResult(myIntent, 1);   
     } 
     });      
} 

繼數據庫處理程序代碼,檢索使用循環從數據庫中的所有細節:

// Getting All detail 
        public List<Detail> getAllDetail() { 
         List<Detail> detailList = new ArrayList<Detail>(); 
         // Select All Query 
         String selectQuery = "SELECT * FROM " + TABLE_DETAIL; 

         SQLiteDatabase db = this.getWritableDatabase(); 
         Cursor cursor = db.rawQuery(selectQuery, null); 

         // looping through all rows and adding to list 
         if (cursor.moveToFirst()) { 
          do { 
           Detail detail = new Detail(); 
           detail.setID(Integer.parseInt(cursor.getString(0))); 
           detail.setTitle(cursor.getString(1)); 
           detail.setDetail(cursor.getString(2)); 

           // Adding contact to list 
           detailList.add(detail); 
          } while (cursor.moveToNext()); 
         } 

         // return contact list 
         return detailList; 
        } 

// Deleting single detail 
        public void deleteDetail(Detail detail) { 
         SQLiteDatabase db = this.getWritableDatabase(); 
         db.delete(TABLE_DETAIL, KEY_DETID + " = ?", 
           new String[] { String.valueOf(detail.getID()) }); 
         db.close(); 
        } 

起初的佈局是正常的。刪除第一個或最後一個數據行不會導致任何更改,但是如果中間的一行被刪除,則佈局會相互重疊。

請給我建議,以清除這個邏輯錯誤。

回答

2

好吧,我已經理解你的問題。問題在於,您正在使用相對佈局作爲您的父佈局,並在其中添加您的所有與子相關的佈局。現在,如果您刪除了第一個相對佈局,那麼它會自動與其父級對齊,這樣就沒有問題了。

如果刪除最後一個相對佈局,那麼也不會出現問題。

現在,您已將所有相對佈局對齊到上面的佈局,所以如果刪除上面的佈局,它會自動對齊其父級。

解決方法很簡單。將您的父級佈局用作線性佈局,以便您不需要將相對佈局與其上面的佈局對齊。它會自動安排在一個線性的方式....

RelativeLayout rl =(RelativeLayout)findViewById(R.id.relativeLayout3);在xml文件中將此佈局轉換爲linearlayout。

的代碼,它可以幫助你:從這裏

RelativeLayout rl = new RelativeLayout(getApplicationContext()); 
    RelativeLayout.LayoutParams lp_btn = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, 
                    LayoutParams.WRAP_CONTENT); 
    lp_btn.addRule(RelativeLayout.ALIGN_PARENT_LEFT); 
    Button temp_button = new Button(getApplicationContext()); 
    temp_button.setText("button"); 
    rl.addView(temp_button, lp_btn); 
    TextView tv = new TextView(getApplicationContext()); 
    tv.setText("bharat"); 
    tv.setBackgroundColor(Color.GREEN); 
    RelativeLayout.LayoutParams lp_tv = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, 
                    LayoutParams.WRAP_CONTENT); 
    lp_tv.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); 
    rl.addView(tv, lp_tv); 
    lp.addView(rl); 

// for循環將在這裏結束

我認爲你應該使用的ListView

LinearLayout lp = (LinearLayout) findViewById(R.id.LinearLayout1); 

// for循環的開始爲了你的目的,它會更好。無論如何,這也將工作,你必須爲你的目的管理relativelayout數組和按鈕數組。

+0

謝謝..得到了解決方案..但這使得像這樣的佈局..在textview下面的按鈕...但我想要並排。 – 2012-04-17 06:01:35

+0

爲您可以創建簡單的child relativelayout添加您的按鈕和在他們的textview,然後將其添加到線性佈局它會減少你的工作,如果你想刪除一行,那麼你可以完全刪除相對佈局。你有沒有明白或者我應該向你發送一些小小的代碼... – 2012-04-17 06:07:28

+0

對不起..我對android很陌生..你能否建議我如何實現這個.. – 2012-04-17 06:12:47