You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android多表查询适配ListView及SimpleCursorAdapter问题求助

解决SQLite多表查询适配ListView及布局异常问题

Hey there! Let's break down your two main issues and fix them step by step:

1. 修复多表查询与Historique构造器不匹配的问题

首先看你的getTableHistoAsCursor()方法,查询返回了5个字段:Q.KEY_QUESTION, U.KEY_NOM, U.KEY_PRENOM, A.KEY_DATE, KEY_REPONSE,但你的Historique构造器只接收4个参数,这肯定会报错。不过更关键的是——你其实没必要把Cursor转成ArrayList再用,SimpleCursorAdapter可以直接绑定Cursor数据,但有个硬性要求:Cursor必须包含名为_id的列(这是SimpleCursorAdapter的底层规则,用来唯一标识每条数据)。

步骤1:修正SQL查询语句

添加主键列(确保唯一),同时调整字段顺序匹配你的TextView需求,还要注意SQL语法的空格问题(你之前的AND前面没加空格,会导致语法错误):

public Cursor getTableHistoAsCursor() {
    SQLiteDatabase db = this.getReadableDatabase();
    // 加入A._id作为唯一标识列,同时调整字段顺序适配后续绑定
    return db.rawQuery(
        " SELECT A._id, U." + KEY_NOM + ", Q." + KEY_QUESTION + ", " + KEY_REPONSE + ", A." + KEY_DATE + 
        " FROM " + TABLE_QUESTION + " Q, " + TABLE_USER + " U, " + TABLE_ANSWER + " A " +
        " WHERE Q." + KEY_ID_QUESTION + " = A." + KEY_ID_QUESTION + 
        " AND A." + KEY_MATRICULE + " = U." + KEY_MATRICULE,
        null
    );
}

步骤2:修正Historique Activity代码

去掉多余的ArrayList转换逻辑,直接用Cursor绑定,同时修正几个关键错误:

  • 你把页面布局activity_historique传给了SimpleCursorAdapter,这是错的,应该传单个列表项的自定义布局(就是你开头贴的包含四个TextView的LinearLayout布局)
  • 修正TextView的id拼写错误:R.id.hnate应该是R.id.hdate
  • 确保字段数组和查询列的顺序对应(跳过第一个_id列)
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_historique);
    mListView = findViewById(R.id.lsthisto);
    openhelper = new ModelHelper(this);
    
    mCsr = openhelper.getTableHistoAsCursor();
    if (mCsr.getCount() == 0 ){
        Toast.makeText(Historique.this, "No histo", Toast.LENGTH_SHORT).show();
    } 

    // 初始化SimpleCursorAdapter
    mSCA = new SimpleCursorAdapter(
        this,
        R.layout.your_custom_item_layout, // 替换成你的列表项布局文件名
        new String[]{openhelper.KEY_NOM, openhelper.KEY_QUESTION, openhelper.KEY_REPONSE, openhelper.KEY_DATE},
        new int[]{R.id.txtnoom, R.id.txthistq, R.id.txthistr, R.id.hdate},
        0
    );
    mListView.setAdapter(mSCA);
}

2. 修复添加日期TextView后的布局异常问题

你的列表项布局有几个核心问题:

  • 外层weightSum=100但子布局权重总和只有66.6,剩余空间分配混乱
  • 子LinearLayout设置了固定layout_width,和layout_weight冲突(权重分配剩余空间时,layout_width应该设为0dp)
  • 嵌套层级可以简化,避免布局冗余

修正后的列表项布局:

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"
    android:weightSum="3"> <!-- 分成3等份,可根据需求调整权重比例 -->

    <!-- 第一列:显示nom和date -->
    <LinearLayout 
        android:layout_width="0dp" 
        android:layout_height="wrap_content" 
        android:layout_weight="1" 
        android:orientation="vertical">
        <TextView 
            android:id="@+id/txtnoom" 
            android:layout_width="match_parent" 
            android:layout_height="30dp" 
            android:gravity="center" 
            android:text="nom" />
        <TextView 
            android:id="@+id/hdate" 
            android:layout_width="match_parent" 
            android:layout_height="30dp" 
            android:gravity="center" 
            android:text="date" />
    </LinearLayout>

    <!-- 第二列:显示question和réponse -->
    <LinearLayout 
        android:orientation="vertical" 
        android:layout_width="0dp" 
        android:layout_height="wrap_content" 
        android:layout_weight="2"> <!-- 让第二列占更大宽度 -->
        <TextView 
            android:id="@+id/txthistq"
            android:gravity="center" 
            android:text="question" 
            android:layout_width="match_parent" 
            android:layout_height="30dp" />
        <TextView 
            android:id="@+id/txthistr"
            android:gravity="center" 
            android:text="réponse" 
            android:layout_width="match_parent" 
            android:layout_height="30dp" />
    </LinearLayout>
</LinearLayout>

内容的提问来源于stack exchange,提问作者Master49

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:05:14