用android stdio写一个简单的购物APP 有商家和用户登录后的两个界面 商家界面可以对商品进行修改查看 用户界面可以对商品进行选择 用数据库连接的详细代码
时间: 2024-03-02 13:54:26 浏览: 102
好的,我可以为您提供一个简单的购物APP的代码示例,基于Android Studio和SQLite数据库。
首先,我们需要创建两个活动:商家活动和用户活动。商家活动包括修改和查看商品的功能,用户活动包括选择商品的功能。
商家活动的主要代码如下:
```java
public class MerchantActivity extends AppCompatActivity {
private EditText mNameEditText;
private EditText mPriceEditText;
private Button mAddButton;
private ListView mListView;
private DBHelper mDBHelper;
private SQLiteDatabase mDatabase;
private Cursor mCursor;
private SimpleCursorAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_merchant);
mNameEditText = findViewById(R.id.edit_text_name);
mPriceEditText = findViewById(R.id.edit_text_price);
mAddButton = findViewById(R.id.button_add);
mListView = findViewById(R.id.list_view);
mDBHelper = new DBHelper(this);
mDatabase = mDBHelper.getWritableDatabase();
updateUI();
mAddButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = mNameEditText.getText().toString();
String price = mPriceEditText.getText().toString();
ContentValues values = new ContentValues();
values.put(DBHelper.COLUMN_NAME, name);
values.put(DBHelper.COLUMN_PRICE, price);
mDatabase.insert(DBHelper.TABLE_NAME, null, values);
updateUI();
}
});
}
private void updateUI() {
mCursor = mDatabase.query(DBHelper.TABLE_NAME, null, null, null, null, null, null);
mAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_2, mCursor,
new String[]{DBHelper.COLUMN_NAME, DBHelper.COLUMN_PRICE},
new int[]{android.R.id.text1, android.R.id.text2}, 0);
mListView.setAdapter(mAdapter);
}
@Override
protected void onDestroy() {
super.onDestroy();
mDatabase.close();
mCursor.close();
}
}
```
用户活动的主要代码如下:
```java
public class UserActivity extends AppCompatActivity {
private ListView mListView;
private DBHelper mDBHelper;
private SQLiteDatabase mDatabase;
private Cursor mCursor;
private SimpleCursorAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user);
mListView = findViewById(R.id.list_view);
mDBHelper = new DBHelper(this);
mDatabase = mDBHelper.getReadableDatabase();
updateUI();
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
mCursor.moveToPosition(position);
String name = mCursor.getString(mCursor.getColumnIndex(DBHelper.COLUMN_NAME));
String price = mCursor.getString(mCursor.getColumnIndex(DBHelper.COLUMN_PRICE));
Toast.makeText(UserActivity.this, "You selected " + name + " for " + price, Toast.LENGTH_SHORT).show();
}
});
}
private void updateUI() {
mCursor = mDatabase.query(DBHelper.TABLE_NAME, null, null, null, null, null, null);
mAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_2, mCursor,
new String[]{DBHelper.COLUMN_NAME, DBHelper.COLUMN_PRICE},
new int[]{android.R.id.text1, android.R.id.text2}, 0);
mListView.setAdapter(mAdapter);
}
@Override
protected void onDestroy() {
super.onDestroy();
mDatabase.close();
mCursor.close();
}
}
```
接下来,我们需要创建一个数据库帮助器类 DBHelper,用于创建和管理数据库。代码如下:
```java
public class DBHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "shopping.db";
public static final int DATABASE_VERSION = 1;
public static final String TABLE_NAME = "items";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_NAME = "name";
public static final String COLUMN_PRICE = "price";
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String createTable = "CREATE TABLE " + TABLE_NAME + " (" +
COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COLUMN_NAME + " TEXT, " +
COLUMN_PRICE + " TEXT)";
db.execSQL(createTable);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
}
```
最后,我们需要在AndroidManifest.xml文件中声明两个活动。
```xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.shoppingapp">
<application
...>
<activity android:name=".MerchantActivity" />
<activity android:name=".UserActivity" />
...
</application>
</manifest>
```
这就是一个简单的购物APP的代码示例,您可以根据您的需求进行修改和扩展。
阅读全文