我的問題很簡單。我的應用程序可以將數據添加到sqlite數據庫,但現在我的工作ListView代碼顯示我的數據在我的應用程序。如果我使用下面的代碼,如果我使用ListView代碼,我可以顯示數據庫中的數據嗎?我使用SQLiteDatabase db;而不是DbAdapter db;。我只是想知道是否有可能使用ListView代碼顯示我的數據從sqlite數據庫。以ListView形式顯示數據android studio sqlite
public class SalesActivity extends AppCompatActivity implements View.OnClickListener{
TextView editDescription;
EditText editDate,editTime,editBarcode,editQuantity,editRetail,editAmount;
Button btnView,btnAdd;
SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sales);
editDate = (EditText)findViewById(R.id.editDate);
editTime = (EditText)findViewById(R.id.editTime);
editBarcode = (EditText)findViewById(R.id.editBarcode);
editDescription = (TextView) findViewById(R.id.editDescription);
editRetail = (EditText) findViewById(R.id.editRetail);
editQuantity = (EditText)findViewById(R.id.editQuantity);
editAmount = (EditText)findViewById(R.id.editAmount);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat stf = new SimpleDateFormat("HH:mm:ss a");
editDate.setText(sdf.format(new Date()));
editTime.setText(stf.format(new Date()));
btnView = (Button)findViewById(R.id.btnView);
btnView.setOnClickListener(this);
btnAdd = (Button)findViewById(R.id.btnAdd);
btnAdd.setOnClickListener(this);
db = openOrCreateDatabase("ProductDB", Context.MODE_PRIVATE,null);
db.execSQL("CREATE TABLE IF NOT EXISTS product2(id INTEGER PRIMARY KEY AUTOINCREMENT, date VARCHAR, time VARCHAR, barcode VARCHAR, description VARCHAR, quantity VARCHAR, retail VARCHAR, amount VARCHAR)");
}
public void onClick(View view){
if(view==btnView){
if(editBarcode.getText().toString().trim().length()==0){
showMessage("","Please insert Barcode");
return;
}
else{
Cursor c = db.rawQuery("select * from product where " +
"barcode ='"+editBarcode.getText()+"'",null);
if(c.moveToFirst()){
editDescription.setText(c.getString(1));
editRetail.setText(c.getString(2));
}
}
}
if(view==btnAdd){
if(editDate.getText().toString().trim().length()==0 ||
editTime.getText().toString().trim().length()==0 ||
editBarcode.getText().toString().trim().length()==0 ||
editDescription.getText().toString().trim().length()==0 ||
editQuantity.getText().toString().trim().length()==0 ||
editRetail.getText().toString().trim().length()==0 ||
editAmount.getText().toString().trim().length()==0
){
showMessage("","Please insert all fields");
return;
}
else{
db.execSQL("INSERT INTO product2(date,time,barcode,description,quantity,retail,amount)"+
"VALUES('"+editDate.getText()+
"','"+editTime.getText()+
"','"+editBarcode.getText()+
"','"+editDescription.getText()+
"','"+editQuantity.getText()+
"','"+editRetail.getText()+
"','"+editAmount.getText()+"')");
showMessage("","Record added");
clearText();
}
}
}
public void clearText(){
editDate.setText("");
editTime.setText("");
editBarcode.setText("");
editDescription.setText("");
editQuantity.setText("");
editRetail.setText("");
editAmount.setText("");
editBarcode.requestFocus();
}
public void showMessage(String title,String message){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(message);
builder.show();
}
}
使用'CursorAdapter',就像['SimpleCursorAdapter'](https://developer.android.com/reference/android/widget/SimpleCursorAdapter.html)一樣。 –
[在android中顯示數據從sqlite數據庫中顯示數據]可能的副本(https://stackoverflow.com/questions/30369253/show-data-in-listview-from-sqlite-database-in-android) – Sufian