lundi 26 octobre 2015

How to retrieve the next value in sqlite android

Hi I have an app that display the name and gender of the students. In my query, I limit the display to 20. How will I apply the display of 21-40 by clicking the next button. Also it will go back if i click previous.

MainActivity class

public class MainActivity extends Activity {

    List<Students> GetAll;
    DatabaseHelper db = new DatabaseHelper(this);
    ListView lv;
    Context context = this;
    DatabaseHelper dbhelper;
    Button btnprevious,btnnext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        dbhelper = new DatabaseHelper(MainActivity.this);
        //Add below lines to your original code
        try{
            dbhelper.createDataBase();
        }
        catch(IOException e){
            e.printStackTrace();
        }
        try {
            dbhelper.openDataBase();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        //Till here
        GetAll = dbhelper.getAll();
        lv = (ListView) findViewById(R.id.list);
        lv.setAdapter(new ViewAdapter());
    }



    public class ViewAdapter extends BaseAdapter {

        LayoutInflater mInflater;

        public ViewAdapter() {
            mInflater = LayoutInflater.from(context);
        }

        @Override
        public int getCount() {
            return GetAll.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {

            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.list_item,null);
            }

            final TextView names = (TextView) convertView.findViewById(R.id.doctorlist_name);
            final TextView gender = (TextView) convertView.findViewById(R.id.doctorlist_gender);

            names.setText(GetAll.get(position).getname());
            gender.setText(GetAll.get(position).getgender());

            return convertView;
        }
    }
}

DatabaseHelper class

public List<Students> getAll() {

        final int maxCount = 20;

        List<Students> sList = new ArrayList<Students>();
        {
            String selectQuery =
                    "SELECT id,full_name,gender FROM students LIMIT " +maxCount+" ";
            Log.e("students query: ", selectQuery);
            SQLiteDatabase db = this.getWritableDatabase();
            Cursor cursor = db.rawQuery(selectQuery, null);

            // looping through all rows and adding to list
            if (cursor.moveToFirst()) {
                do {
                    Students si = new Students();
                    si.setid(Integer.parseInt(cursor.getString(0)));
                    si.setname(cursor.getString(1));
                    si.setgender(cursor.getString(2));

                    sList.add(si);
                } while (cursor.moveToNext());
            }
            db.close();
        }
        return sList;
    }

Aucun commentaire:

Enregistrer un commentaire