サーバーでデータベースMySQLを使用してチャットを実装しています。今、スレッドでカスタム Listview を更新したいのですが、方法がわかりません??
私のコードは次のとおりです。
chat_con_activity.java
import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.StrictMode;
import android.os.SystemClock;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;
import com.example.classes.IpUpdater;
import com.example.classes.Messages_con_adapter;
import com.example.classes.Send_Message;
import com.example.classes.View_Messages;
import com.example.tabs.user_online;
import com.example.wifivoip.R;
public class chat_con_activity extends Activity implements Runnable{
private ListView list = null;
ArrayAdapter<String> la;
Button send_btn;
EditText chat_msg;
String user;
String cTo;
String[] names;
String[] namesfrm;
String[] msgs;
String[] datez;
int stop = 0;
Context cn = this;
Thread x;
int count;
Messages_con_adapter mca;
@Override
public void onCreate(Bundle savedInstanceState) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
super.onCreate(savedInstanceState);
setContentView(R.layout.send_chat);
send_btn = (Button) findViewById(R.id.chat_btn_send);
chat_msg = (EditText) findViewById(R.id.chat_text_send);
user = getIntent().getStringExtra("cFrom");
cTo = getIntent().getStringExtra("cTo");
list = (ListView) findViewById(R.id.chat_list_view);
Toast.makeText(getApplicationContext(), ""+user+" "+cTo, Toast.LENGTH_SHORT).show();
display();
x = new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
while(true){
try{
//Thread.sleep(1000);
View_Messages vm = new View_Messages(user, cTo);
int cnt = vm.getIfNew();
System.err.println(""+cnt+" "+count);
if(cnt != count){
System.err.println(""+cnt+" "+count);
names = vm.getAllNamesTo();
namesfrm = vm.getAllNamesFrom();
msgs = vm.getAllMsgs();
datez = vm.getAllDates();
count = vm.getIfNew();
list.setAdapter(la);
list.setSelection(list.getAdapter().getCount() - 1);
}
if(stop == 1){
break;
}
}catch (Exception e) {
// TODO: handle exception
}
}
}
});x.start();
send_btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String msg = chat_msg.getText().toString().trim();
if(!msg.equals("")){
new Send_Message().sendMessage(user, cTo, msg);
display();
chat_msg.setText("");
}else{
Toast.makeText(getApplicationContext(), "No message to send!!", Toast.LENGTH_SHORT).show();
}
}
});
}
@Override
public void onDestroy(){
super.onDestroy();
stop = 1;
}
public void display(){
View_Messages vm = new View_Messages(user, cTo);
names = vm.getAllNamesTo();
namesfrm = vm.getAllNamesFrom();
msgs = vm.getAllMsgs();
datez = vm.getAllDates();
count = vm.getIfNew();
list.setDivider(null);
la = new Messages_con_adapter(this, user, names, namesfrm,msgs,datez,cTo);
list.setAdapter(la);
list.setSelection(list.getAdapter().getCount() - 1);
}
私のアダプターは: Message_con_adapter.java
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class Messages_con_adapter extends ArrayAdapter<String>{
String user;
String[] name;
String[] namefrm;
String[] msgs;
String[] dates;
Context context;
View rowView;
public Messages_con_adapter(Context context,String user, String[] name,String[] namefrm, String[] msgs,String[] dates,String To) {
super(context, R.layout.chat_con,name);
this.user = user;
this.context = context;
this.msgs = msgs;
this.name = name;
this.namefrm = namefrm;
this.dates = dates;
this.To = To;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(namefrm[position].equals(user)){
rowView = inflater.inflate(R.layout.chat_con_home, parent, false);
TextView dateTimeHome = (TextView) rowView.findViewById(R.id.time_c_home);
TextView c_home = (TextView) rowView.findViewById(R.id.c_u_home);
TextView c_home_m = (TextView) rowView.findViewById(R.id.c_u_home_m);
dateTimeHome.setText(dates[position]);
c_home.setText(user);
c_home_m.setText(msgs[position]);
}else
{
rowView = inflater.inflate(R.layout.chat_con, parent, false);
TextView dateTimeAway = (TextView) rowView.findViewById(R.id.time_c_away);
TextView c_away = (TextView) rowView.findViewById(R.id.c_u_away);
TextView c_away_m = (TextView) rowView.findViewById(R.id.c_u_away_m);
dateTimeAway.setText(dates[position]);
c_away.setText(namefrm[position]);
c_away_m.setText(msgs[position]);
}
return rowView;
}
アイデアはありますか?助けてください。=(
前もって感謝します。