如何从 sqlite 数据库添加到 for 循环

Posted

技术标签:

【中文标题】如何从 sqlite 数据库添加到 for 循环【英文标题】:How to add from sqlite database into a for loop 【发布时间】:2021-01-26 06:05:44 【问题描述】:

所以我试图从我的数据库中从 sqlite 获取我的 3 个列,然后将其放入我的数组列表中,然后显示到一个列表视图中,但目前它只显示数量。例如: 名称:1 数量:1 类型:1 但我希望它显示所有其他列而不仅仅是数量,因此输出实际上是:名称:牛奶数量:1 类型:成分 有谁知道怎么做?

InventoryStatus 类:

public class InventoryStatus extends AppCompatActivity 
DatabaseHelper myDB;
int currentPosition = 0;

private ArrayList<String> fullList;
private ArrayList<String> displayedList;
ArrayAdapter<String> listAdapter;
ListView listView;

@Override
protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_inventory_status);

    listView = findViewById(R.id.listViewStock);
    Button btNext = findViewById(R.id.buttonNext);
    Button btPrevious = findViewById(R.id.buttonPrevious);

    myDB = new DatabaseHelper(this);
    Cursor data = myDB.getAllData();

    fullList = new ArrayList<>();
    displayedList = new ArrayList<>();

    for (int i = 0; i < 5; i++)
    
        fullList.add("Name: " + i + "Quantity: " + i + "Type: " + i);
    

    if (fullList.size() == 0)
    
        Toast.makeText(this, "No data found", Toast.LENGTH_SHORT).show();
    
    else // fullList isn't empty show first 5 items in ListView
    
        setDisplayListPosition();
        listView.setAdapter(listAdapter);
    

    btNext.setOnClickListener(new View.OnClickListener() 
        public void onClick(View view) // change current position and refresh ListView
        
            currentPosition += 5;
            if (currentPosition > fullList.size()) currentPosition -= 5;
            else
                setDisplayListPosition();
        
    );

    btPrevious.setOnClickListener(new View.OnClickListener() // change current position and refresh ListView
    
        public void onClick(View view) 
            currentPosition -= 5;
            if (currentPosition < 0) currentPosition = 0;
            else
                setDisplayListPosition();
        
    );



private void setDisplayListPosition() // helper function which refresh ListView based on `currentPos`

    displayedList.clear();

    for (int i = 0; i < 5 && i + currentPosition < fullList.size(); i++) 
        displayedList.add(fullList.get(currentPosition + i)); // ad 5 items from finalList to displayedList
    

    listAdapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1,
            displayedList
    );



DatabaseHelper 类:

public class DatabaseHelper extends SQLiteOpenHelper 

public static final String DATABASE_NAME = "Inventory.db";
public static final String TABLE_NAME = "Inventory_table";
public static final String COL1 = "ID";
public static final String COL2 = "Name";
public static final String COL3 = "Quantity";
public static final String COL4 = "Type";


public  DatabaseHelper(Context context)
    super(context, DATABASE_NAME, null, 1);



@Override
public void onCreate(SQLiteDatabase db) 

    String createTable = "create table " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT, " + " Name TEXT, Quantity Text, Type Text)";
    db.execSQL(createTable);



@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) 

    db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
    onCreate(db);

public boolean insertData(String Name, String Quantity, String Type)
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(COL2, Name);
    contentValues.put(COL3, Quantity);
    contentValues.put(COL4, Type);
    long result = db.insert(TABLE_NAME, null, contentValues);

    if(result == -1)
        return false;
    else
        return true;



// Gets all data
public Cursor getAllData()
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor data = db.rawQuery("select * from " + TABLE_NAME, null);
    return data;


// Deletes all data
public String deletedata()
    SQLiteDatabase myDB = this.getWritableDatabase();
    myDB.delete(TABLE_NAME, null, null);
    myDB.close();

    return null;


【问题讨论】:

宿主语言是什么?爪哇?可能适用于安卓系统? Edit问题并添加适当的标签。 是的,它在 android studio 中的 java 对不起@stickybit 【参考方案1】:

改变这个:

 for (int i = 0; i < 5; i++)
    
        fullList.add("Name: " + i + "Quantity: " + i + "Type: " + i);
    

到:

if(cursor.moveToFirst())
    for(int i=0; i<5 ;i++)   
          fullList.add("Name: " + cursor.getString(1) + "Quantity: " + cursor.getString(2) + "Type: " + cursor.getString(3));
          if(!cursor.moveToNext())
               break;
   

【讨论】:

嗨@Anasxon 我试过你的代码,但现在当我进入这个活动时,应用程序就会关闭。你知道有什么其他方法可以解决这个问题吗? @HarryBrown 我也试过这段代码,但我没有这样的问题。也许其他地方有错误

以上是关于如何从 sqlite 数据库添加到 for 循环的主要内容,如果未能解决你的问题,请参考以下文章

如何使用光标和循环显示来自 sqlite 的片段的 recyclerview

我需要将数据从 Sqlite 数据库发送到服务器

for循环遍历查找数据与sqlite数据库查找数据性能问题

TypeError:“sqlite3.Cursor”对象不可下标。如何在没有for循环的情况下打印sql选择数据[重复]

Kotlin基础从入门到进阶系列讲解(入门篇)下载SQLite软件,如何创建/添加/更新/查询/删除数据

Kotlin基础从入门到进阶系列讲解(入门篇)下载SQLite软件,如何创建/添加/更新/查询/删除数据