2014-02-24 59 views
1

我正在爲當前視圖動態添加布局。即使問題是,即使我有超過兩個值的遊標只有一個視圖充氣。動態擴展視圖的問題

這意味着舊數據被覆蓋。但我想在phoneCrsr中添加每個記錄的新視圖。

我在做什麼錯?

  Cursor phoneCrsr = database.rawQuery(phoneSql, null); 
      while(phoneCrsr.moveToNext()){ 
       phone_number = new ArrayList<String>();     
       String number = phoneCrsr.getString(phoneCrsr.getColumnIndex(MySQLiteHelper.COLUMN_PHN_NUMBER)); 

       if(!number.isEmpty()){ 
        phone_number.add(number);     

        LayoutInflater inflator = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
        View view = inflator.inflate(R.layout.phone_number_textview, null); 

        // fill in any details dynamically here 
        TextView phoneTv = (TextView) view.findViewById(R.id.phone_number); 
        phoneTv.setText(number); 

        // insert into main view 
        LinearLayout phoneLayout = (LinearLayout) findViewById(R.id.phone_info); 
        phoneLayout.setVisibility(View.VISIBLE); 
        ((ViewGroup) phoneLayout).addView(view, 0, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT)); 

        //phoneLayout.setPadding(20,0,0, 0); 
        //phoneLayout.setBackgroundColor(Color.WHITE);  
       }     
       Log.e("PHONE DETAIL:",phone_number.toString()); 
      } 
      phoneCrsr.close(); 
+2

您需要爲每條記錄創建一個視圖?爲什麼使用textview刪除視圖。使用listview和textviews它的動態 – Raghunandan

+0

@Raghunandan:因爲我的外部視圖是'scrollview',所以它在某處閱讀我不應該在'scrollview'裏面使用'listview'。 – astuter

+0

然後不需要scrollview coz listview滾動自己 – Raghunandan

回答

2

當您添加視圖到你的「主」的佈局,你告訴它要FILL_PARENT在兩個方向:

((ViewGroup) phoneLayout).addView(view, 0, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT)); 

如果添加了佔據整個LinearLayout一個觀點,那就是所有你永遠不會看到。當您添加另一個時,它在一個方向上「脫離邊緣」(取決於orientation)。如果您嘗試垂直添加它們,請將高度更改爲WRAP_CONTENT。對於水平佈局,更改寬度。

您可能還想簡化您的addView通話。首先,它不應該被投入ViewGroup。您也可以完全跳過LayoutParams構造函數,只需將寬度和高度直接傳遞給父項即可,其中包含simpler call。像這樣的東西應該可以工作:

phoneLayout.addView(view, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.FILL_PARENT); 

總而言之,拉古南丹的評論是最好的「答案」。你可能應該使用ListView這個,因爲它正是爲它設計的。

+0

感謝您的解釋。您對佈局方向的評論解決了我的問題。 – astuter