Question

Anyone can help me with this problem? I'm just a beginner in android programming and I found this IM app and do some modifications with it. My problem is that the messages was set to display in edittext and not in listview. I try to apply listview but it always have error and I don't know anymore how to do it.

Here is my code for messaging

package at.vcity.uriantext;

import java.io.UnsupportedEncodingException;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import at.vcity.uriantext.R;
import at.vcity.uriantext.interfaces.IAppManager;
import at.vcity.uriantext.services.IMService;
import at.vcity.uriantext.tools.FriendController;
import at.vcity.uriantext.tools.LocalStorageHandler;
import at.vcity.uriantext.types.FriendInfo;
import at.vcity.uriantext.types.MessageInfo;


public class Messaging extends Activity {


private static final int EXIT_APP_ID = Menu.FIRST + 1;
private static final int MESSAGE_CANNOT_BE_SENT = 0;
public String username;
private EditText messageText;
private ListView messageHistoryText;
private Button sendMessageButton;
private IAppManager imService;
private FriendInfo friend = new FriendInfo();
private LocalStorageHandler localstoragehandler; 
private Cursor dbCursor;
private Button exitButton;

private ServiceConnection mConnection = new ServiceConnection() {


    public void onServiceConnected(ComponentName className, IBinder service) {          
        imService = ((IMService.IMBinder)service).getService();
    }
    public void onServiceDisconnected(ComponentName className) {
        imService = null;
        Toast.makeText(Messaging.this, R.string.local_service_stopped,
                Toast.LENGTH_SHORT).show();
    }
};

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);    


    setContentView(R.layout.messaging_screen); //messaging_screen);

    messageHistoryText = (ListView) findViewById(R.id.messageHistory);


    messageText = (EditText) findViewById(R.id.message);

    messageText.requestFocus();         

    sendMessageButton = (Button) findViewById(R.id.sendMessageButton);

    exitButton =(Button) findViewById(R.id.exitButton);

    Bundle extras = this.getIntent().getExtras();


    friend.userName = extras.getString(FriendInfo.USERNAME);
    friend.ip = extras.getString(FriendInfo.IP);
    friend.port = extras.getString(FriendInfo.PORT);
    String msg = extras.getString(MessageInfo.MESSAGETEXT);



    setTitle("Messaging with " + friend.userName);


//  EditText friendUserName = (EditText) findViewById(R.id.friendUserName);
//  friendUserName.setText(friend.userName);


    localstoragehandler = new LocalStorageHandler(this);
    dbCursor = localstoragehandler.get(friend.userName, IMService.USERNAME );

    exitButton.setOnClickListener(new OnClickListener(){

        public void onClick(View arg0) 
        {                   

            Intent i = new Intent(Messaging.this, FriendList.class);
            startActivity(i);

        }

    });

    if (dbCursor.getCount() > 0){
    int noOfScorer = 0;
    dbCursor.moveToFirst();
        while ((!dbCursor.isAfterLast())&&noOfScorer<dbCursor.getCount()) 
        {
            noOfScorer++;

            this.appendToMessageHistory(dbCursor.getString(2) , dbCursor.getString(3));
            dbCursor.moveToNext();
        }
    }
    localstoragehandler.close();

    if (msg != null) 
    {
        this.appendToMessageHistory(friend.userName , msg);
        ((NotificationManager)getSystemService(NOTIFICATION_SERVICE)).cancel((friend.userName+msg).hashCode());
    }

    sendMessageButton.setOnClickListener(new OnClickListener(){
        CharSequence message;
        Handler handler = new Handler();
        public void onClick(View arg0) {
            message = messageText.getText();
            if (message.length()>0) 
            {       
                appendToMessageHistory(imService.getUsername(), message.toString());

                localstoragehandler.insert(imService.getUsername(), friend.userName, message.toString());

                messageText.setText("");
                Thread thread = new Thread(){                   
                    public void run() {
                        try {
                            if (imService.sendMessage(imService.getUsername(), friend.userName, message.toString()) == null)
                            {

                                handler.post(new Runnable(){    

                                    public void run() {

                                        Toast.makeText(getApplicationContext(),R.string.message_cannot_be_sent, Toast.LENGTH_LONG).show();


                                        //showDialog(MESSAGE_CANNOT_BE_SENT);                                       
                                    }

                                });

                            }
                        } catch (UnsupportedEncodingException e) {
                            Toast.makeText(getApplicationContext(),R.string.message_cannot_be_sent, Toast.LENGTH_LONG).show();

                            e.printStackTrace();
                        }
                    }                       
                };
                thread.start();

            }


        }});




    messageText.setOnKeyListener(new OnKeyListener(){
        public boolean onKey(View v, int keyCode, KeyEvent event) 
        {
            if (keyCode == 66){
                sendMessageButton.performClick();
                return true;
            }
            return false;
        }


    });


}

@Override
protected Dialog onCreateDialog(int id) {
    int message = -1;
    switch (id)
    {
    case MESSAGE_CANNOT_BE_SENT:
        message = R.string.message_cannot_be_sent;
    break;
    }

    if (message == -1)
    {
        return null;
    }
    else
    {
        return new AlertDialog.Builder(Messaging.this)       
        .setMessage(message)
        .setPositiveButton(R.string.OK, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                /* User clicked OK so do some stuff */
            }
        })        
        .create();
    }
}

@Override
protected void onPause() {
    super.onPause();
    unregisterReceiver(messageReceiver);
    unbindService(mConnection);

    FriendController.setActiveFriend(null);

}

@Override
protected void onResume() 
{       
    super.onResume();
    bindService(new Intent(Messaging.this, IMService.class), mConnection , Context.BIND_AUTO_CREATE);

    IntentFilter i = new IntentFilter();
    i.addAction(IMService.TAKE_MESSAGE);

    registerReceiver(messageReceiver, i);

    FriendController.setActiveFriend(friend.userName);      


}


public class  MessageReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) 
    {       
        Bundle extra = intent.getExtras();
        String username = extra.getString(MessageInfo.USERID);          
        String message = extra.getString(MessageInfo.MESSAGETEXT);

        if (username != null && message != null)
        {
            if (friend.userName.equals(username)) {
                appendToMessageHistory(username, message);
                localstoragehandler.insert(username,imService.getUsername(), message);

            }
            else {
                if (message.length() > 15) {
                    message = message.substring(0, 15);
                }
                Toast.makeText(Messaging.this,  username + " says '"+
                                                message + "'",
                                                Toast.LENGTH_SHORT).show();     
            }
        }           
    }

};

private MessageReceiver messageReceiver = new MessageReceiver();

public  void appendToMessageHistory(String username, String message) {
    if (username != null && message != null) {

        messageHistoryText.append(message + ":\n");
        messageHistoryText.append(username + ":\n");    
        messageHistoryText.setBackgroundResource(R.drawable.bubble_yellow);


    }
}


@Override
protected void onDestroy() {
    super.onDestroy();
    if (localstoragehandler != null) {
        localstoragehandler.close();
    }
    if (dbCursor != null) {
        dbCursor.close();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {     
    boolean result = super.onCreateOptionsMenu(menu);       


    //menu.add(0, EXIT_APP_ID, 0, R.string.exit_application);       

    return result;
}
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) 
{       

    switch(item.getItemId()) 
    {     

        case EXIT_APP_ID:
        {

            Intent i = new Intent(Messaging.this, FriendList.class);
            startActivity(i);

        }           
    }

    return super.onMenuItemSelected(featureId, item);       
}   

}

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top