0

I am having the code as shown below. Load.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());
    }

}

}

Class 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;
}

} It encountered an error as follows:

Error in the Logcat:Screenshot of Logcat

Caused by: java.lang.IllegalArgumentException: column '_id' does not exist at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:303) at android.widget.CursorAdapter.init(CursorAdapter.java:172) at android.widget.CursorAdapter.(CursorAdapter.java:149) at android.widget.ResourceCursorAdapter.(ResourceCursorAdapter.java:91) at android.widget.SimpleCursorAdapter.(SimpleCursorAdapter.java:104)

Please do help me. at in.complit.csync.Load.onCreate(Load.java:31)

Sagar
  • 23,903
  • 4
  • 62
  • 62

3 Answers3

2
//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>
0

There is no need to loop through the data cursor using do{...}whie(...). Simply change your code as follows

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();           
}

Note:

Remember to add a space between the left parenthesis '(' and _id during creation. You might need to uninstall and install the app so that changes are taken place correctly.

Sagar
  • 23,903
  • 4
  • 62
  • 62
  • He needs to loop through the data inside `SimpleCursorAdapter`. – Reaz Murshed Jun 18 '18 at 06:43
  • Caused by: java.lang.IllegalArgumentException: column '_id' does not exist....... at in.complit.csync.Load.onCreate(Load.java:29) where (Load.java:29) contains adapter= new SimpleCursorAdapter(this, R.layout.mylist,data,arrayCols,arrayIDs,0); – Bhaskar Ranjan Jun 18 '18 at 07:05
  • @BhaskarRanjan does your DB has column named 'NAME' & 'PHONE' ? – Sagar Jun 18 '18 at 07:07
  • public void onCreate(SQLiteDatabase sqLiteDatabase) { sqLiteDatabase.execSQL("CREATE TABLE BHASKAR(NAME TEXT, PHONE INT);"); sqLiteDatabase.execSQL("INSERT INTO BHASKAR VALUES('RAHUL',9127543008)"); } – Bhaskar Ranjan Jun 18 '18 at 07:09
  • @BhaskarRanjan can you use `String[] arrayCols = new String[]{"_id","NAME","PHONE"};` – Sagar Jun 18 '18 at 07:12
  • @BhaskarRanjan can you add a space between the left parenthesis '(' and _id during creation? – Sagar Jun 18 '18 at 07:36
  • @BhaskarRanjan You might need to uninstall and install the app when you change the create query. Also change PHONE to `INTEGER` rather than `INT` – Sagar Jun 18 '18 at 07:41
  • it is done by uninstalling the .apk file from the device. – Bhaskar Ranjan Jun 19 '18 at 05:35
  • yes, can you tell how can I implement onClickListener() to the adapter . – Bhaskar Ranjan Jun 19 '18 at 05:51
  • @BhaskarRanjan Great! Remember to mark answer as accepted if it was useful, so that others can benefit too. For clickListener, you can follow this [SO](https://stackoverflow.com/questions/7645880/listview-with-onitemclicklistener-android) – Sagar Jun 19 '18 at 05:53
0

As you did not mention what exactly the error that you are getting there, I might suggest the changes that you can do in your code.

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);
}

Then loop through your data in your SimpleCursorAdapter.

Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
  • Error in the logcat ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::Caused by: java.lang.IllegalArgumentException: column '_id' does not exist at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:303) .....at in.complit.csync.Load.onCreate(Load.java:29) where (Load.java:29) contain the line " adapter = new SimpleCursorAdapter(this, R.layout.mylist, data, arrayCols, arrayIDs, 0); " – Bhaskar Ranjan Jun 18 '18 at 07:02
  • We cannot help you without the code in your adapter, i.e. SimpleCursorAdapter. And please make sure that, you have created the database as well. – Reaz Murshed Jun 18 '18 at 07:05
  • public MydbHelper(Context context) { super(context, "MyDB", null, 1); this.mycontext=context; } public void onCreate(SQLiteDatabase sqLiteDatabase) { sqLiteDatabase.execSQL("CREATE TABLE BHASKAR(NAME TEXT, PHONE INT);"); sqLiteDatabase.execSQL("INSERT INTO BHASKAR VALUES('RAHUL',9127543008)"); } – Bhaskar Ranjan Jun 18 '18 at 07:11
  • done sir, please go through the code and provide a suitable solution for the specific error – Bhaskar Ranjan Jun 18 '18 at 07:34