gpt4 book ai didi

android - 在同一气泡中显示来自用户的连续聊天

转载 作者:太空狗 更新时间:2023-10-29 14:13:11 25 4
gpt4 key购买 nike

我正在开发聊天应用程序..这是我到目前为止所做的。 chat bw two

现在我想要的是: grouping continuous chat from a user

拜托,我被卡住了,不知道如何在这个 UI 上工作..IE如果用户 View 再次查看聊天,则如果消息是来自用户的连续消息,则将它们显示在与第二张图片相同的气泡中。这个聊天东西都是本地的,没有使用任何服务或服务器。我正在提供我的代码。

MessageActivity.java

public class MessageActivity extends ListActivity {
/** Called when the activity is first created. */

ArrayList<Message> messages;
AwesomeAdapter adapter;
EditText text;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

text = (EditText) this.findViewById(R.id.text);


this.setTitle("Harish");
messages = new ArrayList<Message>();
adapter = new AwesomeAdapter(this, messages);
setListAdapter(adapter);

}
public void sendMessage(View v)
{
String newMessage = text.getText().toString().trim();
//String msg="\nTesting";
if(newMessage.length() > 0)
{
text.setText("");
//addNewMessage(new Message(newMessage+msg, true));
addNewMessage(new Message(newMessage, true));
}
}
public void sendMessage2(View v)
{
String newMessage = text.getText().toString().trim();
if(newMessage.length() > 0)
{
text.setText("");
addNewMessage(new Message(newMessage, false));

}
}
void addNewMessage(Message m)
{
messages.add(m);
adapter.notifyDataSetChanged();
getListView().setSelection(messages.size()-1);
}

AwesomeAdapter.java

public class AwesomeAdapter extends BaseAdapter{
private Context mContext;
private ArrayList<Message> mMessages;



public AwesomeAdapter(Context context, ArrayList<Message> messages) {
super();
this.mContext = context;
this.mMessages = messages;
}

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

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
Message message = (Message) this.getItem(position);

ViewHolder holder;
if(convertView == null)
{
holder = new ViewHolder();
convertView = LayoutInflater.from(mContext).inflate(R.layout.sms_row, parent, false);
holder.message = (TextView) convertView.findViewById(R.id.message_text);
convertView.setTag(holder);
}
else
holder = (ViewHolder) convertView.getTag();

holder.message.setText(message.getMessage());

LayoutParams lp = (LayoutParams) holder.message.getLayoutParams();


if(message.isMine())
{
holder.message.setBackgroundResource(R.drawable.me);
lp.setMargins(15, 10, 0, 10);


}

else
{
holder.message.setBackgroundResource(R.drawable.you);
lp.setMargins(0, 10, 15, 10);
}
holder.message.setLayoutParams(lp);


return convertView;
}
private static class ViewHolder
{
TextView message;
}


@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}

消息.java

public class Message {

String message;

boolean isMine;

public Message(String message, boolean isMine) {
super();
this.message = message;
this.isMine = isMine;

}
/**
* Constructor to make a status Message object
* consider the parameters are swaped from default Message constructor,
* not a good approach but have to go with it.
*/
public Message(boolean status, String message) {
super();
this.message = message;
this.isMine = false;

}
public String getMessage() {
return message;
}

public boolean isMine() {
return isMine;
}

最佳答案

收到新消息后,将该消息附加到 ArrayList<Message> 中的前一条消息(即从 arraylist 中获取以前的消息并添加附加此新消息和换行符(\n)它应该工作)并调用 adapter.notifyDataSetChanged();

关于android - 在同一气泡中显示来自用户的连续聊天,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25072800/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com