如何检索sqlite android中的下一个值

时间:2015-10-27 04:51:10

标签: android sqlite android-listview android-arrayadapter

您好我有一个应用程序,显示学生的姓名和性别。在我的查询中,我将显示限制为20.如何通过单击下一步按钮来应用21-40的显示。如果我点击上一个,它也会回来。

  

MainActivity类

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类

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

2 个答案:

答案 0 :(得分:3)

在下一个按钮上单击增加索引20和上一个按钮单击将索引减少到20,您可以使用以下查询来实现。

X

编辑:

String selectQuery = "SELECT id,full_name,gender FROM students LIMIT 20, "+index;

答案 1 :(得分:0)

您可以更改whereCluse: 例如下一页:( page = 2) ID&GT; 20 *(第1页)和LIMIT 20 *(页)