I am working on a chat application and stuck at this position where when a user types his message it is displayed in the listview row , but i want to show it in a chat bubble , i am also posting here my complete code,
我正在开发一个聊天应用程序并停留在这个位置,当用户输入他的消息时,它显示在listview行中,但我想在聊天气泡中显示它,我也在这里发布我的完整代码,
i want to make something like this,
我想做这样的事,
but right now i can only post simple List elements with text written on them with send button press.
但是现在我只能发送简单的List元素,文字写在上面,按下发送按钮。
How can i achieve the desired result ? I have done a lot of research but none of the post was satisfactory.
我怎样才能达到预期的效果?我做了很多研究,但没有一篇文章令人满意。
Here is my
这是我的
chat.xml
Now here is the
现在这里是
chatactivity.java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
//import android.view.Menu;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.KeyEvent;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class ChatScreenActivity extends Activity implements OnClickListener {
Button chatButton;
EditText medit;
ListView mView;
private ArrayList itemArrey;
private ArrayAdapter itemAdapter;
private InputStream OpenHttpConnection(String urlString) throws IOException
{
InputStream in = null;
int respOnse= -1;
URL url = new URL(urlString);
URLConnection cOnn= url.openConnection();
if (!(conn instanceof HttpURLConnection))
throw new IOException("Not an HTTP connection");
try{
HttpURLConnection httpCOnn= (HttpURLConnection) conn;
httpConn.setAllowUserInteraction(false);
httpConn.setInstanceFollowRedirects(true);
httpConn.setRequestMethod("GET");
httpConn.connect();
respOnse= httpConn.getResponseCode();
if (respOnse== HttpURLConnection.HTTP_OK) {
in = httpConn.getInputStream();
}
}
catch (Exception ex)
{
Log.d("Networking", ex.getLocalizedMessage());
throw new IOException("Error connecting");
}
return in;
}
private Bitmap DownloadImage(String URL)
{
Bitmap bitmap = null;
InputStream in = null;
try {
in = OpenHttpConnection(URL);
bitmap = BitmapFactory.decodeStream(in);
in.close();
} catch (IOException e1) {
Log.d("NetworkingActivity", e1.getLocalizedMessage());
}
return bitmap;
}
private class DownloadImageTask extends AsyncTask {
protected Bitmap doInBackground(String... urls) {
return DownloadImage(urls[0]);
}
protected void onPostExecute(Bitmap result) {
ImageView img = (ImageView) findViewById(R.id.img);
img.setImageBitmap(result);
}
}
// static TextView txtMessagesReceived;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Remove title bar
//this.requestWindowFeature(Window.FEATURE_NO_TITLE);
//Remove notification bar
//this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_chat_screen);
// LinearLayout lView = new LinearLayout(this);
/*TextView myText = new TextView(this);
myText.setText("Connecting to server...");
lView.addView(myText);
setContentView(lView); */
//LinearLayout lView = new LinearLayout(this);
chatButton=(Button)findViewById(R.id.buttonchat3);
chatButton.setOnClickListener(this);
}
private void setUpView() {
// TODO Auto-generated method stub
medit = (EditText)this.findViewById(R.id.txtMessage);
chatButton = (Button)this.findViewById(R.id.buttonchat3);
mView = (ListView)this.findViewById(R.id.txtMessagesReceived);
itemArrey = new ArrayList();
itemArrey.clear();
itemAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1,itemArrey);
mView.setAdapter(itemAdapter);
chatButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
addItemList();
}
});
medit.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (keyCode == KeyEvent.KEYCODE_ENTER) {
addItemList();
}
return true;
}
});
}
protected void addItemList() {
// TODO Auto-generated method stub
// TODO Auto-generated method stub
if (isInputValid(medit)) {
itemArrey.add(0,medit.getText().toString());
medit.setText("");
itemAdapter.notifyDataSetChanged();
}
}
protected boolean isInputValid(EditText etInput2) {
// TODO Auto-generatd method stub
if (etInput2.getText().toString().trim().length()<1) {
etInput2.setError("Please Enter Message");
return false;
} else {
return true;
}
}
private void chatbutton()
{
medit=(EditText)findViewById(R.id.txtMessage);
String Str=medit.getText().toString();
if(Str.equals("") || Str.equals(null) )
{
Toast.makeText(this,"Please Type Something...",Toast.LENGTH_LONG).show();
}
else
{
mView=(ListView)findViewById(R.id.txtMessagesReceived);
// mView.append("Me: "+Sr+"\n");
//Toast.makeText(this,"Me:"+Str,Toast.LENGTH_LONG).show();
setUpView();
// medit.setText("");
}
// connectServer();
}
public void onClick(View v2)
{
switch (v2.getId())
{
case R.id.buttonchat3:
chatbutton();
break;
}
}
private void connectServer()
{
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet("http://members.webtitude.in/project/Android/android.php");
try {
HttpResponse respOnse= httpclient.execute(httpget);
if(response != null) {
String line = "";
InputStream inputstream = response.getEntity().getContent();
line = convertStreamToString(inputstream);
Toast.makeText(this, line, Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "Unable to complete your request", Toast.LENGTH_LONG).show();
}
} catch (ClientProtocolException e) {
Toast.makeText(this, "Caught ClientProtocolException", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
Toast.makeText(this, "Internet Seems to not work properly", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(this, "Did not get the url !", Toast.LENGTH_SHORT).show();
}
}
private String convertStreamToString(InputStream is) {
String line = "";
StringBuilder total = new StringBuilder();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
try {
while ((line = rd.readLine()) != null) {
total.append(line);
}
} catch (Exception e) {
Toast.makeText(this, "Stream Exception", Toast.LENGTH_SHORT).show();
}
return total.toString();
}
}
Any kind of help is appreciable !
任何形式的帮助都是可观的!
I just need a Guidance , just show me the right path , i will travel the whole way by myself.
我只需要一个指导,只要告诉我正确的道路,我将自己一路旅行。
0
Make your own class extending BaseAdapter
and define the behavior that you want. Also create your own list item layouts. I recommend you watch The World of ListView to undertsand everything you need to know about ListView
使您自己的类扩展BaseAdapter并定义所需的行为。还可以创建自己的列表项布局。我建议您观看ListView的World of underts以及您需要了解的有关ListView的所有信息