自定义适配器有九个补丁图像

时间:2015-01-30 09:37:33

标签: android xmpp custom-adapter

我正在尝试使用xmpp在android中创建一个聊天应用程序。我能够与简单的数组适配器聊天但是当我尝试在listview上设置自定义适配器时,我的代码中出现错误。请帮我。我哪里错了。

我的自定义适配器代码:

public class ChatArrayAdapter extends ArrayAdapter
{
    private TextView chatText;

    private LinearLayout singleMessageContainer;

    private Activity activity;

    public ChatArrayAdapter(Context context, int textViewResourceId, List chatList) {
        super(context, textViewResourceId, chatList);
    }


    public void add(ChatMessage object) {
        chatMessageList.add(object);
        super.add(object);
    }

    public ChatArrayAdapter(Context context, int textViewResourceId) {
        super(context, textViewResourceId);
    }


    public int getCount() {
        return chatMessageList.size();
    }

    public ChatMessage getItem(int index) {
        return (ChatMessage) chatMessageList.get(index);
    }


    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        if (row == null) {
            LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = inflater.inflate(R.layout.chat_singlemessage, parent, false);
        }
        singleMessageContainer = (LinearLayout) row.findViewById(R.id.singleMessageContainer);
        ChatMessage chatMessageObj = getItem(position);
        chatText = (TextView) row.findViewById(R.id.singleMessage);
        chatText.setText(chatMessageObj.message);
        chatText.setBackgroundResource(chatMessageObj.left ? R.drawable.bubbleaaa : R.drawable.bubblebbb);
        singleMessageContainer.setGravity(chatMessageObj.left ? Gravity.LEFT : Gravity.RIGHT);
        return row;
    }

    public Bitmap decodeToBitmap(byte[] decodedByte) {
        return BitmapFactory.decodeByteArray(decodedByte, 0, decodedByte.length);
    }


}

ChatMessage类:

public class ChatMessage
{
    public boolean left;
public String message;

public ChatMessage(boolean left, String message) {
    super();
    this.left = left;
    this.message = message;
}
}

错误:

01-30 14:39:05.663  10813-10813/com.example.welcome.kids_chat E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.welcome.kids_chat, PID: 10813
java.lang.ClassCastException: java.lang.String cannot be cast to com.example.welcome.kids_chat.ChatMessage
        at com.example.welcome.kids_chat.Chat_Screen$ChatArrayAdapter.getItem(Chat_Screen.java:307)
        at com.example.welcome.kids_chat.Chat_Screen$ChatArrayAdapter.getView(Chat_Screen.java:317)
        at android.widget.AbsListView.obtainView(AbsListView.java:2301)
        at android.widget.ListView.measureHeightOfChildren(ListView.java:1274)
        at android.widget.ListView.onMeasure(ListView.java:1186)
        at android.view.View.measure(View.java:16677)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5286)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
        at android.view.View.measure(View.java:16677)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5286)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
        at android.view.View.measure(View.java:16677)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5286)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
        at android.view.View.measure(View.java:16677)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5286)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2421)
        at android.view.View.measure(View.java:16677)
        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1927)
        at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1119)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1301)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1006)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5652)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
        at android.view.Choreographer.doCallbacks(Choreographer.java:574)
        at android.view.Choreographer.doFrame(Choreographer.java:544)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5426)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
        at dalvik.system.NativeStart.main(Native Method)

3 个答案:

答案 0 :(得分:0)

尝试将列出聊天列表更改为列出<ChatMessage>聊天列表以及列出<ChatMessage> chatMessageList

答案 1 :(得分:0)

  private List<ChatMessage> chatMessageList = new ArrayList();

并将适配器声明为

public class ChatArrayAdapter extends ArrayAdapter<ChatMessage>{

这将使arrayadapter默认处理ChatMessage。

然后您可以删除

  public void add(ChatMessage object) {
    chatMessageList.add(object);
    super.add(object);
}

public ChatMessage getItem(int index) {
    return (ChatMessage) chatMessageList.get(index);
}

它们将自动属于ChatMessage

类型

答案 2 :(得分:0)

将BaseAdapter与ViewHolder设计模式一起使用:

public class ChatAdapter extends BaseAdapter
{
    private Context context;
    private List<ChatMessage> chatList;

    public ChatAdapter(Context context,List<ChatMessage> chatList) {
        this.context=context;
        this.chatList=chatList;
    }


    public void add(ChatMessage object) {
        if(chatList==null){
            chatList = new ArrayList<ChatMessage>();
        }
        chatList.add(object);
        notifyDataSetChanged();
    }

    public int getCount() {
        return chatList.size();
    }

    @Override
    public ChatMessage getItem(int position) {
        return chatList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.chat_singlemessage, null, false);
            holder.chatText = (TextView) convertView.findViewById(R.id.singleMessage);
            holder.singleMessageContainer = (LinearLayout) convertView.findViewById(R.id.singleMessageContainer);
            convertView.setTag(holder);
        }else{
            holder =(ViewHolder) convertView.getTag();
        }
        holder.chatText.setText(chatList.get(position).message);
        holder.chatText.setBackgroundResource(chatList.get(position).left ? context.getResources(R.drawable.bubbleaaa) : context.getResources(R.drawable.bubbleaaa));
        holder.singleMessageContainer.setGravity(chatList.get(position).left ? Gravity.LEFT : Gravity.RIGHT);
        return convertView;
    }

    class ViewHolder{
        TextView chatText;
        LinearLayout singleMessageContainer;
    }

    public Bitmap decodeToBitmap(byte[] decodedByte) {
        return BitmapFactory.decodeByteArray(decodedByte, 0, decodedByte.length);
    }
}