1

我正在使用列表视图在其上显示一些数据并将其与自定义适配器一起使用,现在在某个时间实例中,我想将图像视图显示为其中的列表项。对于这种情况,我制作了动态线性布局并在其中添加了图像视图。现在我希望在某些情况下,这个图像视图布局应该作为列表视图的最后一个列表项显示为添加到列表中的新项目。

动态生成布局

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

但它给出了一个名为: - 视图不能直接添加到适配器视图中的错误。我知道我们必须为适配器膨胀视图,但是如何使用列表中的先前数据添加新视图。???

谢谢

4

4 回答 4

4

我认为MergeAdapter完美地解决了您的问题。

于 2012-12-06T07:30:20.337 回答
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);
于 2012-12-06T07:41:05.717 回答
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;
        }
于 2012-12-06T07:45:01.243 回答
-1

notifyDataSetChange() 方法用于更新适配器

listView 动态添加项

于 2012-12-06T08:23:28.167 回答