如何通过ListView点击索引获取SQLite表数据并显示在新Activity的TextView中
实现步骤
- 首先修改
activity_booking.xml布局,新增对应字段的TextView组件,每个航班属性对应一个展示组件,可自行调整样式:
<!-- 仅展示核心组件部分 --> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:padding="16dp"> <TextView android:id="@+id/tv_destination" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="18sp" android:layout_marginBottom="8dp"/> <TextView android:id="@+id/tv_price" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="18sp" android:layout_marginBottom="8dp"/> <TextView android:id="@+id/tv_departure" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="18sp" android:layout_marginBottom="8dp"/> <TextView android:id="@+id/tv_arrival" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="18sp" android:layout_marginBottom="8dp"/> <TextView android:id="@+id/tv_duration" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="18sp" android:layout_marginBottom="8dp"/> <TextView android:id="@+id/tv_seats" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="18sp"/> </LinearLayout>
- 给
DBHelper类新增按点击索引查询对应航班数据的方法,避免全量查询浪费性能:
// 直接加入到DBHelper类中即可 public FlightsModel getFlightByPosition(int position){ SQLiteDatabase db = this.getReadableDatabase(); // 排序规则和ListView数据源保持一致,保证索引对应正确 Cursor cursor = db.rawQuery("SELECT * FROM " + FLIGHTS + " ORDER BY " + COLUMN_ID + " ASC LIMIT 1 OFFSET ?", new String[]{String.valueOf(position)}); FlightsModel flight = null; if(cursor.moveToFirst()){ int id = cursor.getInt(0); String destination = cursor.getString(1); double price = cursor.getDouble(2); String departure = cursor.getString(3); String arrival = cursor.getString(4); String duration = cursor.getString(5); int space = cursor.getInt(6); flight = new FlightsModel(id, destination, price, departure, arrival, duration, space); } cursor.close(); db.close(); return flight; }
- 修改
BookingActivity代码,获取航班数据后赋值给对应TextView:
package com.example.shashank.fffffffffffffffffffffffffff; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.widget.TextView; import android.widget.Toast; public class BookingActivity extends AppCompatActivity { TextView tvDestination, tvPrice, tvDeparture, tvArrival, tvDuration, tvSeats; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_booking); // 绑定所有布局组件 tvDestination = findViewById(R.id.tv_destination); tvPrice = findViewById(R.id.tv_price); tvDeparture = findViewById(R.id.tv_departure); tvArrival = findViewById(R.id.tv_arrival); tvDuration = findViewById(R.id.tv_duration); tvSeats = findViewById(R.id.tv_seats); Intent mIntent = getIntent(); int clickPosition = mIntent.getIntExtra("intVariableName", 0); // 查询对应航班数据 DBHelper dbHelper = new DBHelper(this); FlightsModel currentFlight = dbHelper.getFlightByPosition(clickPosition); if(currentFlight != null){ // 赋值给各TextView展示 tvDestination.setText("目的地:" + currentFlight.getDestination()); tvPrice.setText("票价:" + currentFlight.getPrice() + "元"); tvDeparture.setText("起飞时间:" + currentFlight.getDepartureTime()); tvArrival.setText("到达时间:" + currentFlight.getArrivalTime()); tvDuration.setText("飞行时长:" + currentFlight.getDuration() + "小时"); tvSeats.setText("剩余座位:" + currentFlight.getAvailableSeats() + "个"); }else { Toast.makeText(this, "未查询到对应航班信息", Toast.LENGTH_SHORT).show(); } } }
注意事项
- 请确认
FlightsModel类已经实现了所有字段的getter方法,没有的话需要先补充 - 如果你的ListView数据源排序规则不是按ID升序,需要同步修改
getFlightByPosition方法的排序规则,保证索引对应正确 - 也可以在跳转前的Activity把选中的
FlightsModel序列化后直接通过Intent传递,不需要二次查询数据库,性能更好
内容的提问来源于stack exchange,提问作者Innovated
相关产品推荐
相关产品推荐

