我正在使用列表视图来显示一些数据并将其与自定义适配器一起使用,现在在一个时间实例中,我想将图像视图显示为其中的列表项。在这种情况下,我做了动态线性布局并在其中添加了图像视图。现在我希望在某些情况下,这个图像视图布局应该作为列表视图的最后一个列表项显示为添加到列表中的新项目。
动态生成布局
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 :(得分:4)
我认为MergeAdapter完美地解决了您的问题。
答案 1 :(得分: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);
答案 2 :(得分: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;
}
答案 3 :(得分:-1)
notifiyDataSetChange()方法用于更新适配器