【问题标题】:ListView rows are not showingListView 行未显示
【发布时间】:2013-07-01 07:54:30
【问题描述】:

我正在使用 ListView 来显示聊天消息。消息未出现在列表视图上。我正在使用'setListAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, messageList));`

请帮助我,我无法识别问题。

提前致谢。

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:id/list 的 ListView ?
  • @blackbelt 我已经更新了问题。
  • @blackbelt 文本不可见,或者它们是白色的。如何设置黑色
  • 你必须从 arrayadapter 覆盖 getView 方法
  • 您能否在@android:id/list 中提供任何这样做的链接

标签: android


【解决方案1】:

这是一个如何覆盖ArrayAdater.getView 的示例。

  ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, messageList) {
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = LayoutInflater.from(YoutActivity.this).inflate(android.R.layout.simple_list_item_1, null);
            }

            ((TextView)convertView).setText(getItem(position))
            ((TextView)convertView).setTextColor(colors)

            return convertView;

        };
    };

 setListAdapter(arrayAdapter);

检查错别字

【讨论】:

    猜你喜欢
    • 2020-12-17
    • 1970-01-01
    • 2021-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多