开发聊天应用程序。我使用smack库完成了它们。一切正常,即使用户也可以互相聊天,但无法将传入的消息设置到ListView
的右侧。我已经尝试了TextView
对权利的严重性。但我无法实现它。我提到了一些这些问题,但我没有帮助过我!
public class ChatRoom extends Activity {
private final static String SERVER_HOST = "talk.google.com";
private final static int SERVER_PORT = 5222;
private final static String SERVICE_NAME = "gmail.com";
private final static String LOGIN = "acd@gmail.com";
private final static String PASSWORD = "password";
private List<String> m_discussionThread;
private XMPPConnection m_connection;
private Handler m_handler;
private String mtemp_user_id;
private ProgressDialog p_dialog;
EditText mMsg;
ListView listview;
MyAdapter myadpter;
String fromName;
String mtempSpecial;
String SendMSg, RecievdMsg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat_screen);
PrepareChat prechat = new PrepareChat(); //AsyncTask
m_handler = new Handler();
p_dialog = new ProgressDialog(ChatRoom.this);
m_discussionThread = new ArrayList<String>();
myadpter = new MyAdapter(m_discussionThread);
Bundle bundle = getIntent().getExtras();
mtemp_user_id = bundle.getString("USER");
prechat.execute();
listview = (ListView) findViewById(R.id.list_view_);
listview.setAdapter(myadpter);
mMsg = (EditText) findViewById(R.id.ed_msg);
findViewById(R.id.b_MSG_To).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
SendMSg = "SEND";
Message msg = new Message(mtemp_user_id, Message.Type.chat);
msg.setBody(mMsg.getText().toString());
m_connection.sendPacket(msg);
m_discussionThread.add(mMsg.getText().toString());
mMsg.setText("");
}
});
}
public class PrepareChat extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
p_dialog.setMessage("Preparing chat");
p_dialog.setCancelable(false);
p_dialog.show();
}
@Override
protected Void doInBackground(Void... params) {
System.out.println("In do in back ...");
try {
initConnection();
} catch (XMPPException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
p_dialog.dismiss();
}
}
private void initConnection() throws XMPPException {
ConnectionConfiguration config = new ConnectionConfiguration(
SERVER_HOST, SERVER_PORT, SERVICE_NAME);
m_connection = new XMPPConnection(config);
m_connection.connect();
m_connection.login(LOGIN, PASSWORD);
System.out.println("After LOGInd");
Presence presence = new Presence(Presence.Type.available);
presence.setStatus("available");
m_connection.sendPacket(presence);
PacketFilter filter = new MessageTypeFilter(Message.Type.chat);
m_connection.addPacketListener(new PacketListener() {
public void processPacket(Packet packet) {
Message message = (Message) packet;
if (message.getBody() != null) {
String fromName = StringUtils.parseBareAddress(message
.getFrom());
String mtempSpecial = message.getBody().toString();
m_discussionThread.add(mtemp_user_id + mtempSpecial);
RecievdMsg = "RE";
m_handler.post(new Runnable() {
public void run() {
myadpter.notifyDataSetChanged();
listview.setSelection(myadpter.getCount() - 1);
}
});
}
}
}, filter);
}
public class MyAdapter extends BaseAdapter {
LayoutInflater layoutinflate;
List<String> list = new ArrayList<String>();
Context context;
public MyAdapter(List<String> m_discussionThread) {
list = m_discussionThread;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int items) {
return items;
}
@Override
public long getItemId(int posi) {
return posi;
}
@Override
public View getView(int position, View arg1, ViewGroup view_Group) {
View view_ = arg1;
LayoutInflater layout_inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (SendMSg.equals("SEND")) {
view_ = layout_inflater.inflate(R.layout.chat_b, view_Group,
false);
TextView tv_MyMsg = (TextView) view_
.findViewById(R.id.tv_mymsg);
System.out.println("in send loop");
// tv_MyMsg.setGravity(Gravity.RIGHT);
tv_MyMsg.setText(list.get(position)); //TextView not binding to the right.
SendMSg = "";
} else {
view_ = layout_inflater.inflate(R.layout.chat_ab, view_Group,
false);
TextView tv_FromMsg = (TextView) view_
.findViewById(R.id.tv_From_Msg);
tv_FromMsg.setText(list.get(position));
/*
* tv_ToMsg.setGravity(Gravity.LEFT);
* System.out.println("in else"); tv_FromMsg.setText("");
* tv_ToMsg.setText(list.get(position));
*/}
return view_;
}
}
}
更新
仅填充chat_ab.xml,但忽略包含绿色文本的chat_b.xml。
chat_ab.xml
...
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:background="#ffffff"
android:layout_gravity="left"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/tv_From_Msg"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:gravity="left"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#FF00FF" />
chat_b.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:background="#ffffff"
android:layout_gravity="right"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/tv_mymsg"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#0000ff"
android:layout_alignParentRight="true"
android:gravity="right"
android:layout_alignParentTop="true"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
到目前为止我得到的是
我期待的是我的消息(走出去)ListView
的右侧并且向屏幕左侧传出。
请建议。
答案 0 :(得分:0)
您可以使用LinearLayout中的android:gravity
和android:layout_gravity
属性来实现此目的。
答案 1 :(得分:0)
对列表视图内的textview使用不同的布局。每次列表视图填充新条目(消息)时,请使用user.getEmail()将当前用户的Auth与发送者的Auth进行比较。 如果它们匹配,则将上述textview的布局重力设置为右,否则为左。