I am using the ListView to show the chat messages. Messages are not appearing on the listview. I am doing this using 'setListAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, messageList));`
Please help me i could not recognize the problem.
Thanks in advance.
import java.util.ArrayList;
import android.app.ListActivity;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class ChatActivity extends ListActivity {
private String userId;
private String roomName = "Stackoverflow";
private Context context;
private ArrayList<String> messageList = new ArrayList<String>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.chat_layout);
// Setting Custom Title of Activity
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title_login);
TextView textView = (TextView) findViewById(R.id.loginText);
textView.setText(roomName);
context = this;
//Get the User Credentials
String userCredentials[] = new Util().getUserCredentials(context);
userId = userCredentials[0];
//Fetch the messages
fetchMessages();
//Display the messages
displayResultList();
}
/**
* It fetch the chat messages.
*/
public void fetchMessages() {
SQLiteDatabase database = null;
try {
SQLLiteHelper sqlLiteHelper = new SQLLiteHelper(context);
database = sqlLiteHelper.getWritableDatabase();
Cursor cursor = database.rawQuery("select from_id, message from groups where roomname = " + roomName, null);
if(cursor != null) {
if(cursor.moveToFirst()) {
String chatMessage = null;
String from = null;
do {
chatMessage = cursor.getString(cursor.getColumnIndex("message"));
from = cursor.getString(cursor.getColumnIndex("from_id"));
messageList.add(from + ":" + chatMessage);
} while(cursor.moveToNext());
}
}
} catch(Exception e) {
Log.v("HB", "Exceptino at::" + e.getMessage());
} finally {
if(database != null) {
database.close();
}
}
}
/**
* It display the chat messages.
*/
private void displayResultList() {
for(int index = 0; index < 10; index++) {
messageList.add("SO" + ":" + "hello");
}
if(messageList != null && messageList.size() > 0) {
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, messageList));
getListView().setTextFilterEnabled(true);
}
}
}
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/bottomLinearLayout" />