【问题标题】:Error in Implementing SimpleCursorAdapter实现 SimpleCursorAdapter 时出错
【发布时间】:2018-06-18 06:34:43
【问题描述】:

我有如下所示的代码。 加载.java

public class Load extends AppCompatActivity {

ListView list;
MydbHelper db;
SimpleCursorAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_load);
    db= new MydbHelper(getApplicationContext());
    list = findViewById(R.id.lv1);
    String[] arrayCols = new String[]{"_id","NAME","PHONE"};
    int[] arrayIDs = new int[]{0,R.id.tv1,R.id.tv2};
    Cursor data = db.getdata();
    if (data.getCount()==0){
        Toast.makeText(this, "No data to print in the list", Toast.LENGTH_SHORT).show();
    }
    else{
        do{
            //adapter = new NewAdapter(getApplicationContext(),R.layout.mylist,data,0);
            adapter= new SimpleCursorAdapter(this, R.layout.mylist,data,arrayCols,arrayIDs,0);
            list.setAdapter(adapter);
            Toast.makeText(this,"list is running",Toast.LENGTH_LONG).show();
        }while (data.moveToNext());
    }

}

}

类 MydbHelper.java

  public class MydbHelper extends SQLiteOpenHelper {
Context mycontext;
public MydbHelper(Context context) {
    super(context, "MyDB", null, 1);
    this.mycontext=context;
}

@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
    sqLiteDatabase.execSQL("CREATE TABLE BHASKAR(_id INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, PHONE INT);");
    sqLiteDatabase.execSQL("INSERT INTO BHASKAR VALUES(1,'RAHUL',9127543008)");
}

@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

}
public Cursor getdata(){
    SQLiteDatabase database = getReadableDatabase();

    Cursor data = database.rawQuery("SELECT * FROM BHASKAR",null);
    return data;
}

} 遇到如下错误:

Logcat 中的错误:Screenshot of Logcat

引起:java.lang.IllegalArgumentException:列'_id'没有 存在 在 android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:303) 在 android.widget.CursorAdapter.init(CursorAdapter.java:172) 在 android.widget.CursorAdapter.(CursorAdapter.java:149) 在 android.widget.ResourceCursorAdapter.(ResourceCursorAdapter.java:91) 在 android.widget.SimpleCursorAdapter.(SimpleCursorAdapter.java:104)

请帮帮我。 在 in.complit.csync.Load.onCreate(Load.java:31)

【问题讨论】:

  • Error inImplementing SimpleCursorAdapter 什么错误??

标签: java android sqlite listview android-cursoradapter


【解决方案1】:
//MyHelper.java

public class MyHelper extends SQLiteOpenHelper {

    public MyHelper(Context context) {
        super(context,"student",null,1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

        db.execSQL("create table stud(rollno int primary key,name TEXT,phone long);");
        db.execSQL("insert into stud values(1,'bca',9988998899);");
        db.execSQL("insert into stud values(2,'mca',9988998897);");

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}

//MainActivity.java

public class MainActivity extends AppCompatActivity {

    MyHelper mh;
    SQLiteDatabase db;
    Cursor rs;
    ListView lv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        MyHelper mh = new MyHelper(getApplicationContext());
        db = mh.getReadableDatabase();

        lv = (ListView) findViewById(R.id.mylist);

        rs = db.rawQuery("select rollno _id,name,phone from stud",null);

        SimpleCursorAdapter sca = new SimpleCursorAdapter(getApplicationContext(),
                R.layout.detail_adapter,rs,new String[]{"_id","name","phone"},
                new int[]{R.id.textid,R.id.textname,R.id.textphone},0);

        lv.setAdapter(sca);
    }
}


    //activity_main.xml

       <ListView
            android:id="@+id/mylist"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true" />

//detail_adapter.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <TextView
        android:text="TextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/textid" />

    <TextView
        android:text="TextView"
        android:layout_marginLeft="25dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/textname" />

    <TextView
        android:text="TextView"
        android:layout_marginLeft="25dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/textphone" />
</LinearLayout>

【讨论】:

    【解决方案2】:

    由于您没有提及您遇到的确切错误,我可能会建议您可以在代码中进行的更改。

    list = findViewById(R.id.lv1);
    String[] arrayCols = new String[]{"NAME", "PHONE"};
    int[] arrayIDs = new int[]{R.id.tv1, R.id.tv2};
    Cursor data = db.getdata();
    
    if (data.getCount() == 0) {
        Toast.makeText(this, "No data to print in the list", Toast.LENGTH_SHORT).show();
    
    } else {
    
        // Move the cursor to first position
        data.moveToFirst();
    
        // Initialize the adapter here. 
        adapter = new SimpleCursorAdapter(this, R.layout.mylist, data, arrayCols, arrayIDs, 0);
        list.setAdapter(adapter);
    }
    

    然后在您的SimpleCursorAdapter 中循环访问您的data

    【讨论】:

    • logcat 中的错误 :::::::::::::::::::::::::::::::::::::: :::::::::::::::::::::::::::::::::::::::::::::::::: :::::::::::::::::::::::::::::::::: 引起:java.lang.IllegalArgumentException:列'_id'不存在在 android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:303) .....在 in.complit.csync.Load.onCreate(Load.java:29) 其中 (Load.java:29) 包含行“适配器= new SimpleCursorAdapter(this, R.layout.mylist, data, arrayCols, arrayIDs, 0); "
    • 如果没有适配器中的代码(即 SimpleCursorAdapter),我们无法为您提供帮助。请确保您也创建了数据库。
    • public MydbHelper(Context context) { super(context, "MyDB", null, 1); this.mycontext=上下文; } public void onCreate(SQLiteDatabase sqLiteDatabase) { sqLiteDatabase.execSQL("CREATE TABLE BHASKAR(NAME TEXT, PHONE INT);"); sqLiteDatabase.execSQL("插入 BHASKAR 值('RAHUL',9127543008)"); }
    • done 先生,请过代码并针对具体错误提供合适的解决方案
    【解决方案3】:

    无需使用do{...}whie(...) 循环遍历数据游标。只需按如下方式更改您的代码

    list = findViewById(R.id.lv1);
    String[] arrayCols = new String[]{"NAME","PHONE"};
    int[] arrayIDs = new int[]{R.id.tv1,R.id.tv2};
    Cursor data = db.getdata();
    if (data.getCount()==0) {
        Toast.makeText(this, "No data to print in the list", Toast.LENGTH_SHORT).show();
    }
    else{
        adapter= new SimpleCursorAdapter(this, R.layout.mylist,data,arrayCols,arrayIDs,0);
        list.setAdapter(adapter);
        Toast.makeText(this,"list is running",Toast.LENGTH_LONG).show();           
    }
    

    注意:

    请记住在创建过程中在左括号'('_id 之间添加一个空格。您可能需要卸载并安装应用程序才能正确进行更改。

    【讨论】:

    • 他需要循环遍历SimpleCursorAdapter里面的数据。
    • 引起:java.lang.IllegalArgumentException:列'_id'不存在......在in.complit.csync.Load.onCreate(Load.java:29) where ( Load.java:29) 包含 adapter= new SimpleCursorAdapter(this, R.layout.mylist,data,arrayCols,arrayIDs,0);
    • @BhaskarRanjan 您的数据库是否有名为“NAME”和“PHONE”的列?
    • public void onCreate(SQLiteDatabase sqLiteDatabase) { sqLiteDatabase.execSQL("CREATE TABLE BHASKAR(NAME TEXT, PHONE INT);"); sqLiteDatabase.execSQL("插入 BHASKAR 值('RAHUL',9127543008)"); }
    • @BhaskarRanjan 你能用String[] arrayCols = new String[]{"_id","NAME","PHONE"};
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多