我编写了一个代码,该代码具有一个下一步按钮,该按钮以自定义 list-view 显示存储在我的数据库中的接下来的 3 个项目。我创建了一个名为 id 的列,它是一个自动增量列。但是当我试图在我的日志猫中打印出 id 列的值时,它只显示一个零。那有什么问题?
这是我的数据库:
public class youtube_db extends SQLiteOpenHelper {
public static final String dataBase_NAME="YOUTUBE_database";
private static final int dataBase_VERSION=1;
public static final String dataBase_TABLE="youtube_VIDEOS";
public static final String[] COLS_List={"video_Name","video_Descrption","video_Img","video_Url","video_CountView","video_LIKES","video_CommentCount","id" };
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//end of declaring attributes and tables conents
public youtube_db(Context context) {
super(context,dataBase_NAME, null, dataBase_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(
" create table " + dataBase_TABLE + "(" + COLS_List[0] +" text not null , "+ COLS_List[1]
+" text not null , "+ COLS_List[2]+" text not null , "+COLS_List[3]+" text not null , "+COLS_List[4]+" integer , "+COLS_List[5]
+" integer , "+COLS_List[6]+" integer ,"+COLS_List[7]+" integer primary key autoincrement ) ");
}
在这里我尝试打印 id 值:
ArrayList<videos> getAllvideosList=new ArrayList<videos>();
getAllvideosList=connection.getAllData();
connection.close();
for(int i=0;i<getAllvideosList.size();i++){
videos videoObject=new videos();
videoObject.setVideoname(getAllvideosList.get(i).getVideoname());
videoObject.setDecscrption(getAllvideosList.get(i).getDecscrption());
videoObject.setImageurl(getAllvideosList.get(i).getImageurl());
videoObject.setVediourl(getAllvideosList.get(i).getVediourl());
Log.d("try to print id ", String.valueOf(getAllvideosList.get(i).getId()));
vlist.add(videoObject);
lview.setAdapter(new custome(MainActivity.this,vlist));
ad.notifyDataSetChanged();
这是我获取所有数据的功能:
public ArrayList<videos> getAllData(){
ArrayList<videos> videoArray=new ArrayList<videos>();
Cursor cursor= SQL_db.query(my_Database.dataBase_TABLE, my_Database.COLS_List, null, null, null, null, null);
for(cursor.moveToFirst();!cursor.isAfterLast();cursor.moveToNext()){
videos videoObject=new videos();
videoObject.setVideoname(cursor.getString(0));
videoObject.setDecscrption(cursor.getString(1));
videoObject.setImageurl(cursor.getString(2));
videoObject.setVediourl(cursor.getString(3));
videoArray.add(videoObject);
Log.d("getAlLvideos", "ok.... ");
}
cursor.close();
Log.d("after close cursor", "ok.... ");
return videoArray;
}//end of function get all data