【问题标题】:How to retrieve the next value in sqlite android如何在sqlite android中检索下一个值
【发布时间】:2016-01-26 09:41:45
【问题描述】:

您好,我有一个应用程序可以显示学生的姓名和性别。在我的查询中,我将显示限制为 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;
    }

【问题讨论】:

    标签: android sqlite android-listview android-arrayadapter


    【解决方案1】:

    在下一个按钮单击时将索引增加 20,在上一个按钮单击时将索引减少到 20,您可以通过以下查询实现。

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

    编辑:

    public List<Students> getStudent(int index) {
            final int maxCount = 20;
            List<Students> sList = new ArrayList<Students>();{
                String selectQuery = "SELECT id,full_name,gender FROM students LIMIT "+maxCount+", "+index;;
                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;
    }
    
    
    
    private int currentPageIndex=0; 
    @Override
    public void onClick(View view) {
         switch(view.getId()){
              case R.id.prevBtn;
                    currentPageIndex -=20;
                    GetStudent.clear();
                    GetStudent.addAll(dbhelper.getStudent(int index));
                    adapter.notifyDataSetChanged();
            break;
              case R.id.nextBtn;
                    currentPageIndex +=20;
                    GetStudent.clear();
                    GetStudent.addAll(dbhelper.getStudent(int index));
                    adapter.notifyDataSetChanged();
            break;
         }
    }
    

    【讨论】:

    • 我将在哪里将其插入到我的 dbhelper 类或我的主要活动中?对不起 android 新手
    • @GemUbaldo 检查更新的代码。使用 getStudent() 方法检索学生。传递您需要从中获取学生的索引。正如我之前所说,在下一个按钮单击时将索引增加 20,在上一个按钮单击时将索引减少到 20。
    • 我在这一行的主要活动中遇到错误 GetStudent = dbhelper.getStudent(int index);
    • dbhelper = new DatabaseHelper(MainActivity.this); //在原始代码中添加以下行 try{ dbhelper.createDataBase(); } catch(IOException e){ e.printStackTrace(); } 尝试 { dbhelper.openDataBase(); } catch (SQLException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } //直到这里 GetStudent = dbhelper.getStudent(int index); lv = (ListView) findViewById(R.id.list); lv.setAdapter(new ViewAdapter()); }
    • 我替换 GetAll = dbhelper.getAll(); with GetStudent = dbhelper.getStudent(int index);
    【解决方案2】:

    您可以更改 whereCluse: 例如下一页:(page = 2) id> 20*(page - 1) 和 LIMIT 20*(page)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-04-20
      • 2014-04-03
      • 2018-04-18
      • 1970-01-01
      • 2016-01-28
      • 2017-10-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多