0

我正在开发聊天应用程序,用户可以在其中发送文本消息和图像我为此目的使用列表视图,当只有文本消息在数据集中时,列表视图以正确的方式显示正确的数据,当用户发送图像时,图像在列表中正确添加时我调用通知 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();
        }
    }
4

2 回答 2

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
    }

其中 createImageMes​​sageView 是您可以通过 id 找到相关布局子视图并设置其内容的位置。

这种方法允许您为每个项目类使用不同的布局。这样,您不会创建一个视图,将其存储在视图持有者中,然后再尝试修改其内容。相反,如果自定义布局尚不存在,则您正在扩充自定义布局,该布局旨在显示该项目类型的内容。

于 2013-07-15T12:59:09.650 回答
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 使用静态视图持有人它会导致内存泄漏错误

于 2013-07-16T11:51:40.017 回答