在android中插入和检索数据库(陷入无限循环)



问题是我只插入了两行,但在执行时,它进入了无限循环,并重复显示相同的数据。。任何形式的帮助都将不胜感激这是我的密码。

这是我的数据库类(即DbAdapter)

public Cursor getAllRows() {
                String[]columns = new String[]{ KEY_MODEL,KEY_BRAND, KEY_PRICE};
                Cursor c = db.query(TABLE_LAPTOPS, columns, null, null, null, null, null);
                if (c != null) {
                    c.moveToLast();
                }
                return c;
}

这是我的主要类别(即笔记本电脑)

DbAdapter obj = new DbAdapter(this);
obj.open();
obj.insertRow("Dell1", "Dell", 345);
obj.insertRow("Hp1", "HP", 546);
obj.close();  

obj.open();
            Cursor c1 = obj.getAllRows();
            try{
                  if(c1 !=  null){
                  ListView empListView = (ListView)findViewById(R.id.listView1);
                  ArrayList<String> data1 = new ArrayList<String>(); 
                  for(c1.moveToFirst();!c1.isAfterLast();c1.moveToNext())
                        {
                            Toast.makeText(getApplicationContext(), c1.getString(0), Toast.LENGTH_LONG).show();
                        }
                        //empListView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data1));
            }}catch(SQLException e){
                e.printStackTrace();
            }
            obj.close();
c.moveToLast();

这是的问题

c.moveToFirst();

试试这个。。

尝试使用Like This

listView = (ListView) rootView.findViewById(R.id.Assignment_VM_List);

AssignmentDatabaseConnector dao = new AssignmentDatabaseConnector(getActivity());
            List<AssignmentPojo> assignments = dao.getAllAssignmentList();
 AssignmentCustomAdapter assignmentCustomAdapter = new AssignmentCustomAdapter(getActivity(),
                    assignments, this);
            listView.setAdapter(assignmentCustomAdapter);

工作分配数据库连接器:

public List<AssignmentPojo> getAllAssignmentList() {
        open();
        List<AssignmentPojo> assignmentList = new ArrayList<AssignmentPojo>();
        // Select All Query
        String selectQuery = "SELECT  * FROM Assignment";
        Cursor cursor = database.rawQuery(selectQuery, null);
        // looping through all rows and adding to list
        if (cursor.moveToFirst()) { // ##Your Main Issue is Here ##
            do {
                AssignmentPojo assignment = new AssignmentPojo();
                assignment.setAssign_Id(cursor.getString(0));
                assignment.setAssign_Course(cursor.getString(1));
                
                // Adding contact to list
                assignmentList.add(assignment);
            } while (cursor.moveToNext());
        }
        cursor.close();
        database.close();
        // return contact list
        return assignmentList;
    }

这只是一个示例代码,您可以根据自己的需求使用此代码

最新更新