android - 在布局中频繁填充 listView

标签 android eclipse

我正在创建一个显示数据库值的布局。但是这个错误出现在 logcat 中:

Your content must have a ListView whose id attribute is 'android.R.id.list'

这是我的 ListActivity:

import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.SimpleCursorAdapter;
import android.widget.SimpleCursorAdapter.ViewBinder;
import android.widget.TextView;


public class Row_User extends ListActivity
    {


        private static String   TAG     = "Row_User";
        static final String[]   FROM    = { Data.C_USER, Data.C_PASS };
        static final int[]      TO      = { R.id.row_user, R.id.row_pass };
        Cursor                  cursor;
        SimpleCursorAdapter     adapter;


        @Override
        protected void onCreate(Bundle savedInstanceState)
            {

                Log.d(TAG, "onCreate-------Start");

                super.onCreate(savedInstanceState);
                setContentView(R.layout.row_users);


                make_and_fill_rows();

                Log.d(TAG, "onCreate-------End");

            }



        static final ViewBinder VIEW_BINDER = new ViewBinder()
                                                {


                                                    public
                                                            boolean
                                                            setViewValue(
                                                                    View view,
                                                                    Cursor cursor,
                                                                    int columnIndex)
                                                        {

                                                            Log.d(
                                                                    TAG,
                                                                    "Entering switch-case...");
                                                            switch (view
                                                                    .getId())
                                                                {
                                                                    case R.id.row_user:
                                                                        ((TextView) view)
                                                                                .setText("userrrrrr");
                                                                        return true;

                                                                    case R.id.row_pass:
                                                                        ((TextView) view)
                                                                                .setText("passssssss");
                                                                        return true;


                                                                    default:
                                                                        return false;
                                                                }
                                                        }

                                                };

        private void make_and_fill_rows()
            {

                Log.d(TAG, "make_and_fill_rows-----Start");

                cursor = ((Constant) getApplication()).data.query("1=1", null);

                adapter =
                            new SimpleCursorAdapter(
                                    this,
                                    R.layout.row_users,
                                    cursor,
                                    FROM,
                                    TO);
                adapter.setViewBinder(VIEW_BINDER);

                setTitle("کاربران");
                setListAdapter(adapter);

                Log.d(TAG, "make_and_fill_rows-----End");
            }

    }

这是我的布局 xml:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true" >

    <TextView
        android:id="@+id/row_user"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="20dp"
        android:text="user" />

    <TextView
        android:id="@+id/row_pass"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="22dp"
        android:text="pass" />

    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" >
    </ListView>

</LinearLayout>

我在另一个布局中有一个按钮,它在 onClickListener 中启动此 Activity 。 谁能帮帮我?

最佳答案

替换这一行

    android:id="@+id/list"

有了这个

    android:id="@android:id/list"

你得到的异常

Your content must have a ListView whose id attribute is 'android.R.id.list'

它说你必须定义你的 ListView 已经存在的资源 id android.R.id.list 你可以使用 @android:id/列表

关于android - 在布局中频繁填充 listView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23076787/

相关文章:

eclipse:添加Chrome扩展库

android - 如何以编程方式检测android中可用的底部软导航栏?

android - 在 Android 中使用 SearchView 进行搜索后,ListView 未更新

android - 如何在 'release' 版本上不编译某些 Android 菜单项

eclipse - 一个自动格式工具可以自动在单行子句周围插入大括号?

xml - android View 中经常出现的问题,解析XML时出错: unbound prefix

eclipse - 无法从 Intershop Studio 启动应用程序服务器

Android - 更改屏幕方向,方向将应用于整个系统

java - 在蓝牙安卓中配对设备

android - 如何在 Eclipse 中刷新项目