The application crashes when i click button
. with the error android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
.
This is my: MainActivty.java
public class MainActivity extends AppCompatActivity{
EditText editText,editText2;
ListView listView;
Button button,button2;
DBHelper dbHelper = new DBHelper(this);
Cursor cursor = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
populateListView();
editText = (EditText)findViewById(R.id.editText);
editText2 = (EditText)findViewById(R.id.editText2);
button = (Button)findViewById(R.id.button);
button2 = (Button)findViewById(R.id.button2);
listView = (ListView)findViewById(R.id.listView);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Cursor mycursor = (Cursor) parent.getItemAtPosition(position);
String str = mycursor.getString(1);
Toast.makeText(getBaseContext(),str,Toast.LENGTH_SHORT).show();
dbHelper.deleteData(str);
populateListView();
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SQLiteDatabase db = dbHelper.getWritableDatabase();
dbHelper.onUpgrade(db, 1, 2);
populateListView();
}
});
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(editText.getText().toString().isEmpty() ||editText2.getText().toString().isEmpty()) return;
Cursor upCursor = dbHelper.getData();
boolean presence = false;
if(upCursor!=null) {
upCursor.moveToFirst();
do{
if(upCursor.getString(1).equals(editText.getText().toString())) presence = true;
upCursor.moveToNext();
}while(!upCursor.isAfterLast());
}
else;
if(presence)
dbHelper.updateData(editText.getText().toString(), Integer.parseInt(editText2.getText().toString()));
else dbHelper.insertActivity(editText.getText().toString(), Integer.parseInt(editText2.getText().toString()));
populateListView();
}
});
private void populateListView(){
cursor = dbHelper.getData();
cursor.moveToFirst();
String[] fromFieldNames = new String[] {dbHelper.ROW_ID,dbHelper.ACTIVITY_TITLE,dbHelper.ACTIVITY_PRIORITY};
int[] toViewIDs = new int[] {R.id.row_id, R.id.activity_name,R.id.activity_priority};
if(cursor == null) Toast.makeText(getBaseContext(),"cursor is null",Toast.LENGTH_SHORT).show();
else
{
Toast.makeText(getBaseContext(),"cursor is not null",Toast.LENGTH_SHORT).show();
SimpleCursorAdapter myCursorAdapter = new SimpleCursorAdapter(getBaseContext(),
R.layout.list_layout,cursor,fromFieldNames,toViewIDs,0);
//listView.setAdapter(myCursorAdapter);
}
}
}
This is my DBHelper.java
class:
public class DBHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "MyDBName.db";
public static final int database_version = 24;
public static final String TABLE_NAME = "list_11";
public static final String ACTIVITY_TITLE = "activity_title";
public static final String ROW_ID = "_id";
public static final String ACTIVITY_PRIORITY = "priority";
public DBHelper(Context context)
{
super(context, DATABASE_NAME , null, database_version);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(
"create table list_11 " +
"( _id integer primary key autoincrement, activity_title text, priority int )"
);
Log.d("DBHelper", "onCreate");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS list_11");
onCreate(db);
Log.d("DBHelper", "onUpgrade");
}
public void insertActivity (String name,int a)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("activity_title", name);
contentValues.put("priority", a);
db.insert(TABLE_NAME, null, contentValues);
Log.d("DBHelper", "one row inserted");
}
public Cursor getData(){
SQLiteDatabase db = this.getReadableDatabase();
String[] columns = {ROW_ID,ACTIVITY_TITLE,ACTIVITY_PRIORITY};
Cursor res = db.query(TABLE_NAME, columns, null, null,null,null,ACTIVITY_PRIORITY+" DESC ");
return res;
}
public void updateData(String name,int a)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(ACTIVITY_PRIORITY,a);
db.update(TABLE_NAME,contentValues,ACTIVITY_TITLE+" = ?",new String[] {name});
}
public void deleteData(String name)
{
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_NAME,ACTIVITY_TITLE+" = ?",new String[] {name});
}
}
Aucun commentaire:
Enregistrer un commentaire