I am trying to display received messages using custom adapter on list view , until now everything is working fine , but when I press back and come back and launch this activity again , only message send works but received messages cannot be displayed on the list view.
Steps I have done so far :
Use debugger to find out if message is not empty, messages are received and not empty.
Use debugger inside receives() function , it turns out on first launch of activity looper.loop() function gives control back to main thread but next time on activity resume this loops doesn't seem to end (may be this is cause of error) .
p.s: On first launch of this activity I can display received messages , but when I go back and resume activity , receives() fails , I am clueless here , any small hint in right direction is welcome, thanks for time.
public class hotListener extends ListActivity {
private XMPPConnection connection; private IBinder binder; private Handler mHandler = new Handler(); private ArrayList<String> messages = new ArrayList<String>(); ArrayList<ChatMessage> messagex= new ArrayList<ChatMessage>();; ChattingAdapter adaptex; Intent mIntent ; private ListView listview; EditText sender_message ; String msg; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.listener); //messagex.add(new ChatMessage("Hello", false)); adaptex = new ChattingAdapter(getApplicationContext(),messagex); setListAdapter(adaptex); Button send_button = (Button) findViewById(R.id.chat_send_message); sender_message = (EditText) findViewById(R.id.chat_input); send_button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { msg = sender_message.getText().toString(); sender_message.setText(""); if(!(msg.length()==0)){ messagex.add(new ChatMessage(msg, true)); //addNewMessage(new ChatMessage(msg, true)); adaptex.notifyDataSetChanged(); getListView().setSelection(messagex.size()-1); } } }); if(!isMyServiceRunning()){ System.out.println("seems like service not running"); startService(new Intent(this,xService.class)); System.out.print(" now started "); } } @Override protected void onStart(){ super.onStart(); Boolean kuch = bindService(new Intent(this,xService.class), mConnection,Context.BIND_AUTO_CREATE); //System.out.println(kuch); //System.out.println("bind done"); } private void receives(XMPPConnection connection2) { //ChatManager chatmanager = connection.getChatManager(); connection2.getChatManager().addChatListener(new ChatManagerListener() { @Override public void chatCreated(Chat arg0, boolean arg1) { arg0.addMessageListener(new MessageListener() { @Override public void processMessage(Chat chat, Message message) { final String from = message.getFrom(); final String body = message.getBody(); mHandler.post(new Runnable() { ChatMessage kudi = new ChatMessage(body, false); @Override public void run() { //setListAdapter(1); messagex.add(kudi); adaptex.notifyDataSetChanged(); getListView().setSelection(messagex.size()-1); Toast.makeText(hotListener.this,body,Toast.LENGTH_SHORT).show(); } }); //System.out.println(String.format("Received message "+body +" from "+ from)); } }); } }); } private boolean isMyServiceRunning() { ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); for(RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)){ if(xService.class.getName().equals(service.service.getClassName())){ return true; } } //System.out.print("false"); return false; } @Override protected void onResume() { bindService(new Intent(this, xService.class), mConnection, Context.BIND_AUTO_CREATE); setListAdapter(adaptex); adaptex.notifyDataSetChanged(); super.onResume(); } @Override protected void onPause() { unbindService(mConnection); super.onPause(); } private ServiceConnection mConnection = new ServiceConnection() { @Override public void onServiceDisconnected(ComponentName name) { connection = null; service = null; } @Override public void onServiceConnected(ComponentName name, IBinder binder) { //System.out.println("binding in hot listener"); service = ((xService.MyBinder)binder).getService(); connection = service.getConnection(); receives(connection); Log.wtf("Service","connected"); } }; void addNewMessage(ChatMessage m) { System.out.println("1"); messagex.add(m); System.out.println("2"); adaptex.notifyDataSetChanged(); System.out.println("3"); getListView().setSelection(messagex.size()-1); }
}
My adapter :
import java.util.ArrayList;
import android.content.Context;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;
public class ChattingAdapter extends BaseAdapter{
private Context mContext;
private ArrayList<ChatMessage> mMessages;
public ChattingAdapter(Context context, ArrayList<ChatMessage> messages) {
super();
this.mContext = context;
this.mMessages = messages;
}
@Override
public int getCount() {
return mMessages.size();
}
@Override
public Object getItem(int position) {
return mMessages.get(position);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ChatMessage message = (ChatMessage) this.getItem(position);
ViewHolder holder;
if(convertView == null)
{
holder = new ViewHolder();
convertView = LayoutInflater.from(mContext).inflate(R.layout.listitem, parent, false);
holder.message = (TextView) convertView.findViewById(R.id.text1);
convertView.setTag(holder);
}
else
holder = (ViewHolder) convertView.getTag();
holder.message.setText(message.getMessage());
LayoutParams lp = (LayoutParams) holder.message.getLayoutParams();
//Check whether message is mine to show green background and align to right
if(message.isMine())
{ holder.message.setBackgroundResource(R.drawable.msgbox_new_selected_go_up);
lp.gravity = Gravity.RIGHT;
}
//If not mine then it is from sender to show orange background and align to left
else
{
holder.message.setBackgroundResource(R.drawable.msgbox_other_go_up);
lp.gravity = Gravity.LEFT;
}
holder.message.setLayoutParams(lp);
//holder.message.setTextColor(R.color.textColor);
return convertView;
}
private static class ViewHolder
{
TextView message;
}
@Override
public long getItemId(int position) {
//Unimplemented, because we aren't using Sqlite.
return position;
}
}
来源:https://stackoverflow.com/questions/21961171/android-custom-adapter-fails-to-reload-list-view-on-activity-resume