Answer the question
In order to leave comments, you need to log in
How to remove the error of working with the database?
application crashes. in the logs refers to the line SQLiteDatabase database = dbHelper.getWritableDatabase();
public class Cold extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
DBHelper dbHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cold);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
SwipeMenuListView listView = (SwipeMenuListView) findViewById(R.id.listView);
List<ListData> cold = new ArrayList<>();
cold.add(new ListData("Филадельфия Classic ", 169, R.drawable.filadelfia));
cold.add(new ListData("Радуга", 169, R.drawable.raduga));
cold.add(new ListData("Двойной удар", 209, R.drawable.dvoi));
cold.add(new ListData("Чикен хот", 209, R.drawable.chikenhot));
cold.add(new ListData("Лава", 219, R.drawable.lava));
cold.add(new ListData("Филадельфия премиум", 240, R.drawable.filadpro));
cold.add(new ListData("Сливочный с лососем", 169, R.drawable.slivslos));
cold.add(new ListData("Сайман", 240, R.drawable.saiman));
cold.add(new ListData("Вегетарианский",149 , R.drawable.veg));
cold.add(new ListData("Ясай ролл", 159, R.drawable.yasai));
cold.add(new ListData("Калифорния", 169, R.drawable.california));
cold.add(new ListData("Филадельфия в икре", 189, R.drawable.vikrefill));
cold.add(new ListData("Чикен ролл", 209, R.drawable.chikenroll));
cold.add(new ListData("Калифорния чиз", 209, R.drawable.californiachis));
cold.add(new ListData("Калифорния с креветкой", 209, R.drawable.californiaskrev));
cold.add(new ListData("Алькапоне", 210, R.drawable.alkap));
cold.add(new ListData("Джаз блюз", 210, R.drawable.jazbluz));
cold.add(new ListData("Чука", 210, R.drawable.chuka));
cold.add(new ListData("Киота", 220, R.drawable.kiota));
cold.add(new ListData("Дакота", 214, R.drawable.dakota));
cold.add(new ListData("Вулкан", 219, R.drawable.vulkan));
cold.add(new ListData("Спайси Эби", 220, R.drawable.spaisiebi));
cold.add(new ListData("Вендетта", 230, R.drawable.vendeta));
cold.add(new ListData("Окинава", 220, R.drawable.okinava));
cold.add(new ListData("Угорь с грибам", 230, R.drawable.ugorsgribami));
cold.add(new ListData("Ролл акори", 239, R.drawable.rollakori));
cold.add(new ListData("Филадельфия роял", 240, R.drawable.fillroial));
cold.add(new ListData("Крем чиз", 240, R.drawable.crem));
cold.add(new ListData("Грин ролл", 179, R.drawable.green));
cold.add(new ListData("Бонито", 189, R.drawable.bonito));
cold.add(new ListData("Острый с тунцом", 199, R.drawable.ostristuncom));
cold.add(new ListData("Цезарь маки", 209, R.drawable.cezarmakki));
final CustomArrayAdapter adapter = new CustomArrayAdapter(this, R.layout.item, cold);
listView.setAdapter(adapter);
SwipeMenuCreator creator = new SwipeMenuCreator() {
@Override
public void create(SwipeMenu menu) {
//create an action that will be showed on swiping an item in the list
SwipeMenuItem item1 = new SwipeMenuItem(
getApplicationContext());
item1.setBackground(new ColorDrawable(Color.DKGRAY));
// set width of an option (px)
item1.setWidth(dp2px(60));
item1.setIcon(R.drawable.ic_action_corz);
item1.setTitleSize(18);
item1.setTitleColor(Color.WHITE);
menu.addMenuItem(item1);
SwipeMenuItem item2 = new SwipeMenuItem(
getApplicationContext());
// set item background
item2.setBackground(new ColorDrawable(Color.RED));
item2.setWidth(dp2px(60));
item2.setIcon(R.drawable.ic_action_like);
item2.setTitleSize(18);
item2.setTitleColor(Color.WHITE);
menu.addMenuItem(item2);
SwipeMenuItem item3 = new SwipeMenuItem(
getApplicationContext());
// set item background
item3.setBackground(new ColorDrawable(Color.YELLOW));
item3.setWidth(dp2px(60));
item3.setIcon(R.drawable.ic_action_like);
item3.setTitleSize(18);
item3.setTitleColor(Color.WHITE);
menu.addMenuItem(item3);
}
};
//set MenuCreator
listView.setMenuCreator(creator);
// set SwipeListener
listView.setOnSwipeListener(new SwipeMenuListView.OnSwipeListener() {
@Override
public void onSwipeStart(int position) {
// swipe start
}
@Override
public void onSwipeEnd(int position) {
// swipe end
}
});
listView.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(int position, SwipeMenu menu, int index) {
String value3 = adapter.getItem(position).title;
int value2 = adapter.getItem(position).price;
final SQLiteDatabase database = dbHelper.getWritableDatabase();
final ContentValues contentValues = new ContentValues();
switch (index) {
case 0:
contentValues.put(DBHelper.KEY_NAME, value3);
contentValues.put(DBHelper.KEY_PRICE, value2);
database.insert(DBHelper.TABLE_IZBRANNOE, null, contentValues);;
Toast.makeText(getApplicationContext(), "Action 2 for " + value3 +"__"+"цена"+"__"+ value2, Toast.LENGTH_SHORT ).show();
break;
case 1:
Cursor cursor = database.query(DBHelper.TABLE_IZBRANNOE, null, null, null, null, null, null);
if (cursor.moveToFirst()) {
int idIndex = cursor.getColumnIndex(DBHelper.KEY_ID);
int nameIndex = cursor.getColumnIndex(DBHelper.KEY_NAME);
int priceIndex = cursor.getColumnIndex(DBHelper.KEY_PRICE);
do {
Log.d("mLog", "ID = " + cursor.getInt(idIndex) +
", name = " + cursor.getString(nameIndex) +
", price = " + cursor.getString(priceIndex));
} while (cursor.moveToNext());
} else
Log.d("mLog","0 rows");
cursor.close();
break;
case 2:
database.delete(DBHelper.TABLE_IZBRANNOE, null, null);
break;
}
return false;
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_camera) {
// Handle the camera action
} else if (id == R.id.nav_gallery) {
} else if (id == R.id.nav_slideshow) {
} else if (id == R.id.nav_manage) {
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
private int dp2px(int dp) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
getResources().getDisplayMetrics());
}
}
public class DBHelper extends SQLiteOpenHelper{
public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "contactDb";
public static final String TABLE_IZBRANNOE = "contacts";
public static final String KEY_ID = "_id";
public static final String KEY_NAME = "name";
public static final String KEY_PRICE= "price";
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table " + TABLE_IZBRANNOE + "(" + KEY_ID
+ " integer primary key on conflict replace," + KEY_NAME + " text unique on conflict ignore," + KEY_PRICE + " text" + ")");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("drop table if exists " + TABLE_IZBRANNOE);
onCreate(db);
}
}
Answer the question
In order to leave comments, you need to log in
You have not initialized Helper. Here are my methods:
// открыть подключение
public void open() {
mDBHelper = new DBHelper(mCtx, DB_NAME, null, DB_VERSION);
mDB = mDBHelper.getWritableDatabase();
}
public boolean isOpen() {
return mDB.isOpen();
}
// закрыть подключение
public void close() {
if (mDBHelper != null)
mDBHelper.close();
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question