将图像(表情符号)放在ListView上

时间:2017-07-17 14:11:07

标签: android xml listview emoticons

我想在聊天中添加新功能。我有一个代码,允许我插入聊天气泡,它工作得很好(12)。我尝试使用类似于以前的适配器和消息(用于文本)类来创建消息和适配器(用于图像)类。我希望它不像Messenger那样工作。当消息框为空时,发送按钮是表情符号按钮,它应该在ListView上放置一个表情符号(我跳过在按下的长度内增加表情符号大小)。当消息框有文本时,表情符号按钮将变为发送按钮,此按钮应将该框的内容放在ListView上。我尝试了但是我试过的一切都失败了(当我把setAdapter放到TextChangedListener时按钮什么都不做,当我将setAdapter严格设置为emojiAdapter时,Android Studio会抛出Null Pointer Exception)。以下是我的代码:

错误日志:

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.bk.cryptit, PID: 2720
              java.lang.NullPointerException
                  at com.bk.cryptit.Chats.sendEmojiMessage(Chats.java:115)
                  at com.bk.cryptit.Chats$2.onClick(Chats.java:77)
                  at android.view.View.performClick(View.java:4438)
                  at android.view.View$PerformClick.run(View.java:18422)
                  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:5017)
                  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:779)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
                  at dalvik.system.NativeStart.main(Native Method)
Application terminated.

ChatMessage.java

package com.bk.cryptit;

import java.util.Random;

public class ChatMessage {

public String body, sender, receiver, senderName;
public String Date, Time;
public String msgid;
public boolean isMine;

public ChatMessage(String Sender, String Receiver, String messageString,
                   String ID, boolean isMINE) {
    body = messageString;
    isMine = isMINE;
    sender = Sender;
    msgid = ID;
    receiver = Receiver;
    senderName = sender;
}

public void setMsgID() {

    msgid += "-" + String.format("%02d", new Random().nextInt(100));
    ;
}
}

ChatAdapter.java

package com.bk.cryptit;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.text.InputFilter;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.ArrayList;

import static android.R.id.list;

public class ChatAdapter extends BaseAdapter {

private static LayoutInflater inflater = null;
ArrayList<ChatMessage> chatMessageList;

public ChatAdapter(Activity activity, ArrayList<ChatMessage> list) {
    chatMessageList = list;
    inflater = (LayoutInflater) activity
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}

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

@Override
public Object getItem(int position) {
    return position;
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ChatMessage message = (ChatMessage) chatMessageList.get(position);
    View vi = convertView;
    if (convertView == null)
        vi = inflater.inflate(R.layout.chatbubble, null);

    TextView msg = (TextView) vi.findViewById(R.id.message_text);
    msg.setText(message.body);
    msg.setTextSize(16);
    msg.setSingleLine(false);
    LinearLayout layout = (LinearLayout) vi
            .findViewById(R.id.bubble_layout);
    LinearLayout parent_layout = (LinearLayout) vi
            .findViewById(R.id.bubble_layout_parent);
    layout.setPadding(15,11,15,15);

    // if message is mine then align to right
    if (message.isMine) {
        layout.setBackgroundResource(R.drawable.chat_bubble_right);
        parent_layout.setGravity(Gravity.RIGHT);
    }
    // If not mine then align to left
    else {
        layout.setBackgroundResource(R.drawable.chat_bubble_left);
        parent_layout.setGravity(Gravity.LEFT);
    }
    if (message.isMine) {
        msg.setTextColor(Color.WHITE);
    }

    else {
        msg.setTextColor(Color.BLACK);
    }

    return vi;
}

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

EmojiMessage.java

package com.bk.cryptit;

import android.widget.ImageView;

import java.util.Random;

public class EmojiMessage {
public String sender, receiver, senderName;
public ImageView body;
public String Date, Time;
public String msgid;
public boolean isMine;// Did I send the message.

public EmojiMessage(String Sender, String Receiver, ImageView emoji,
                   String ID, boolean isMINE) {
    body = emoji;
    isMine = isMINE;
    sender = Sender;
    msgid = ID;
    receiver = Receiver;
    senderName = sender;
}
public void setMsgID() {

    msgid += "-" + String.format("%02d", new Random().nextInt(100));
    ;
}
}

EmojiAdapter.java

package com.bk.cryptit;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.text.InputFilter;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class EmojiAdapter extends BaseAdapter{
private static LayoutInflater inflater = null;
ArrayList<EmojiMessage> chatMessageListEmoji;

public EmojiAdapter(Activity activity, ArrayList<EmojiMessage> list) {
    chatMessageListEmoji = list;
    inflater = (LayoutInflater) activity
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}

@Override
public int getCount() {
    return chatMessageListEmoji.size();
}

@Override
public Object getItem(int position) {
    return position;
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    EmojiMessage message = (EmojiMessage) chatMessageListEmoji.get(position);
    View vi = convertView;
    if (convertView == null)
        vi = inflater.inflate(R.layout.imagechatbubble, null);

    ImageView msgEmoji = (ImageView) vi.findViewById(R.id.message_image);
    LinearLayout layout = (LinearLayout) vi
            .findViewById(R.id.bubble_layout);
    LinearLayout parent_layout = (LinearLayout) vi
            .findViewById(R.id.bubble_layout_parent);

    // if message is mine then align to right
    if (message.isMine) {
        parent_layout.setGravity(Gravity.RIGHT);
    }
    // If not mine then align to left
    else {
        parent_layout.setGravity(Gravity.LEFT);
    }

    return vi;
}

public void add(EmojiMessage object) {
    chatMessageListEmoji.add(object);
}
}

Chats.java

package com.bk.cryptit;

import java.util.ArrayList;
import java.util.Random;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;

import com.bk.cryptit.ChatAdapter;
import com.bk.cryptit.ChatMessage;
import com.bk.cryptit.CommonMethods;
import com.bk.cryptit.EmojiMessage;
import com.bk.cryptit.EmojiAdapter;
import com.bk.cryptit.R;

public class Chats extends Fragment {

private EditText msg_edittext;
private String user1 = "khushi", user2 = "khushi1";
private Random random;
public static ArrayList<ChatMessage> chatlist;
public static ArrayList<EmojiMessage> emojilist;
public static EmojiAdapter emojiAdapter;
public static ChatAdapter chatAdapter;
ListView msgListView;
String editText;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.activity_welcome, container, false);
    random = new Random();
    msg_edittext = (EditText) view.findViewById(R.id.editText);
    msgListView = (ListView) view.findViewById(R.id.messegesListView);
    editText = msg_edittext.getText().toString();
    final ImageButton sendButton = (ImageButton) view
            .findViewById(R.id.button10);
    sendButton.setImageResource(R.drawable.like);
    // ----Set autoscroll of listview when a new message arrives----//
    msgListView.setTranscriptMode(ListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
    msgListView.setStackFromBottom(true);

    chatlist = new ArrayList<ChatMessage>();
    chatAdapter = new ChatAdapter(getActivity(), chatlist);
    emojilist = new ArrayList<EmojiMessage>();
    emojiAdapter = new EmojiAdapter(getActivity(), emojilist);

    msg_edittext.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            if (msg_edittext.getText().length()>0) {
                sendButton.setImageResource(R.drawable.sending_black);
                msgListView.setAdapter(chatAdapter);
            } else sendButton.setImageResource(R.drawable.like); msgListView.setAdapter(emojiAdapter);
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if (msg_edittext.getText().length()>0) {
                sendButton.setImageResource(R.drawable.sending_black);
                msgListView.setAdapter(chatAdapter);
            } else sendButton.setImageResource(R.drawable.like); msgListView.setAdapter(emojiAdapter);
        }

        @Override
        public void afterTextChanged(Editable s) {
            if (msg_edittext.getText().length()>0) {
                sendButton.setImageResource(R.drawable.sending_black);
                msgListView.setAdapter(chatAdapter);
            } else sendButton.setImageResource(R.drawable.like); msgListView.setAdapter(emojiAdapter);
        }
    });
    sendButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            sendTextMessage(v);
        }
    });




    return view;
}

@Override
public void onSaveInstanceState(Bundle outState) {
}

public void sendTextMessage(View v) {
    String message = msg_edittext.getEditableText().toString();
    if (!message.equalsIgnoreCase("")) {
        final ChatMessage chatMessage = new ChatMessage(user1, user2,
                message, "" + random.nextInt(1000), true);
        chatMessage.setMsgID();
        chatMessage.body = message;
        chatMessage.Date = CommonMethods.getCurrentDate();
        chatMessage.Time = CommonMethods.getCurrentTime();
        msg_edittext.setText("");
        chatAdapter.add(chatMessage);
        chatAdapter.notifyDataSetChanged();
    }
}
public void sendEmojiMessage(View v) {
    String message = msg_edittext.getEditableText().toString();
    android.support.v7.widget.AppCompatImageView emoji = (android.support.v7.widget.AppCompatImageView) getView().findViewById(R.id.message_image);
    emoji.setImageResource(R.drawable.like);
    if (message.equalsIgnoreCase("")) {
        final EmojiMessage emojiMessage = new EmojiMessage(user1, user2, emoji, "" + random.nextInt(1000), true);
        emojiMessage.body = emoji;
        emojiMessage.Date = CommonMethods.getCurrentDate();
        emojiMessage.Time = CommonMethods.getCurrentTime();
        msg_edittext.setText("");
        emojiAdapter.add(emojiMessage);
        emojiAdapter.notifyDataSetChanged();
    }
}
}

chatbubble.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/imagebubble_layout_parent"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
    android:id="@+id/imagebubble_layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="24dp"
    android:layout_marginLeft="56dp"
    android:background="@android:color/transparent">

    <android.support.v7.widget.AppCompatImageButton
        android:id="@+id/message_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="@android:color/transparent"
        app:srcCompat="@drawable/like"/>

</LinearLayout>

imagechatbubble.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/imagebubble_layout_parent"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
    android:id="@+id/imagebubble_layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="24dp"
    android:layout_marginLeft="56dp"
    android:background="@android:color/transparent">

    <android.support.v7.widget.AppCompatImageButton
        android:id="@+id/message_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="@android:color/transparent"
        app:srcCompat="@drawable/like"/>

</LinearLayout>

1 个答案:

答案 0 :(得分:0)

首先,我们需要测试表情符号是否容易发送到listView,它上面有文字。 您可以在活动中添加从适配器调用的接口。 界面将调用TextChangedListener,最终显示发送按钮或表情符号。