0
我正在使用ListView來顯示聊天消息。消息不會出現在列表視圖中。我正在使用'setListAdapter(new ArrayAdapter(this,android.R.layout.simple_list_item_1,messageList));`ListView行不顯示
請幫助我,我無法識別問題。
在此先感謝。
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" />
是否chat_layout包含ID @android一個ListView:ID /列表? – Blackbelt
@blackbelt我已經更新了這個問題。 –
@blackbelt文字不可見,要麼是白色的。如何設置黑色 –