Android开发:如何在UserProfile Activity的TextView展示SQLite用户Profile数据?
实现UserProfile Activity展示SQLite中的Profile数据
嘿,你已经把核心的Profile模型、数据库操作类都搞定了,剩下的就是在UserProfile里把数据取出来展示到界面上,咱一步步来:
1. 先准备UserProfile的布局文件
首先确保你的activity_user_profile.xml里有对应展示姓名、电话、地址的TextView,给它们设置好id,方便后续绑定数据:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="16dp"> <TextView android:id="@+id/tv_name_label" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="姓名:" android:textSize="16sp" android:textStyle="bold"/> <TextView android:id="@+id/tv_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="16sp" android:layout_marginBottom="12dp"/> <TextView android:id="@+id/tv_phone_label" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="电话:" android:textSize="16sp" android:textStyle="bold"/> <TextView android:id="@+id/tv_phone" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="16sp" android:layout_marginBottom="12dp"/> <TextView android:id="@+id/tv_address_label" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="地址:" android:textSize="16sp" android:textStyle="bold"/> <TextView android:id="@+id/tv_address" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="16sp"/> </LinearLayout>
2. 在UserProfileActivity中实现数据检索与展示
接下来写Activity的逻辑,主要做三件事:初始化数据库操作类、获取Profile数据、把数据绑定到TextView上。这里假设你的getProfile()方法是返回单个用户的Profile实例(因为是个人资料,一般只有一条),如果你的方法是根据id查询,记得传入对应的用户id:
public class UserProfileActivity extends AppCompatActivity { private TextView tvName, tvPhone, tvAddress; private DBHandler dbHandler; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_user_profile); // 初始化TextView控件 tvName = findViewById(R.id.tv_name); tvPhone = findViewById(R.id.tv_phone); tvAddress = findViewById(R.id.tv_address); // 初始化DBHandler,注意用ApplicationContext避免内存泄漏 dbHandler = new DBHandler(getApplicationContext()); // 从数据库获取Profile数据 Profile userProfile = dbHandler.getProfile(); // 检查数据是否存在,然后绑定到TextView if (userProfile != null) { tvName.setText(userProfile.getName()); tvPhone.setText(userProfile.getPhone()); tvAddress.setText(userProfile.getAddress()); } else { // 如果数据库里还没有数据,显示提示 tvName.setText("暂无资料,请先完善个人信息"); tvPhone.setText(""); tvAddress.setText(""); } } @Override protected void onDestroy() { super.onDestroy(); // 关闭数据库连接,避免资源浪费 if (dbHandler != null) { dbHandler.close(); } } }
3. 关键细节检查
- 确保你的
Profile类有对应的getter方法:getName()、getPhone()、getAddress(),不然上面的代码会报错; - 确认
DBHandler的getProfile()方法逻辑正确:比如如果是查询所有用户的第一条,或者根据唯一标识查询,要保证能正确返回已存入的Profile对象; - 从SetUpProfile跳转到UserProfile的时候,最好在
addProfile执行完成后再跳转,确保数据已经写入数据库;
这样操作下来,用户在SetUpProfile创建的资料就能在UserProfile里正常展示啦!
内容的提问来源于stack exchange,提问作者Thorvald
相关产品推荐
相关产品推荐

