建立一个数据库,包含时间,项目,场馆三个字段,插入三条数据。布局有三个spinner,分别代表时间,项目和场馆,当选中不同的选项时,查询数据库显示不同的时间,项目,场馆数据。利用recycleview显示数据。请写出示例代码。
时间: 2023-12-28 07:05:03 浏览: 74
好的,以下是一个简单的示例代码:
首先,在布局文件中添加三个 Spinner 和一个 RecyclerView:
```xml
<Spinner
android:id="@+id/time_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Spinner
android:id="@+id/project_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Spinner
android:id="@+id/venue_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
```
然后,在 Java 文件中定义一个 Model 类来表示数据库中的数据:
```java
public class DataModel {
private String time;
private String project;
private String venue;
public DataModel(String time, String project, String venue) {
this.time = time;
this.project = project;
this.venue = venue;
}
public String getTime() {
return time;
}
public String getProject() {
return project;
}
public String getVenue() {
return venue;
}
}
```
接下来,定义一个 Adapter 类来绑定 RecyclerView 和数据:
```java
public class DataAdapter extends RecyclerView.Adapter<DataAdapter.ViewHolder> {
private List<DataModel> dataList;
public DataAdapter(List<DataModel> dataList) {
this.dataList = dataList;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_data, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
DataModel data = dataList.get(position);
holder.timeTextView.setText(data.getTime());
holder.projectTextView.setText(data.getProject());
holder.venueTextView.setText(data.getVenue());
}
@Override
public int getItemCount() {
return dataList.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView timeTextView;
public TextView projectTextView;
public TextView venueTextView;
public ViewHolder(@NonNull View itemView) {
super(itemView);
timeTextView = itemView.findViewById(R.id.time_text_view);
projectTextView = itemView.findViewById(R.id.project_text_view);
venueTextView = itemView.findViewById(R.id.venue_text_view);
}
}
}
```
其中,item_data.xml 是 RecyclerView 中每个数据项的布局文件。
然后,在 Activity 或 Fragment 中,初始化 Spinner 和 RecyclerView,并填充数据:
```java
// 初始化 Spinner
Spinner timeSpinner = findViewById(R.id.time_spinner);
Spinner projectSpinner = findViewById(R.id.project_spinner);
Spinner venueSpinner = findViewById(R.id.venue_spinner);
// 初始化 RecyclerView
RecyclerView recyclerView = findViewById(R.id.recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
// 填充数据
List<DataModel> dataList = new ArrayList<>();
dataList.add(new DataModel("2021-01-01 10:00", "篮球比赛", "体育馆A"));
dataList.add(new DataModel("2021-01-02 14:00", "足球比赛", "体育馆B"));
dataList.add(new DataModel("2021-01-03 09:00", "乒乓球比赛", "体育馆C"));
DataAdapter adapter = new DataAdapter(dataList);
recyclerView.setAdapter(adapter);
```
最后,添加 Spinner 的监听器,在选择不同选项时,查询数据库并更新 RecyclerView:
```java
timeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String selectedTime = parent.getItemAtPosition(position).toString();
// 查询数据库并更新 RecyclerView
// ...
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
projectSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String selectedProject = parent.getItemAtPosition(position).toString();
// 查询数据库并更新 RecyclerView
// ...
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
venueSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String selectedVenue = parent.getItemAtPosition(position).toString();
// 查询数据库并更新 RecyclerView
// ...
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
```
在查询数据库时,可以使用 SQLiteOpenHelper 类来创建和访问数据库。具体实现方式可以参考 Android 官方文档。
阅读全文