Tolen
Tolen

Reputation: 111

Sqlite auto-increment column issue

I have written a code that has a next button that display the next 3 items that stored in my database in a custom list-view . I have created a column called id which is an auto-increment column. but when I am trying to print out the value of id column in my log cat it just displays a zero. what is wrong with that?

here is my database:

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 )  ");
    }

and here where I have tried to print id value:

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(); 

and here is my function to get all data:

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

Upvotes: 1

Views: 176

Answers (1)

nano_nano
nano_nano

Reputation: 12523

the problem is you dont call setId for your business object: videoObject.

add this line in your for-loop:

        videoObject.setId(cursor.getInteger(7));

Upvotes: 2

Related Questions