我正在开发聊天应用程序,用户可以在其中发送文本消息和图像我为此目的使用列表视图,当只有文本消息在数据集中时,列表视图以正确的方式显示正确的数据,当用户发送图像时,图像在列表中正确添加时我调用通知 datasetchanged 数据集位置更改,例如在 0 处添加的图像显示在第 3 或第 4 位置
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;
}
}
这是我的列表视图,我在布局 xml 中的两个布局上方和下方将宽度和高度设置为 match_parent
<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>
在活动结果上,我将图像添加到列表视图
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();
}
}