我是Android编程的新手,我想为产品创建数据库。我按照教程,但仍然有一些错误,但我修复了它们,但这是我迄今为止最大的问题。当我输入数据时,它只是说"数据添加成功!"但我的列表适配器中没有任何内容。
这是我的databaseHandler
private static final String prodID = "ID ";
private static final String PRODNAME = "ProdName";
private static final String CATEGORY = "Category";
public DatabaseHANDLER(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String createTable = "create table " + TABLE_NAME + " ( " + prodID + " String primary key," + PRODNAME + " text not null,"
+ CATEGORY + " text not null);";
db.execSQL(createTable);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
public boolean NewData (String PROD_ID, String prodNAME,String Category) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(prodID, PROD_ID);
values.put(PRODNAME, prodNAME);
values.put(CATEGORY, Category);
long result = db.insert(TABLE_NAME, null, values);
if (result == -1) {
return false;
} else {
return true;
}
}
public Cursor getListContents() {
SQLiteDatabase db = this.getWritableDatabase();
return db.rawQuery("SELECT * FROM " + TABLE_NAME, null);
}
我想要添加新数据
prodID = findViewById(R.id.editText5);
PROD_NAME = findViewById(R.id.editText4);
CATEGORY = findViewById(R.id.editText3);
btnAdd = findViewById(R.id.btnAdd) ;
db = new DatabaseHANDLER(this);
Button btnAdd = findViewById(R.id.btnAdd);
btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String product_ID = prodID.getText().toString();
String prod_name = PROD_NAME.getText().toString();
String category = CATEGORY.getText().toString();
if(prod_name.length() != 0 && product_ID.length() != 0 && category.length() != 0){
NewData(product_ID,prod_name, category);
prodID.setText(" ");
PROD_NAME.setText(" ");
CATEGORY.setText(" ");
}
else {
Toast.makeText(NewData.this, "Fill the forms completely!",Toast.LENGTH_LONG)
.show();
}
}
});
}
public void NewData(String PROD_ID, String prodNAME,String CATEGORY){
boolean insertData = db.NewData(PROD_ID, prodNAME, CATEGORY);
if(insertData == true) {
Toast.makeText(NewData.this, "Data inserted Successfully!", Toast.LENGTH_LONG).show();
}
else {
Toast.makeText(NewData.this,"Too bad, something went wrong :( ", Toast.LENGTH_LONG).show();
}
}
}
这是我的列表适配器
private LayoutInflater mInflater;
private ArrayList<Products> products;
private int mViewResourceId;
public ThreeColumn_ListAdapter(Context context, int textViewResourceId, ArrayList<Products> products) {
super(context, textViewResourceId, products);
this.products = products;
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mViewResourceId = textViewResourceId;
Products products1;
}
public View getView(int position, View convertView, ViewGroup parent) {
convertView = mInflater.inflate(R.layout.view_data,parent,false);
Products products1 = products.get(position);
if (products != null) {
TextView ProdID = (TextView) convertView.findViewById(R.id.textProdID);
TextView ProdName = (TextView) convertView.findViewById(R.id.textProdName);
TextView Category = (TextView) convertView.findViewById(R.id.textCategory);
if (ProdID != null) {
ProdID.setText(products1.getProdID());
}
if (ProdName != null) {
ProdName.setText(products1.getProdNAME());
}
if (Category != null) {
Category.setText((products1.getCategory()));
}
}
return convertView;
}
这是我的视图数据类
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view_data);
databaseHANDLER = new DatabaseHANDLER(this);
ProductList = new ArrayList<>();
Cursor data = databaseHANDLER.getListContents();
int numrows = data.getCount();
if (data.getCount() == 0){
Toast.makeText(viewdata.this, "There is nothing in this database!",Toast.LENGTH_LONG).show();
}
else{
int i = 0;
while (data.moveToNext()){
products = new Products(data.getString(0),data.getString(1),data.getString(2));
ProductList.add(i,products);
}
ListAdapter adapter = new ArrayAdapter<>(this, R.layout.view_data,ProductList);
listView = (ListView) findViewById(R.id.list_view);
listView.setAdapter(adapter);
}
}
感谢您的帮助:)