android studio增删改查功能且有五个页面的代码
时间: 2023-12-25 07:04:49 浏览: 239
好的,以下是一个简单的实现增删改查功能并且有五个页面的 Android Studio 代码示例,供您参考:
1. MainActivity.java
```java
public class MainActivity extends AppCompatActivity {
Button btnAdd, btnView;
EditText editTextName, editTextAge;
SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnAdd = findViewById(R.id.btnAdd);
btnView = findViewById(R.id.btnView);
editTextName = findViewById(R.id.editTextName);
editTextAge = findViewById(R.id.editTextAge);
db = openOrCreateDatabase("MyDB", Context.MODE_PRIVATE, null);
db.execSQL("CREATE TABLE IF NOT EXISTS Users (Id INTEGER PRIMARY KEY AUTOINCREMENT, Name VARCHAR, Age INT)");
btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String name = editTextName.getText().toString();
int age = Integer.parseInt(editTextAge.getText().toString());
db.execSQL("INSERT INTO Users (Name, Age) VALUES ('" + name + "', " + age + ")");
Toast.makeText(MainActivity.this, "User added successfully", Toast.LENGTH_SHORT).show();
}
});
btnView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, ViewUsersActivity.class);
startActivity(intent);
}
});
}
}
```
2. ViewUsersActivity.java
```java
public class ViewUsersActivity extends AppCompatActivity {
RecyclerView recyclerView;
SQLiteDatabase db;
ArrayList<User> userList;
UserAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_users);
recyclerView = findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
userList = new ArrayList<>();
adapter = new UserAdapter(this, userList);
recyclerView.setAdapter(adapter);
db = openOrCreateDatabase("MyDB", Context.MODE_PRIVATE, null);
Cursor cursor = db.rawQuery("SELECT * FROM Users", null);
if (cursor.moveToFirst()) {
do {
int id = cursor.getInt(cursor.getColumnIndex("Id"));
String name = cursor.getString(cursor.getColumnIndex("Name"));
int age = cursor.getInt(cursor.getColumnIndex("Age"));
userList.add(new User(id, name, age));
} while (cursor.moveToNext());
}
adapter.notifyDataSetChanged();
}
}
```
3. UserAdapter.java
```java
public class UserAdapter extends RecyclerView.Adapter<UserAdapter.ViewHolder> {
Context context;
ArrayList<User> userList;
public UserAdapter(Context context, ArrayList<User> userList) {
this.context = context;
this.userList = userList;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.user_item, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.textViewId.setText(String.valueOf(userList.get(position).getId()));
holder.textViewName.setText(userList.get(position).getName());
holder.textViewAge.setText(String.valueOf(userList.get(position).getAge()));
}
@Override
public int getItemCount() {
return userList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView textViewId, textViewName, textViewAge;
public ViewHolder(@NonNull View itemView) {
super(itemView);
textViewId = itemView.findViewById(R.id.textViewId);
textViewName = itemView.findViewById(R.id.textViewName);
textViewAge = itemView.findViewById(R.id.textViewAge);
}
}
}
```
4. User.java
```java
public class User {
private int id, age;
private String name;
public User(int id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
```
5. activity_main.xml
```xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:padding="16dp">
<EditText
android:id="@+id/editTextName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Name"
android:inputType="text" />
<EditText
android:id="@+id/editTextAge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Age"
android:inputType="number" />
<Button
android:id="@+id/btnAdd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Add User" />
<Button
android:id="@+id/btnView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="View Users" />
</LinearLayout>
```
6. activity_view_users.xml
```xml
<androidx.recyclerview.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
```
7. user_item.xml
```xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="16dp">
<TextView
android:id="@+id/textViewId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Id: "
android:textStyle="bold" />
<TextView
android:id="@+id/textViewName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name: " />
<TextView
android:id="@+id/textViewAge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Age: " />
</LinearLayout>
```
以上是一个简单的实现增删改查功能并且有五个页面的 Android Studio 代码示例,具体实现过程可以根据您的需求和技术水平进行调整。
阅读全文