【问题标题】:Values are not Populating in Listview [duplicate]Listview中未填充值[重复]
【发布时间】:2015-07-11 07:46:39
【问题描述】:

我正在尝试开发一个可以显示来自多个表的值的 android 应用程序。

我使用 Base Adapter 来显示值,因为我只需要在列表中显示查询的值。但是当我尝试在列表视图中显示值时,它是空白的。

我不确定我做错了什么。

我正在使用以下方法从数据库中查询数据以仅显示所需的值。

  public Cursor getAssetInStore()
    {

        SQLiteDatabase db_database = getReadableDatabase();

        Cursor c  = db_database.rawQuery("SELECT asset_name,warrenty,AssetImage,asset_status FROM `Assets`,`AseetIssued` WHERE Assets.Assetid = AseetIssued.Assetissuedid AND asset_status =?" ,
                new String [] {"In Storage"}, null);
        return c;

    }

以下方法用于将值填充到列表视图中。

private void populateAssetListView()
    {

        Cursor c = db_database.getAssetInStore();
        AssetListView.setAdapter(new AssetListAdapter(this, c));


    }


    public class AssetListAdapter  extends BaseAdapter
    {

        private Context mContext;
        Cursor cursor;

        public AssetListAdapter(Context context, Cursor c) {
            super();

            mContext = context;
            cursor =c ;

        }


        @Override
        public int getCount() {
            return 0;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View view, ViewGroup parent) {

            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
           //The layout is assigned to the custome created created in this case it is customeassetview the cales will be displayed as per this list
            view = inflater.inflate(R.layout.assetsinstore_placeholder, null);

            cursor.moveToPosition(position);

            TextView Assetsstatus = (TextView) view.findViewById(R.id.Assetssatutsstore_view);
            int status = cursor.getInt(cursor.getColumnIndex("asset_status"));
            Assetsstatus .setText(String.valueOf(status));

            TextView Assetsname = (TextView) view.findViewById(R.id.Assetstore_name_view);
            String Assetname = cursor.getString(cursor.getColumnIndex("asset_name"));
            Assetsname.setText(Assetname);

            TextView Warrenty = (TextView) view.findViewById(R.id.Assetstore_Warrenty_view);
            String CustDesignation = cursor.getString(cursor.getColumnIndex("warrenty"));
            Warrenty .setText(CustDesignation);


            ImageView AssetImage = (ImageView) view.findViewById(R.id.list_image);

            byte[] bb = cursor.getBlob(cursor.getColumnIndex("AssetImage"));
            AssetImage.setImageBitmap(BitmapConver.getPhoto(bb));

            return view;
        }
    }

【问题讨论】:

  • getCount 必须返回数据集的大小。它不能返回 0

标签: android android-listview


【解决方案1】:

尝试使用光标适配器代替基本适配器。此外,请注意您传递给光标的位置可能不是您想的那样。

【讨论】:

  • 我不想使用游标适配器,因为我需要将列从 Assetid 更改为 _id,在这种情况下,我会收到一个列名不明确的错误。如果您知道任何替代方案,您可以提出任何建议。
  • 为了解决这个问题,我通常在查询中直接使用我想用作 id 的列名(例如,select *, columname as _id from table)。
  • Thankx 确实使用光标适配器再次尝试了它,它现在可以工作了。
猜你喜欢
  • 1970-01-01
  • 2013-12-17
  • 1970-01-01
  • 1970-01-01
  • 2015-04-24
  • 2015-07-06
  • 1970-01-01
  • 2013-12-31
  • 2019-05-21
相关资源
最近更新 更多