Loading [MathJax]/jax/output/CommonHTML/config.js
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >未能从具有1行9列的CursorWindow中读取第0行第1列

未能从具有1行9列的CursorWindow中读取第0行第1列
EN

Stack Overflow用户
提问于 2022-06-13 22:16:31
回答 1查看 73关注 0票数 -1

我试图在我的活动中将数据从sqlite中的表加载到我的ExpandableListView。我跟踪了这个问题的答案,Android ExpandableListView and SQLite Database

我的父列表将显示MVP_INDUSTRY_TYPE列取决于选定的日期,我的子列表将显示MVP_BRCH_CODE_NAME和MVP_BRCH_ADDRESS。

DatabaseHelper。java

代码语言:javascript
运行
AI代码解释
复制
package com.example.spmapp;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import androidx.annotation.Nullable;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


public class DataBaseHelper extends SQLiteOpenHelper {

    public static final String MVP_TBL = "MVP_tbl";
    public static final String MVP_BRCH_CODE_NAME = "MVP_BRCH_CODE_NAME";
    public static final String MVP_ID = "MVP_ID";
    public static final String MVP_BRCH_ADDRESS = "MVP_BRCH_ADDRESS";
    public static final String MVP_AREA = "AREA";
    public static final String MVP_AREA_CODE = "AREA_CODE";
    public static final String MVP_INDUSTRY_TYPE = "MVP_INDUSTRY_TYPE";
    public static final String token = "TOKEN";
    public static final String MVP_DATE = "SCHEDULED_DATE";
    public static final String MVP_CLASS_ID = "MVP_CLASS_ID";



    public DataBaseHelper(@Nullable Context context) {
        super(context, "taggedList.db", null, 1);

    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        String createMVPVirtualTableStatement = "CREATE VIRTUAL TABLE IF NOT EXISTS " + MVP_TBL + " USING FTS3(" + MVP_BRCH_CODE_NAME + ", " + MVP_ID + " , " + MVP_BRCH_ADDRESS + ", " + MVP_AREA + ", " + MVP_AREA_CODE + ", " + MVP_INDUSTRY_TYPE + ", " + token + ", " + MVP_DATE + ", " + MVP_CLASS_ID + ")";
        sqLiteDatabase.execSQL(createMVPVirtualTableStatement);

    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
        onCreate(sqLiteDatabase);
    }
    public Cursor getSelectedParentMVPDate(String txtMVPDate){
        SQLiteDatabase db = this.getWritableDatabase();
        String query = "Select * from "+MVP_TBL+" WHERE " +MVP_DATE+ " LIKE '%"+txtMVPDate+"%' LIMIT 50";
        return db.rawQuery(query, null);
    }
    public Cursor getSelectedChildMVPDate(String MVPIndustry){
        SQLiteDatabase db = this.getWritableDatabase();
        String query = "Select * from "+MVP_TBL+" WHERE " +MVP_INDUSTRY_TYPE+ " LIKE '%"+MVPIndustry+"%' LIMIT 50";
        return db.rawQuery(query, null);
    }

}

MasterVisitPlan.java

代码语言:javascript
运行
AI代码解释
复制
public class MasterVisitPlan extends AppCompatActivity implements AdapterView.OnItemSelectedListener,CalendarAdapter.OnItemListener {

    private boolean isBackPressedOnce = false;

    public static String email;
    public static String VisitDate;
    public static String ItemBrnchCodeName;
    public static String mrCode;
    public static String ClassID;
    private static String token;
    private static String bearerToken;
    public static int counterVisit;
    private RecyclerView calendarRecyclerView;
    private LocalDate selectedDate;
    private TextView monthYearText;


    TextView empName, empPos, date;

    GoogleSignInClient mGoogleSignInClient;

    DrawerLayout drawerLayout;

    ImageView gps, empPhoto;

    ConstraintLayout calendar;

    DataBaseHelper dataBaseHelper;
    private Cursor mGroupsCursor; // cursor for list of groups (list top nodes)
    private int mGroupIdColumnIndex;
    private MyExpandableListAdapter mAdapter;

    SimpleCursorAdapter sca;
    Cursor csr;
    SearchView searchView;
    ListView Searchlstview;


    @Override
    protected void onCreate(Bundle savedIntanceState) {
        super.onCreate(savedIntanceState);
        setContentView(R.layout.activity_master_visit_plan);
        initWidgets();
        selectedDate = LocalDate.now();
        setMonthView();
        setOrRefreshListView();
        getCurrentDate();

        dataBaseHelper = new DataBaseHelper(MasterVisitPlan.this);
        fillMVPdata();  
    }

    private void fillMVPdata(){
        mGroupsCursor = dataBaseHelper.getSelectedParentMVPDate(date.getText().toString());
        startManagingCursor(mGroupsCursor);
        mGroupsCursor.moveToFirst();


        ExpandableListView Selectedlstview = (ExpandableListView) findViewById(R.id.MVPListitem);
        mAdapter = new MyExpandableListAdapter(mGroupsCursor, this,

                R.layout.mvp_list_parent,
                R.layout.mvp_list_child,
                new String[] {DataBaseHelper.MVP_INDUSTRY_TYPE},
                new int[] {R.id.txtMVPParent},
                new String[] {DataBaseHelper.MVP_BRCH_CODE_NAME, DataBaseHelper.MVP_BRCH_ADDRESS},
                new int[] {R.id.txtviewBrnchCodeName, R.id.txtviewBrchAddr});

                Selectedlstview.setAdapter(mAdapter);

    }
    public class MyExpandableListAdapter extends SimpleCursorTreeAdapter {
        public MyExpandableListAdapter(Cursor cursor, Context context,int groupLayout, int childLayout, String[] groupFrom,
                                       int[] groupTo, String[] childrenFrom, int[] childrenTo) {
            super(context, cursor, groupLayout, groupFrom, groupTo,
                    childLayout, childrenFrom, childrenTo);
        }

        @Override
        protected Cursor getChildrenCursor(Cursor cursor) {
            @SuppressLint("Range") Cursor childCursor = dataBaseHelper.getSelectedChildMVPDate(cursor.getString(cursor.getColumnIndex("MVP_INDUSTRY_TYPE")));
            startManagingCursor(childCursor);
            childCursor.moveToFirst();
            return childCursor;
        }
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-06-14 17:59:56

getColumnIndex -1表示在使用方法时列不在游标中。

由于提供的代码中唯一出现的方法是在getSelectedChildMVPDate类中的DatabaseHelper方法中,所以问题必须发生在那里。

使用DataBaseHelper类中的附加方法复制代码的要点:-

代码语言:javascript
运行
AI代码解释
复制
public void insertMVPTBLRow(String branchCodeName, long id, String address, String area, String areacode, String industryType, String tkn, String date, String classId) {
    ContentValues cv = new ContentValues();
    cv.put(MVP_BRCH_CODE_NAME,branchCodeName);
    cv.put(MVP_ID,id);
    cv.put(MVP_BRCH_ADDRESS,address);
    cv.put(MVP_AREA,area);
    cv.put(MVP_AREA_CODE,areacode);
    cv.put(MVP_INDUSTRY_TYPE,industryType);
    cv.put(token,tkn);
    cv.put(MVP_DATE,date);
    cv.put(MVP_CLASS_ID,classId);
    this.getWritableDatabase().insert(MVP_TBL,null,cv);
}

然后在活动中使用以下代码:

代码语言:javascript
运行
AI代码解释
复制
    dataBaseHelper = new DataBaseHelper(this);
    dataBaseHelper.getWritableDatabase().delete(DataBaseHelper.MVP_TBL,null,null);
    dataBaseHelper.insertMVPTBLRow("B1",100,"StreetX","Area1","A001","I001","tkn001",String.valueOf(System.currentTimeMillis()),"CLASS1");
    dataBaseHelper.insertMVPTBLRow("B1",101,"StreetX","Area1","A001","I001","tkn002",String.valueOf(System.currentTimeMillis()),"CLASS1");
    mGroupsCursor = dataBaseHelper.getSelectedParentMVPDate("");
    startManagingCursor(mGroupsCursor);
    mGroupsCursor.moveToFirst();
    Cursor cursor = mGroupsCursor;
    @SuppressLint("Range") Cursor childCursor = dataBaseHelper.getSelectedChildMVPDate(cursor.getString(cursor.getColumnIndex("MVP_INDUSTRY_TYPE")));
    startManagingCursor(childCursor);
    childCursor.moveToFirst();
    DatabaseUtils.dumpCursor(mGroupsCursor);
    DatabaseUtils.dumpCursor(childCursor);

传递

  • Note空字符串,以便提取所有行。

使用日志成功运行的结果包括:-

代码语言:javascript
运行
AI代码解释
复制
2022-06-15 10:55:26.892 I/System.out: >>>>> Dumping cursor android.database.sqlite.SQLiteCursor@2caa87e
2022-06-15 10:55:26.893 I/System.out: 0 {
2022-06-15 10:55:26.893 I/System.out:    MVP_BRCH_CODE_NAME=B1
2022-06-15 10:55:26.893 I/System.out:    MVP_ID=100
2022-06-15 10:55:26.893 I/System.out:    MVP_BRCH_ADDRESS=StreetX
2022-06-15 10:55:26.893 I/System.out:    AREA=Area1
2022-06-15 10:55:26.893 I/System.out:    AREA_CODE=A001
2022-06-15 10:55:26.893 I/System.out:    MVP_INDUSTRY_TYPE=I001
2022-06-15 10:55:26.893 I/System.out:    TOKEN=tkn001
2022-06-15 10:55:26.893 I/System.out:    SCHEDULED_DATE=1655254526888
2022-06-15 10:55:26.893 I/System.out:    MVP_CLASS_ID=CLASS1
2022-06-15 10:55:26.893 I/System.out: }
2022-06-15 10:55:26.893 I/System.out: 1 {
2022-06-15 10:55:26.893 I/System.out:    MVP_BRCH_CODE_NAME=B1
2022-06-15 10:55:26.893 I/System.out:    MVP_ID=101
2022-06-15 10:55:26.893 I/System.out:    MVP_BRCH_ADDRESS=StreetX
2022-06-15 10:55:26.894 I/System.out:    AREA=Area1
2022-06-15 10:55:26.894 I/System.out:    AREA_CODE=A001
2022-06-15 10:55:26.894 I/System.out:    MVP_INDUSTRY_TYPE=I001
2022-06-15 10:55:26.894 I/System.out:    TOKEN=tkn002
2022-06-15 10:55:26.895 I/System.out:    SCHEDULED_DATE=1655254526889
2022-06-15 10:55:26.895 I/System.out:    MVP_CLASS_ID=CLASS1
2022-06-15 10:55:26.895 I/System.out: }
2022-06-15 10:55:26.895 I/System.out: <<<<<
2022-06-15 10:55:26.895 I/System.out: >>>>> Dumping cursor android.database.sqlite.SQLiteCursor@f1451df
2022-06-15 10:55:26.895 I/System.out: 0 {
2022-06-15 10:55:26.895 I/System.out:    MVP_BRCH_CODE_NAME=B1
2022-06-15 10:55:26.895 I/System.out:    MVP_ID=100
2022-06-15 10:55:26.895 I/System.out:    MVP_BRCH_ADDRESS=StreetX
2022-06-15 10:55:26.895 I/System.out:    AREA=Area1
2022-06-15 10:55:26.895 I/System.out:    AREA_CODE=A001
2022-06-15 10:55:26.895 I/System.out:    MVP_INDUSTRY_TYPE=I001
2022-06-15 10:55:26.895 I/System.out:    TOKEN=tkn001
2022-06-15 10:55:26.896 I/System.out:    SCHEDULED_DATE=1655254526888
2022-06-15 10:55:26.896 I/System.out:    MVP_CLASS_ID=CLASS1
2022-06-15 10:55:26.896 I/System.out: }
2022-06-15 10:55:26.896 I/System.out: 1 {
2022-06-15 10:55:26.896 I/System.out:    MVP_BRCH_CODE_NAME=B1
2022-06-15 10:55:26.896 I/System.out:    MVP_ID=101
2022-06-15 10:55:26.896 I/System.out:    MVP_BRCH_ADDRESS=StreetX
2022-06-15 10:55:26.896 I/System.out:    AREA=Area1
2022-06-15 10:55:26.896 I/System.out:    AREA_CODE=A001
2022-06-15 10:55:26.896 I/System.out:    MVP_INDUSTRY_TYPE=I001
2022-06-15 10:55:26.896 I/System.out:    TOKEN=tkn002
2022-06-15 10:55:26.896 I/System.out:    SCHEDULED_DATE=1655254526889
2022-06-15 10:55:26.896 I/System.out:    MVP_CLASS_ID=CLASS1
2022-06-15 10:55:26.896 I/System.out: }
2022-06-15 10:55:26.897 I/System.out: <<<<<

现在考虑到消息未能从包含1行、9列的CursorWindow中读取第0行第1列。

然后

  1. 光标定位在第0行(而不是位于第一行之前的第1行)

未找到指定列的9列的

  1. 从两个游标的转储中有9列.

  1. getColumnIndex有一个错误,因为它是区分大小写的

然而,

  1. ,由于上面的工作,这不是您的代码的问题,尽管我强烈建议将您的代码更改为使用cursor.getColumnIndex(/*"MVP_INDUSTRY_TYPE" <<<<< CHANGED*/ DataBaseHelper.MVP_INDUSTRY_TYPE) (显然可以省略注释)

因此,

  1. 的问题是:-

代码语言:javascript
运行
AI代码解释
复制
1. Elsewhere in your code, or
2. That the columns name coded/used in the `onCreate` method have been changed BUT that they have not been applied. 
    - The `onCreate` method runs once and only once when the database is created.
    - The `onCreate` method will never automatically run again unless the database is deleted.
    - Uninstalling the App will delete the database BUT will lose any existing data.
    - If you need to retain existing data, then you need to rebuild the table with the correct column name, noting that virtual tables has restrictions. You would probably need to utilise the `onOpen` method and within the method:- 
        - create the new table BUT with a different name
代码语言:javascript
运行
AI代码解释
复制
        - populate the newly created table with the data from the orginal table
代码语言:javascript
运行
AI代码解释
复制
        - drop the original table
代码语言:javascript
运行
AI代码解释
复制
        - rename the newly created table with the original/required name
代码语言:javascript
运行
AI代码解释
复制
        - However, as `onOpen` runs whenever the database is opened you would want to limit this to only do the above when required (i.e. when there is a naming discrepancy). The following `onOPen` method could be the basis

:-

代码语言:javascript
运行
AI代码解释
复制
@Override
public void onOpen(SQLiteDatabase db) {
    super.onOpen(db);
    Log.d("ONOPEN","ON OPEN INVOKED");
    Cursor checkit = db.query(MVP_TBL,null,null,null,null,null,null,"1");
    boolean needToProceed = false;
    for (String s: new String[]{MVP_AREA,MVP_AREA_CODE,MVP_DATE,MVP_ID,MVP_BRCH_ADDRESS,MVP_BRCH_CODE_NAME,MVP_CLASS_ID,MVP_INDUSTRY_TYPE,token}) {
        boolean found = false;
        for (String cc: checkit.getColumnNames()) {
            if (cc.equals(s)) {
                Log.d("ONOPEN","Column " + cc + " matched " + s);
                found = true;
                break;
            }
        }
        if (!found) {
            Log.d("ONOPEN","!!" + s + " NOT MATCHED.!!" );
            needToProceed = true;
            break;
        }
    }
    if (!needToProceed) return;
    Log.d("ONOPEN","PROCEEDING WITH RECREATION OF " + MVP_TBL +" TABLE.");
    String renamed = "ORIGINAL_" + MVP_TBL;
    db.execSQL("ALTER TABLE " + MVP_TBL + " RENAME TO " + renamed + ";");
    onCreate(db);
    db.execSQL("INSERT INTO " + renamed + " SELECT * FROM " + MVP_TBL);
    db.execSQL("DROP TABLE IF EXISTS " + MVP_TBL);
    db.execSQL("ALTER TABLE " + renamed + " RENAME TO " + MVP_TBL);
}

如果发布App.,则应删除日志记录

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72612275

复制
相关文章
Ubuntu安装时出现黑屏问题的解决
问题描述:Ubuntu使用光盘/USB安装时,出现"install ubuntu/ try ubuntu without installation"选择,但是Enter安装时,显示器显示没有信息,进行休眠
知忆
2021/06/07
14.1K0
在Ubuntu上安装Minikube
为了方便开发者体验Kubernetes,社区提供了可以在本地部署的Minikube。由于在国内网络环境内,无法顺利的安装使用Minikube,我们可以从阿里云的镜像地址来获取所需Docker镜像和配置。
mazhen
2023/11/24
1K1
在Ubuntu上安装ReviewBoard
因为ReviewBoard在Windows上停止更新了,所以了解如何在Linux系统安装ReviewBoard是很有必要的。笔者只在Apache+MySQL+Ubuntu下实现过,其余均参考官方安装文档:http://www.reviewboard.org/docs/manual/dev/admin/installation/linux/。 [Note] 安装过程中如碰到报错问题,自行谷歌或者百度,大部分均可解决。有些是软件版本问题,比如之前用Ubuntu 12.04有个软件包用easy_install安装的版本一直低于安装ReviewBoard的要求,升级成Ubuntu 13.04之后就没问题了。
星哥玩云
2022/07/01
1.6K0
在 Ubuntu 上安装 MySQL
在 Ubuntu 中安装 Mysql 最方便方式是用 MySQL 自己的 APT 仓库。 APT 仓库中包含了 MySQL 的服务器和工具相关的软件。我们需要将此 MySQL APT 仓库添加到系统的包源列表中。
不惑
2023/09/23
1.3K0
在自己的电脑上安装GitBook For Mac
之前写了一篇 在自己的服务器上安装GitBook ,发布于 2015-04-20,当时的安装环境是 CentOS 6.X_64Bit,今天想再研究一下这个 GitBook ,发现在基于 Linux 内核的机器上安装基础功能很简单,但是牵涉到生成 pdf/epub/mobi 格式的书籍时,安装calibre的代价非常大(因为要安装 N 多前置库),对于不是非常非常熟练使用 Linux 的用户,几乎是一种灾难。
他叫自己MR.张
2019/07/01
3K0
在Ubuntu上安装使用CouchPotato
CouchPotato(https://couchpota.to/)是开源的自动种子下载器,它是免费的影视资源搜索器,您只要输入资源的名称和或关键词,即可进行后台搜索,支持下载字幕,支持通过bt软件下载。CouchPotato使用C/S架构,Python开发。 CouchPotato源代码:https://github.com/CouchPotato/CouchPotatoServer CouchPotato (CP) is an automatic NZB and torrent downloader.
Zip
2018/07/20
3.3K0
在Ubuntu 16.04上安装Rancher
Rancher是运行容器和构建私有容器服务的开源平台。 Rancher基于Docker,因此您可以在专用的box,KVM机器上甚至LXC容器上运行它。 Rancher提供了一个庞大的应用程序库,只需点击几下即可安装,并且还支持来自Dockerhub的Docker镜像。
星哥玩云
2022/07/12
1.7K0
在Ubuntu上安装Oracle JDK
如果不想使用Ubuntu提供的OpenJDK,想使用Oracle的版本。可以进行如下操作
EltonZheng
2021/01/26
1.4K0
在Ubuntu上单机安装Hadoop
最近大数据比较火,所以也想学习一下,所以在虚拟机安装Ubuntu Server,然后安装Hadoop。
深蓝studyzy
2022/06/16
1.1K0
在Ubuntu 16.04上安装WordPress
在本指南中,您将学习如何在运行Ubuntu 16.04的Linode上安装WordPress。WordPress是一个流行的动态内容管理系统,专注于博客。WordPress可以部署在LAMP或LEMP堆栈上,并具有广泛的插件框架和主题系统,允许网站所有者和开发人员使用其简单但功能强大的发布工具。
Techeek
2018/09/17
5.2K0
在Ubuntu 16.04上安装WordPress
在 Ubuntu 上安装Microsoft Edge[通俗易懂]
1.要安装Microsoft Edge,你首先需要在终端中运行这些命令,在设备上设置微软的存储库。
全栈程序员站长
2022/09/13
2.3K0
在Ubuntu 16.04上安装MediaWiki
MediaWiki是一个流行的免费wiki软件包。它与维基百科使用的软件相同,是完全动态的,可以在LAMP堆栈上运行,利用PHP语言和MySQL数据库后端。通过简单的安装和配置,当您需要用到一个熟悉的、功能齐全的动态wiki引擎时,MediaWiki是一个很好的选择。
物花无语
2018/08/20
3.1K2
在Ubuntu 16.04上安装VNC
虚拟网络计算( Virtual Network Computing ),或称作VNC,是一种图形桌面共享系统,允许您从一台计算机远程控制另一台计算机。VNC服务器传输键盘和鼠标事件,并通过网络连接显示远程主机的屏幕,从而允许您在Linode服务器上运行完整的桌面环境。
苏易北
2018/09/11
6.2K0
在Ubuntu 16.04上安装VNC
在Ubuntu 18.04上安装WordPress
WordPress是一个非常流行的专注于博客的动态内容管理系统(CMS)。WordPress可以部署在LAMP或LEMP堆栈上。它具有的可扩展插件框架和主题系统允许网站所有者使用其简单但功能强大的发布工具。
eru
2018/09/05
7.8K0
在Ubuntu 18.04上安装WordPress
在Ubuntu 16.04上安装Java
Java是世界上最流行的编程语言之一。Java可用于创建从软件到基本Web应用程序的任何内容。
双愚
2018/09/12
1.6K0
在Ubuntu 14.04上安装 Webmin
Webmin是一个开源的基于网页的Unix/Linux系统管理工具。通过使用Webmin,你可以在浏览器上设置和安装所有的系统服务,包括:DNS、DHCP、Apache、NFS和Samba等等。因此,有了这个,你就再也不需要去记住所有的修改配置的命令了。
星哥玩云
2022/07/01
1.8K0
在Ubuntu 14.04上安装 Webmin
在 Ubuntu Linux 上安装 AnyDesk
AnyDesk 是一个流行的远程桌面软件,可用于 Linux、Windows、BSD、macOS 和移动平台。
用户8639654
2021/10/11
3.3K0
在 Ubuntu Linux 上安装 Dropbox
下载相应的 DEB 文件。考虑到你使用的是 64 位的 Ubuntu,请获取 64 位版本的 DEB 文件。
用户9105998
2021/11/22
2.5K0
在Ubuntu 14.04上安装Caffe
http://suanfazu.com/t/ubuntu-14-04-caffe/447/1 安装依赖 sudo apt-get install libatlas-base-dev sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libboost-all-dev libhdf5-serial-dev sudo apt-get install libgflags-dev libgoogle-g
bear_fish
2018/09/19
1.5K0
在 Ubuntu Linux 上安装 Dropbox
下载相应的 DEB 文件。考虑到你使用的是 64 位的 Ubuntu,请获取 64 位版本的 DEB 文件。
用户1685462
2021/09/12
2.1K0

相似问题

如果<img src="">没有图像

52

jQuery附加img src

33

用DomDocument实现img class=的多个src“覆盖图像”

21

基于img src Jquery创建下载链接

11

使用php从img src拉取更改图像

10
添加站长 进交流群

领取专属 10元无门槛券

AI混元助手 在线答疑

扫码加入开发者社群
关注 腾讯云开发者公众号

洞察 腾讯核心技术

剖析业界实践案例

扫码关注腾讯云开发者公众号
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档