建立一个数据库,包含时间,项目,场馆三个字段,插入三条数据。布局有三个spinner,分别代表时间,项目和场馆,当选中不同的选项时,查询数据库显示不同的时间,项目,场馆数据。利用recycleview显示数据。请写出示例代码。
时间: 2023-12-28 22:05:03 浏览: 116
好的,下面是示例代码。
首先是建立数据库的代码,我们使用SQLiteOpenHelper类来实现:
```java
public class MyDatabaseHelper extends SQLiteOpenHelper {
private static final String DB_NAME = "my_database";
private static final int DB_VERSION = 1;
private static final String TABLE_NAME = "my_table";
private static final String COLUMN_ID = "_id";
private static final String COLUMN_TIME = "time";
private static final String COLUMN_PROJECT = "project";
private static final String COLUMN_VENUE = "venue";
public MyDatabaseHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql = "CREATE TABLE " + TABLE_NAME + " (" +
COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COLUMN_TIME + " TEXT, " +
COLUMN_PROJECT + " TEXT, " +
COLUMN_VENUE + " TEXT)";
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
public void insertData(SQLiteDatabase db, String time, String project, String venue) {
ContentValues values = new ContentValues();
values.put(COLUMN_TIME, time);
values.put(COLUMN_PROJECT, project);
values.put(COLUMN_VENUE, venue);
db.insert(TABLE_NAME, null, values);
}
public Cursor queryData(SQLiteDatabase db, String time, String project, String venue) {
String[] columns = {COLUMN_TIME, COLUMN_PROJECT, COLUMN_VENUE};
String selection = COLUMN_TIME + "=? AND " + COLUMN_PROJECT + "=? AND " + COLUMN_VENUE + "=?";
String[] selectionArgs = {time, project, venue};
return db.query(TABLE_NAME, columns, selection, selectionArgs, null, null, null);
}
}
```
接下来是布局文件,我们使用LinearLayout和Spinner来实现:
```xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Spinner
android:id="@+id/time_spinner"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<Spinner
android:id="@+id/project_spinner"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<Spinner
android:id="@+id/venue_spinner"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
```
然后是RecyclerView的item布局文件,我们使用TextView来显示数据:
```xml
<TextView
android:id="@+id/time_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/project_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/venue_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
```
接下来是MainActivity的代码,我们在onCreate方法中初始化布局和数据库,并且设置Spinner的选项和监听器:
```java
public class MainActivity extends AppCompatActivity {
private Spinner mTimeSpinner;
private Spinner mProjectSpinner;
private Spinner mVenueSpinner;
private RecyclerView mRecyclerView;
private MyAdapter mAdapter;
private MyDatabaseHelper mDatabaseHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTimeSpinner = findViewById(R.id.time_spinner);
mProjectSpinner = findViewById(R.id.project_spinner);
mVenueSpinner = findViewById(R.id.venue_spinner);
mRecyclerView = findViewById(R.id.recycler_view);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mAdapter = new MyAdapter();
mRecyclerView.setAdapter(mAdapter);
mDatabaseHelper = new MyDatabaseHelper(this);
SQLiteDatabase db = mDatabaseHelper.getWritableDatabase();
mDatabaseHelper.insertData(db, "10:00", "篮球", "A场馆");
mDatabaseHelper.insertData(db, "11:00", "足球", "B场馆");
mDatabaseHelper.insertData(db, "12:00", "乒乓球", "C场馆");
String[] times = {"10:00", "11:00", "12:00"};
ArrayAdapter<String> timeAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, times);
mTimeSpinner.setAdapter(timeAdapter);
String[] projects = {"篮球", "足球", "乒乓球"};
ArrayAdapter<String> projectAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, projects);
mProjectSpinner.setAdapter(projectAdapter);
String[] venues = {"A场馆", "B场馆", "C场馆"};
ArrayAdapter<String> venueAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, venues);
mVenueSpinner.setAdapter(venueAdapter);
mTimeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String time = (String) parent.getItemAtPosition(position);
String project = (String) mProjectSpinner.getSelectedItem();
String venue = (String) mVenueSpinner.getSelectedItem();
Cursor cursor = mDatabaseHelper.queryData(db, time, project, venue);
mAdapter.swapCursor(cursor);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
mProjectSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String time = (String) mTimeSpinner.getSelectedItem();
String project = (String) parent.getItemAtPosition(position);
String venue = (String) mVenueSpinner.getSelectedItem();
Cursor cursor = mDatabaseHelper.queryData(db, time, project, venue);
mAdapter.swapCursor(cursor);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
mVenueSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String time = (String) mTimeSpinner.getSelectedItem();
String project = (String) mProjectSpinner.getSelectedItem();
String venue = (String) parent.getItemAtPosition(position);
Cursor cursor = mDatabaseHelper.queryData(db, time, project, venue);
mAdapter.swapCursor(cursor);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
private class MyAdapter extends RecyclerView.Adapter<MyViewHolder> {
private Cursor mCursor;
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_layout, parent, false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
mCursor.moveToPosition(position);
String time = mCursor.getString(mCursor.getColumnIndex(MyDatabaseHelper.COLUMN_TIME));
String project = mCursor.getString(mCursor.getColumnIndex(MyDatabaseHelper.COLUMN_PROJECT));
String venue = mCursor.getString(mCursor.getColumnIndex(MyDatabaseHelper.COLUMN_VENUE));
holder.timeTextView.setText(time);
holder.projectTextView.setText(project);
holder.venueTextView.setText(venue);
}
@Override
public int getItemCount() {
return mCursor == null ? 0 : mCursor.getCount();
}
public void swapCursor(Cursor cursor) {
if (mCursor != null) {
mCursor.close();
}
mCursor = cursor;
notifyDataSetChanged();
}
}
private static class MyViewHolder extends RecyclerView.ViewHolder {
private TextView timeTextView;
private TextView projectTextView;
private TextView venueTextView;
public MyViewHolder(@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);
}
}
}
```
这样就实现了你要求的功能,希望能对你有所帮助!
阅读全文