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)