2
中使用自定義列表視圖設置左右對話正在開發一個聊天應用程序。我使用smack庫完成了它們。一切正常,即使用戶也可以相互聊天,但無法設置傳入消息在ListView
的右側。我已經嘗試了TextView
的權重。但我無法實現它。我提到了一些這些問題,但我沒有幫助我!無法在android
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 = "[email protected]";
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_;
}
}
}
UPDATE
只有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" />
我得到到目前爲止,這是 什麼我希望我的留言(走出去)在t的右邊他
ListView
並傳出到屏幕左側。 請不要建議。
時添加傳入和傳出消息,其已經對準的左側和右側的不同佈局看看chat_b.xml和chat_ab.xml –