我正在开发聊天应用,用户可以在其中发送短信和图片 为了这个目的我使用listview只有文本消息在数据集listview以适当的方式显示正确的数据,当用户发送图像时,图像正确添加列表,而当我调用notify datasetchanged数据集位置更改时,例如图像添加0显示在3或第四名
package com.exception.chatapp;
import java.util.ArrayList;
import java.util.HashMap;
import android.content.Context;
import android.net.Uri;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class ChatAdapter extends BaseAdapter {
Context context;
ArrayList<HashMap<String, String>> data;
LayoutInflater inflator;
String imageurl = "http://www.example.com/webservices/";
public ChatAdapter(Context context, ArrayList<HashMap<String, String>> data) {
this.context = context;
this.data = data;
inflator = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
// return data.size();
return data.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertview, ViewGroup parentview) {
// TODO Auto-generated method stub
ViewHolder holder;
if (convertview == null) {
convertview = inflator.inflate(R.layout.chat_item, parentview,false);
holder = new ViewHolder();
holder.message = (TextView) convertview
.findViewById(R.id.txtusermessage);
holder.userimage = (ImageView) convertview
.findViewById(R.id.userimage);
convertview.setTag(holder);
} else {
holder = (ViewHolder) convertview.getTag();
}
holder.userimage.setVisibility(View.GONE);
HashMap<String, String> map = data.get(position);
if (!map.get("user_name").equals("me")) {
if (map.get("type").equals("text")) {
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
holder.message.setLayoutParams(params);
holder.message.requestLayout();
holder.message.setText(map.get("message"));
holder.message.setBackgroundResource(R.drawable.farhan_blue);
} else if (map.get("type").equals("image")) {
holder.userimage.setVisibility(View.VISIBLE);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
200,
200);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
holder.userimage.setLayoutParams(params);
holder.userimage.requestLayout();
holder.userimage.setBackgroundResource(R.drawable.farhan_blue);
holder.message.setVisibility(View.GONE);
ImageLoader loader = new ImageLoader(context);
Log.d("image_url", imageurl + map.get("image_url"));
loader.DisplayImage(imageurl + map.get("image_url"),
holder.userimage);
} else if (map.get("type").equals("gps")) {
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
holder.message.setLayoutParams(params);
holder.message.requestLayout();
holder.message.setText(map.get("lat") + "," + map.get("lng"));
holder.message.setBackgroundResource(R.drawable.farhan_blue);
}
else if (map.get("type").equals("contact")) {
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
holder.message.setLayoutParams(params);
holder.message.requestLayout();
holder.message.setText(map.get("name") + "\n"
+ map.get("number"));
holder.message.setBackgroundResource(R.drawable.farhan_blue);
}
} else {
if (map.get("type").equals("text")) {
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
holder.message.setLayoutParams(params);
holder.message.requestLayout();
holder.message.setText(map.get("message"));
holder.message.setBackgroundResource(R.drawable.farhan_white);
} else if (map.get("type").equals("image")) {
holder.message.setVisibility(View.GONE);
holder.userimage.setVisibility(View.VISIBLE);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
200,
200);
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
holder.userimage.setLayoutParams(params);
holder.userimage.requestLayout();
holder.userimage.setBackgroundResource(R.drawable.farhan_white);
if (map.get("from") != null) {
Log.d("image_url",map.get("image_url"));
holder.userimage
.setImageURI(Uri.parse(map.get("image_url")));
} else {
ImageLoader loader = new ImageLoader(context);
Log.d("image_url", imageurl + map.get("image_url"));
loader.DisplayImage(imageurl + map.get("image_url"),
holder.userimage);
}
}
else if (map.get("type").equals("gps")) {
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
holder.message.setLayoutParams(params);
holder.message.requestLayout();
holder.message.setText(map.get("lat") + "," + map.get("lng"));
holder.message.setBackgroundResource(R.drawable.farhan_white);
} else if (map.get("type").equals("contact")) {
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
holder.message.setLayoutParams(params);
holder.message.requestLayout();
holder.message.setText(map.get("name") + "\n"
+ map.get("number"));
holder.message.setBackgroundResource(R.drawable.farhan_white);
}
}
return convertview;
}
static class ViewHolder {
TextView message;
ImageView userimage;
}
}
这是我的listview我将width和height设置为match_parent,同时在我的布局xml中的两个布局的上方和下方
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/chatlayout"
android:layout_alignParentLeft="true"
android:layout_below="@+id/linearLayout1"
android:divider="@null"
android:dividerHeight="0dp"
android:stackFromBottom="true" >
</ListView>
关于活动结果我将图像添加到listview
if (resultCode == RESULT_OK && requestCode == 1 && data1!=null) {
try {
Uri selectedImage = data1.getData();
filepath = getRealPathFromURI(selectedImage);
Log.d("path", filepath);
ChatItem item = new ChatItem();
item.setUsername("me");
item.setMessage("");
item.setType("image");
item.setImage_uri(filepath);
item.setFrom("local");
data.add(item);
adapter.notifyDataSetChanged();
list.smoothScrollToPosition(data.size() - 1);
message.setText("");
new UploadFile().execute(filepath, id, rid, "image");
} catch (Exception ex) {
ex.printStackTrace();
}
}
答案 0 :(得分:1)
根据您的描述和提供的代码,我认为问题是由于您使用条件切换视图布局的方式与ListView
提供的一些内部优化之间的冲突造成的。
ListView
的优化方式是回收以前膨胀的视图。这旨在避免为每个项目充气新布局。这种回收,结合您用来选择项目类型的逻辑 - 显示和隐藏每个项目的不同子元素 - 可能是导致此问题的原因,因为当您添加图片时可能无法创建新视图,只是更新一个旧的。
我会做什么我改变了你切换项目视图的方式,使得每种类型的视图都有不同的模型对象 - 例如我将以下列方式设置我的适配器
View v = convertView;
Object o = data.get(position);
LayoutInflaer inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(o != null){
if(o instanceof ImageMessage){
if(v == null){
v = inflater.inflate(R.layout.image_message);
}
createImageMessagView(v, (ImageMessage) o);
} else if (o instanceOf textMessage){
// handle different object type
}
其中createImageMessageView是您可以通过id找到相关布局子视图并设置其内容的位置。
此方法允许您为每个项目类使用不同的布局。这样,您不会创建一个视图,将其存储在视图持有者中,然后尝试稍后修改其内容。相反,您正在为自定义布局(如果尚未存在)进行膨胀,该布局旨在显示该项目类型的内容。
答案 1 :(得分:0)
问题在这里
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
我将此更改为
@Override
public ChatItem getItem(int arg0) {
// TODO Auto-generated method stub
return data.get(arg0);
}
我的问题已经解决,但还有另一个问题
plz dnt使用静态视图持有者导致内存泄漏错误