1
我使用Sqlite
数据库Recyclerview
和Cardview
一起显示Recyclerview
。我的数据Recyclerview
正显示出数据,但是,它没有显示在一个序列,它是行为异常,我增加了6个项目,但正显示出18项与重复的名称和每item.What经过大量的空间是issue.Here是我的代码..RecyclervIew不显示SQLite数据库的数据正确
databasehelper.java
public class databasehandler extends SQLiteOpenHelper {
// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "Foodmanager";
// Food table name
private static final String TABLE_FOOD = "Food";
private static databasehandler dbhelper;
// Food Table Columns names
private static final String KEY_ID= "id";
private static final String FOOD_CATEGORY = "Food_category";
private static final String FOOD_ITEM = "Food_item";
private static final String PROTEINS = "Proteins";
private static final String FAT = "Fat";
private static final String CARB = "carb";
private static final String CALORIES = "calories";
public static synchronized databasehandler getInstance(Context context) {
// Use the application context, which will ensure that you
// don't accidentally leak an Activity's context.
if (dbhelper == null) {
dbhelper = new databasehandler(context.getApplicationContext());
}
return dbhelper;
}
public databasehandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
SQLiteDatabase db=this.getWritableDatabase();
}
// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_FOOD_TABLE = "CREATE TABLE " + TABLE_FOOD +" (id INTEGER PRIMARY KEY,Food_category TEXT,Food_item TEXT,Proteins REAL,Fat REAL,carb REAL,calories REAL)";
db.execSQL(CREATE_FOOD_TABLE);
}
// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_FOOD);
// Create tables again
onCreate(db);
}
/**,
* All CRUD(Create, Read, Update, Delete) Operations
*/
// Adding new contact
public void addfood(food_items food_item) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(FOOD_CATEGORY, food_item.getfood_category());
values.put(FOOD_ITEM, food_item.getfood_item());
values.put(PROTEINS, food_item.getproteins());
values.put(FAT, food_item.getfat());
values.put(CARB, food_item.getcarb());
values.put(CALORIES, food_item.getcalorie());
db.insert(TABLE_FOOD, null, values);
db.close(); // Closing database connection
}
// Getting single food item
public food_items getfood_item(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_FOOD, new String[] { FOOD_CATEGORY,
FOOD_ITEM, PROTEINS,FAT,CARB,CALORIES }, FOOD_CATEGORY + "=?",
new String[] { String.valueOf(id) }, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
assert cursor != null;
food_items food_item = new food_items(cursor.getString(0),
cursor.getString(1), Float.parseFloat(cursor.getString(2)),Float.parseFloat(cursor.getString(3)),
Float.parseFloat(cursor.getString(4)),Float.parseFloat(cursor.getString(5)));
// return contact
return food_item;
}
public String getfood_category(){
String food_category="";
String selectQuery="SELECT FOOD_CATEGORY FROM " + TABLE_FOOD;
SQLiteDatabase db=this.getReadableDatabase();
Cursor cursor=db.rawQuery(selectQuery,null);
if(cursor.moveToFirst()){
do{
food_category=cursor.getString(0);
}
while (cursor.moveToNext());
}
return food_category;
}
public List<food_items> getAllfood_items() {
List<food_items> food_item_list = new ArrayList<>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_FOOD;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
food_items food_item = new food_items();
// food_item.setId(Integer.parseInt(cursor.getString(0)));
food_item.setfood_category(cursor.getString(0));
food_item.setfood_item(cursor.getString(1));
food_item.setproteins(Float.parseFloat(cursor.getString(2)));
food_item.setfat(Float.parseFloat(cursor.getString(3)));
food_item.setcarb(Float.parseFloat(cursor.getString(4)));
food_item.setcalorie(Float.parseFloat(cursor.getString(5)));
food_item_list.add(food_item);
} while (cursor.moveToNext());
}
return food_item_list;
}
// Updating single contact
public int updatefood(food_items food_item) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(FOOD_CATEGORY, food_item.getfood_category());
values.put(FOOD_ITEM, food_item.getfood_item());
values.put(PROTEINS, food_item.getproteins());
values.put(FAT, food_item.getfat());
values.put(CARB, food_item.getcarb());
values.put(CALORIES, food_item.getcalorie());
// updating row
return db.update(TABLE_FOOD, values, FOOD_CATEGORY + " = ?",
new String[] { food_item.getfood_category() });
}
// Deleting single contact
public void deletefood(food_items food_item) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_FOOD, FOOD_CATEGORY + " = ?",
new String[] { food_item.getfood_category() });
db.close();
}
// Getting contacts Count
public int getfoodCount() {
String countQuery = "SELECT * FROM " + TABLE_FOOD;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
cursor.close();
// return count
return cursor.getCount();
}
}
foodactivity.java
public class food_activity extends com.samarth.lightweight.base_Activity {
private RecyclerView recyclerView;
private food_category_adapter adapter;
databasehandler db;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getLayoutInflater().inflate(R.layout.food_category, frameLayout);
db=databasehandler.getInstance(getApplicationContext());
preparedata();
recyclerView=(RecyclerView)findViewById(R.id.recycler_view1);
setTitle("Calories in food");
adapter=new food_category_adapter(this,db.getAllfood_items());
recyclerView.setHasFixedSize(true);
recyclerView.setAdapter(adapter);
// RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
}
public void preparedata() {
db.addfood(new food_items("Dairy and Egg Products","Milk",0f,0f,0f,0f));
db.addfood(new food_items("Spices and Herbs","Milk",0f,0f,0f,0f));
db.addfood(new food_items("Fats and Oils","Milk",0f,0f,0f,0f));
db.addfood(new food_items("Soups, Sauces, and Gravies","Milk",0f,0f,0f,0f));
db.addfood(new food_items("Fruits","Milk",0f,0f,0f,0f));
db.addfood(new food_items("Vegetables","Milk",0f,0f,0f,0f));
}
}
food_Category_adapter
public class food_category_adapter extends
RecyclerView.Adapter<com.samarth.lightweight.database.food_category_adapter.ViewHolder>
{
private static final int TYPE_HEAD=0;
private static final int TYPE_LIST=1;
public String category;
private Context mContext;
private List<food_items> food_items;
public food_category_adapter(Context context,List<food_items> food_itemses) {
mContext=context;
food_items=food_itemses;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if(viewType==TYPE_LIST) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.card, parent, false);
return new ViewHolder(itemView,viewType);
}
else if(viewType==TYPE_HEAD)
{
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.header_recycle_view, parent, false);
return new ViewHolder(itemView,viewType);
}
return null;
}
public class ViewHolder extends RecyclerView.ViewHolder
{
int view_type;
public TextView food_category;
public ImageView food_image;
public ImageView header_image;
public ViewHolder(View view,int viewType) {
super(view);
if(viewType==TYPE_LIST) {
food_category = (TextView) view.findViewById(R.id.food_category);
food_image=(ImageView)view.findViewById(R.id.image);
view_type=1;
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
category=food_category.getText().toString();
Intent intent = new Inten
t(v.getContext(),food_item_description.class);
intent.putExtra("category_food",category);
v.getContext().startActivity(intent);
}
}) ;
}
else if(viewType==TYPE_HEAD)
{
header_image=(ImageView) view.findViewById(R.id.food_main);
view_type=0;
}
}
}
@Override
public void onBindViewHolder(com.samarth.lightweight.database.food_category_adapter.ViewHolder viewHolder, int position)
{
if(viewHolder.view_type==TYPE_LIST){
com.samarth.lightweight.database.food_items food_item=food_items.get(position-1);
viewHolder.food_category.setText(food_item.getfood_category());
}
else if(viewHolder.view_type==TYPE_HEAD)
{
viewHolder.header_image.setImageResource(R.drawable.food_category1);
}
}
@Override
public int getItemCount() {
if(food_items!=null)
return food_items.size()+1;
return 0;
}
@Override
public int getItemViewType(int position)
{
if(position==0)
return TYPE_HEAD;
return TYPE_LIST;
}
}
先生,如果我想在数据库中添加更多项目,我是否必须每次都创建一个新的ContentValues对象,或者是否有其他解决方案? –
@samarthkejriwal尝试更新的答案,您可以创建'values'作为全局变量并在'oncreate'内初始化它意味着在类级别声明'ContentValues values'如'CALORIES'并且执行'values = new ContentValues();'inside' oncreate ',但'static'意味着'values'的数据不会消失,所以如果以后你想使用'values'对象,那么旧值将会出现在 –
谢谢先生。它解决了我的问题。 :) –