Android 列表视图的 Bubble Chat 问题
嗨,我遇到了一个奇怪的麻烦,我没有收到气泡,例如您在右侧留言,我在右侧留言离开...当我调用notifydatachanges时,它会擦除所有气泡...请指导我。下面是代码...
private final Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
if(D) Log.d(TAG, "In the Handler");
switch (msg.what) {
case PACKET_CAME:
String incomingMessage = (String) msg.obj;
receivedMessages.add("You: " + incomingMessage);
mg = new Message();
mg.what = 1;
updateListHandler.sendMessage(mg);
// mAdapter.notifyDataSetChanged();
// mAdapter.notifyDataSetInvalidated();
break;
case TOAST:
String toastToMake= (String) msg.obj;
Toast.makeText(getApplicationContext(), toastToMake, Toast.LENGTH_SHORT).show();
break;
}
}
};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
send = (Button)findViewById(R.id.send);
send.setOnClickListener(send_listener);
//msgList.setTextFilterEnabled(true);
msg = (EditText)findViewById(R.id.msg);
msg.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// If the event is a key-up event on the "enter" button
if ((event.getAction() == KeyEvent.ACTION_UP) &&
(keyCode == KeyEvent.KEYCODE_ENTER)) {
postMessage();
return true;
}
return false;
}
});
// Start my server thread
myThread = new ServerThread(getApplicationContext(),mHandler);
//Check if it's running
if (!myThread.socketIsOK()){
Log.e(TAG,"Server NOT STARTED");
Toast.makeText(getApplicationContext(), "Cannot Start Server: ", Toast.LENGTH_LONG).show();
return;
}
// All appears to be OK, start the main loop
myThread.start();
Log.i(TAG,"Server Started");
msgList = (ListView)findViewById(R.id.msgList);
mAdapter = new CustomAdapter();
msgList.setAdapter(mAdapter);
}// end OnCreate()
public class CustomAdapter extends BaseAdapter {
public CustomAdapter(){
// TODO Auto-generated constructor stub
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View row = null;
Log.i("sentmsg", Integer.toString(sentmsg));
Log.i("revmsg", Integer.toString(recvmsg));
if(sentmsg == 1){
row = inflater.inflate(R.layout.message, parent, false);
TextView tv = (TextView)row.findViewById(R.id.textmsg);
tv.setText(receivedMessages.get(position));
sentmsg = 0;
}else{
row = inflater.inflate(R.layout.messagert, parent, false);
TextView tv = (TextView)row.findViewById(R.id.textmsg);
tv.setText(receivedMessages.get(position));
}
return row;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return receivedMessages.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
}
// Sends the message in the msg EditText
private void postMessage(){
String theNewMessage = msg.getText().toString();
try{
myThread.sendMessage(theNewMessage);
}catch(Exception e){
Log.e(TAG,"Cannot send message"+e.getMessage());
}
sentmsg = 1;
receivedMessages.add("Me: " + theNewMessage);
Message msg = new Message();
msg.what = 1;
updateListHandler.sendMessage(msg);
// msgList.invalidateViews();
// mAdapter.notifyDataSetChanged();
// mAdapter.notifyDataSetInvalidated();
// msgList.invalidateViews();
}
private Handler updateListHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 1:
mAdapter.notifyDataSetChanged();
break;
}
;
};
};
// On click listener for the button
private OnClickListener send_listener = new OnClickListener() {
public void onClick(View v) {
postMessage();
}
};
@Override
public void onDestroy(){
super.onDestroy();
myThread.closeSocket();
}
}// Activity class
这是 xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/textmsg"
android:layout_marginTop="2px"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="16sp"
android:background="@drawable/greybox">
</TextView>
</LinearLayout>
这是另一个 xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/textmsg"
android:layout_marginTop="2px"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="16sp">
</TextView>
</LinearLayout>
灰盒是 9 补丁图像。 sentmsg 的作用就像一个标志,以便我可以根据收到或发送的消息来膨胀所需的行......
Hi i am having a strange trouble, i am not gettings bubble for example you messeges on right and mine on left... when ever i call notifydatachanges it erases all the bubbles...plz guide me. below is the code...
private final Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
if(D) Log.d(TAG, "In the Handler");
switch (msg.what) {
case PACKET_CAME:
String incomingMessage = (String) msg.obj;
receivedMessages.add("You: " + incomingMessage);
mg = new Message();
mg.what = 1;
updateListHandler.sendMessage(mg);
// mAdapter.notifyDataSetChanged();
// mAdapter.notifyDataSetInvalidated();
break;
case TOAST:
String toastToMake= (String) msg.obj;
Toast.makeText(getApplicationContext(), toastToMake, Toast.LENGTH_SHORT).show();
break;
}
}
};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
send = (Button)findViewById(R.id.send);
send.setOnClickListener(send_listener);
//msgList.setTextFilterEnabled(true);
msg = (EditText)findViewById(R.id.msg);
msg.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// If the event is a key-up event on the "enter" button
if ((event.getAction() == KeyEvent.ACTION_UP) &&
(keyCode == KeyEvent.KEYCODE_ENTER)) {
postMessage();
return true;
}
return false;
}
});
// Start my server thread
myThread = new ServerThread(getApplicationContext(),mHandler);
//Check if it's running
if (!myThread.socketIsOK()){
Log.e(TAG,"Server NOT STARTED");
Toast.makeText(getApplicationContext(), "Cannot Start Server: ", Toast.LENGTH_LONG).show();
return;
}
// All appears to be OK, start the main loop
myThread.start();
Log.i(TAG,"Server Started");
msgList = (ListView)findViewById(R.id.msgList);
mAdapter = new CustomAdapter();
msgList.setAdapter(mAdapter);
}// end OnCreate()
public class CustomAdapter extends BaseAdapter {
public CustomAdapter(){
// TODO Auto-generated constructor stub
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View row = null;
Log.i("sentmsg", Integer.toString(sentmsg));
Log.i("revmsg", Integer.toString(recvmsg));
if(sentmsg == 1){
row = inflater.inflate(R.layout.message, parent, false);
TextView tv = (TextView)row.findViewById(R.id.textmsg);
tv.setText(receivedMessages.get(position));
sentmsg = 0;
}else{
row = inflater.inflate(R.layout.messagert, parent, false);
TextView tv = (TextView)row.findViewById(R.id.textmsg);
tv.setText(receivedMessages.get(position));
}
return row;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return receivedMessages.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
}
// Sends the message in the msg EditText
private void postMessage(){
String theNewMessage = msg.getText().toString();
try{
myThread.sendMessage(theNewMessage);
}catch(Exception e){
Log.e(TAG,"Cannot send message"+e.getMessage());
}
sentmsg = 1;
receivedMessages.add("Me: " + theNewMessage);
Message msg = new Message();
msg.what = 1;
updateListHandler.sendMessage(msg);
// msgList.invalidateViews();
// mAdapter.notifyDataSetChanged();
// mAdapter.notifyDataSetInvalidated();
// msgList.invalidateViews();
}
private Handler updateListHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 1:
mAdapter.notifyDataSetChanged();
break;
}
;
};
};
// On click listener for the button
private OnClickListener send_listener = new OnClickListener() {
public void onClick(View v) {
postMessage();
}
};
@Override
public void onDestroy(){
super.onDestroy();
myThread.closeSocket();
}
}// Activity class
This is the xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/textmsg"
android:layout_marginTop="2px"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="16sp"
android:background="@drawable/greybox">
</TextView>
</LinearLayout>
this is another xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/textmsg"
android:layout_marginTop="2px"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="16sp">
</TextView>
</LinearLayout>
greybox is 9 patch image.
The sentmsg acts like a flag so that i can inflate the required row according to message recevied or sent...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您没有正确组织您的聊天数据。有一个包含聊天消息及其发件人 ID 的类。
使用此对象创建聊天消息列表。然后在适配器的 getview 方法中执行此操作
You have not organised your chat data properly. Have a class that holds a chat message and its sender id.
Use this object to create the list of chat messages. Then in adapter's
getview
method do this