【问题标题】:How to put data from sqllite into a listview?如何将 sqlite 中的数据放入列表视图?
【发布时间】:2021-01-17 19:37:58
【问题描述】:

我有一个应用程序,用户可以在其中输入三个输入(名称、数量和类型)我希望输入进入数据库,然后当用户单击状态按钮时,他们可以查看所有用户输入列表视图。在我的情况下,当我运行应用程序时,项目会添加到数据库中,但是当我单击以在库存状态活动中查看它们时,应用程序会退出。有谁知道我哪里出错了或者我错过了什么?抱歉,这可能是一个愚蠢的问题。

DatabaseHelperUser 类:

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 = "Name";
public static final String COL2 = "Quantity";
public static final String COL3 = "Type";


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


@Override
public void onCreate(SQLiteDatabase db) {

    db.execSQL("create table " + TABLE_NAME + " (Name TEXT, Quantity Text, Type Text)");

}

@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(COL1, Name);
    contentValues.put(COL2, Quantity);
    contentValues.put(COL3, Type);
    long result = db.insert(TABLE_NAME, null, contentValues);

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

}
public Cursor getAllData(){
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor data = db.rawQuery("select * from " + TABLE_NAME, null);
    return data;
}
public String deletedata(){
    SQLiteDatabase myDB = this.getWritableDatabase();
    myDB.delete(TABLE_NAME, null, null);
    myDB.close();

    return null;
}
}

添加项目类:

public class AddItem extends AppCompatActivity {

DatabaseHelper myDB;
EditText etName, etQuantity, etType;
Button btAdd2, btStatus2;


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

    myDB = new DatabaseHelper(this);

    final EditText etName = findViewById(R.id.editTextName);
    final EditText etQuantity = findViewById(R.id.editTextQuantity);
    final RadioButton rbBiscuit = findViewById(R.id.radioButtonBiscuit);
    final RadioButton rbCookie = findViewById(R.id.radioButtonCookie);
    final RadioButton rbCake = findViewById(R.id.radioButtonCake);
    final RadioButton rbIngredient = findViewById(R.id.radioButtonIngredient);
    final RadioButton rbOther = findViewById(R.id.radioButtonOther);
    Button btAdd2 = findViewById(R.id.buttonAdd2);
    final Button btStatus2 = findViewById(R.id.buttonStatus2);

    final EditText etType = findViewById(R.id.editTextType1);


    btAdd2.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {

            String edtName = etName.getText().toString();
            String edtQuantity = etQuantity.getText().toString();
            String edtType = etType.getText().toString();

            if (etName.length() !=0 || etQuantity.length() !=0 || etType.length() !=0){
                // myDB.insertData(edtName, edtQuantity, edtType);
                AddData(edtName, edtQuantity, edtType);
                etName.setText("");
                etQuantity.setText("");
                etType.setText("");

            }
            else{
                Toast.makeText(AddItem.this, "Fill in all of the fields", Toast.LENGTH_SHORT).show();
            }


        }
        public void AddData(String edtName, String edtQuantity, String edtType){
            boolean insert = myDB.insertData(edtName, edtQuantity, edtType);
            if(insert){
                Toast.makeText(AddItem.this, "Data Inserted", Toast.LENGTH_SHORT).show();
            }
            else{
                Toast.makeText(AddItem.this, "Error, Data not Inserted", Toast.LENGTH_SHORT).show();
            }
        }

    });

    btStatus2.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
                Intent i = new Intent(AddItem.this, InventoryStatus.class);
                startActivity(i);


        }


    });

 }

}

InventoryStatus 类:

public class InventoryStatus extends AppCompatActivity {
DatabaseHelper myDB;
ListView listView;

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

    ListView listView = findViewById(R.id.listViewStock);
    myDB = new DatabaseHelper(this);

    populateListView();

}
public void populateListView(){
    Cursor data = myDB.getAllData();
    ArrayList<String> list = new ArrayList<>();
    while(data.moveToNext()){
        list.add(data.getString(1));
    }
    ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1);
    listView.setAdapter(adapter);
}

}

【问题讨论】:

  • 你的日志在哪里?
  • 对不起@PratikButani 什么是日志?

标签: sqlite android-studio android-listview


【解决方案1】:

您忘记将列表传递给适配器构造函数。

    ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, **list**);

并尝试在 InventoryStatus.class 中将列表视图类型转换为:

ListView listView = (ListView) findViewById(R.id.listViewStock);

【讨论】:

  • 我将列表添加到适配器构造函数中,但仍然给我与注销应用程序相同的结果
  • 在类型转换中添加,仍然出现同样的错误!我不知道还能做些什么来解决这个问题!
  • 将您的项目上传到 github/google 驱动器并分享链接。
猜你喜欢
  • 1970-01-01
  • 2011-02-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多