채팅 응용 프로그램을 작성 중이며 사용자가 메시지를 입력하면 listview 행에 표시되지만 대화 풍선에 표시하고 싶습니다. 내 전체 코드는안드로이드에서 ListView를 사용자 정의하는 방법은 무엇입니까?
입니다.나는 이런 식으로 뭔가를 만들고 싶어,
하지만 지금 난 단지 보내기 버튼을 누릅니다과 그들에 쓰여진 텍스트와 간단한 목록 요소를 게시 할 수 있습니다.
원하는 결과를 얻으려면 어떻게해야합니까? 나는 많은 연구를했지만 게시물 중 어느 것도 만족스럽지 않습니다. 여기
지금 여기 내chat.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#ffffff"
>
<Button
android:id="@+id/buttonchat"
android:layout_width="50dip"
android:layout_height="50dip"
android:background="@drawable/home"
android:paddingBottom="2dip"
android:layout_alignParentLeft="true"/>
<TextView
android:layout_width="fill_parent"
android:fillViewport="true"
android:text="Talk"
android:fontFamily="Sans-Seriff"
android:textSize="32sp"
android:gravity="center"
android:textColor="#1d1d1d"
android:layout_height="fill_parent"
android:layout_toRightOf="@+id/buttonchat"
android:layout_toLeftOf="@+id/buttonchat4"
android:layout_above="@+id/txtMessagesReceived"
android:id="@+id/txtMessagesReceived2"
/>
<Button
android:id="@+id/buttonchat4"
android:layout_width="60dip"
android:layout_height="50dip"
android:layout_alignParentRight="true"
android:background="@drawable/but2"
android:minHeight="45dp"
android:minWidth="60dp" />
<ListView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/txtMessagesReceived"
android:layout_width="fill_parent"
android:fillViewport="true"
android:layout_height="fill_parent"
android:background="#c8c8c8"
android:layout_below="@+id/buttonchat"
android:layout_above="@+id/buttonchat2"
/>
<Button
android:id="@+id/buttonchat2"
android:layout_width="50dip"
android:layout_height="50dip"
android:layout_alignParentBottom="true"
android:background="@drawable/camera"
android:layout_alignParentLeft="true"/>
<EditText
android:id="@+id/txtMessage"
android:layout_width="fill_parent"
android:layout_height="50dip"
android:fillViewport="true"
android:layout_toRightOf="@+id/buttonchat2"
android:layout_toLeftOf="@+id/buttonchat3"
android:layout_below="@+id/txtMessagesReceived"
android:layout_alignParentBottom="true"
/>
<Button
android:layout_width="60dip"
android:layout_height="50dip"
android:id="@+id/buttonchat3"
android:layout_alignParentBottom="true"
android:background="@drawable/send"
android:layout_alignParentRight="true"
/>
</RelativeLayout>
입니다
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<String> itemArrey;
private ArrayAdapter<String> 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<String, Void, Bitmap> {
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<String>();
itemArrey.clear();
itemAdapter = new ArrayAdapter<String>(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();
}
}
도움이 어떤 종류의 appre 인
chatactivity.java에게 있습니다 유능한!
나는 단지 길을 가르쳐 줘, 나에게 올바른 길을 보여줘, 나는 모든 방법으로 혼자 여행 할 것이다.
을 알아야 할 모든 것을 undertsand하는 The World of ListView을보고 추천, 그 –
안녕 마르코, 당신의 의견을 주셔서 감사합니다, 나는 시도 학습 구글 어댑터하지만 여전히 내 문제를 해결하지 않습니다 내 코드를 참조하십시오 setUpView() 함수는 목록에 행을 삽입합니다. 이제이 행을 사용자 정의하려는 대화 거품으로. 도와주세요 ! , 내가 지금까지 이해 한 것은 내가 어댑터를 확장하고 chat_row.xml 파일에 따라 커스터마이징을해야만한다는 것이다.하지만 여전히 많은 돌들이 돌지 않고있다. 좀 더 많은 안내가 아마 나를 통해 이루어질 것이다. 올바른 방향. –