2012-12-06 150 views
1

我正在使用列表視圖來顯示一些數據並將其與自定義適配器一起使用,現在是時間實例,我想將圖像視圖顯示爲列表項它。在這種情況下,我做了動態線性佈局,並在其中添加了圖像視圖。現在,我希望在某些情況下,此圖像視圖佈局應作爲添加到列表中的新項目顯示爲列表視圖最後一個列表項目。將動態線性佈局添加到列表視圖

Genarating佈局動態

LinearLayout linearLayout = new LinearLayout(UserChatActivity.this); 

      LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
        LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); 

      ImageView img = new ImageView(UserChatActivity.this); 
      img.setImageBitmap(BitmapFactory.decodeFile(picturePath)); 


      linearLayout.addView(img, layoutParams); 


      mList.addView(linearLayout); 

但它給命名的錯誤: - 視圖不能直接在adapterViews增加。我知道我們必須誇大適配器的視圖,但是如何將新視圖添加到列表中以前的數據中。

感謝

+0

不能添加線性佈局到列表視圖因爲listview不是容器佈局。 –

+0

@DipakKeshariya我知道這件事。我只是問什麼是解決方案呢? –

+0

如果您嘗試在列表視圖的末尾添加視圖,請嘗試列表視圖頁腳 – Atrix1987

回答

2

你可以試試這個,把代碼放到你的適配器的getView()中。

if (view == null) 
{ 
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
        view = vi.inflate(R.layout.list, null); 
        **// list.xlm is your layout** 
} 

**lnHolder is in list.xml** 
LinearLayout lnHolder = (LinearLayout) view.findViewById(R.id.lnHolder); 

LinearLayout linearLayout = new LinearLayout(UserChatActivity.this); 

LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
         LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);  
ImageView img = new ImageView(UserChatActivity.this); 
img.setImageBitmap(BitmapFactory.decodeFile(picturePath)); 
linearLayout.addView(img, layoutParams); 
lnHolder.addView(linearLayout); 
+0

謝謝。我正在嘗試。 –

1

你getView()方法將這個樣子,

public View getView(int position, View convertView, ViewGroup parent) { 

      if (position == 10) //if you already know the position use static position 
       // else mydatalist.get(position).hasImage()== true and 
       // make one function hasImage in your data structure which checks weather its a image or text 
      { 
       convertView = _inflater.inflate(R.layout.simplelinearlayout, 
         null); 

       LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
         LinearLayout.LayoutParams.WRAP_CONTENT, 
         LinearLayout.LayoutParams.WRAP_CONTENT); 

       ImageView img = new ImageView(UserChatActivity.this); 
       img.setImageBitmap(BitmapFactory.decodeFile(picturePath)); 

       convertView.addView(img, layoutParams); 
      } else { 
       if (convertView == null) { 
        convertView = _inflater.inflate(R.layout.missingitem_row, 
          null); 
        viewHolder = new ViewHolder(); 
        viewHolder.textTitle = (TextView) convertView 
          .findViewById(R.id.txtTitle); 

        convertView.setTag(viewHolder); 

       } else { 
        viewHolder = (ViewHolder) convertView.getTag(); 
       } 

       viewHolder.textTitle.setText(messageText); 
      } 
      return convertView; 
     } 
相關問題