【问题标题】:Display header only when chlid is NOT NULL using Expandablelistview android studio -sqlite仅当使用 Expandablelistview android studio -sqlite chlid 不为 NULL 时显示标题
【发布时间】:2018-07-29 20:58:59
【问题描述】:

我正在尝试制作一个消耗性列表视图,其中标题文本从“器官特征”列中提取,子文本从 pharynx 列中提取,这是我的数据库结构:

Database structure

我希望器官特征仅在其子列表(= 咽列中的实例)不为空时才显示为标题。 这是我目前得到的(如你所见,咽列中的静脉引流是空的,但仍显示为标题,我不希望这样):

picture from the emulator

我认为我应该添加一个语句“if(DATABASECHILD_1 != null)”或类似的东西..

感谢您的帮助!

数据库.Java

package ma.ac.iav.learn;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.readystatesoftware.sqliteasset.SQLiteAssetHelper;

public class Database {
    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "URTdd.db";
    private static final String DATABASE_TABLE = "OrganAnatomy";
    public static final String DATABASE_ID = "_id";
    public static final String DATABASE_GROUP_1 = "Organ_features";
    public static final String DATABASE_CHILD_1 = "Pharynx";
    private final Context mContext;
    private DatabaseHelper mDatabaseHelper;
    private SQLiteDatabase mDB;

    public Database(Context context) {
        mContext = context;
    }

    public void open() {
        mDatabaseHelper = new DatabaseHelper(mContext, DATABASE_NAME, null, DATABASE_VERSION);
        mDB = mDatabaseHelper.getWritableDatabase();
    }


    public void close() {
        if (mDatabaseHelper != null) mDatabaseHelper.close();
    }

    public Cursor getDatabase() {
            return mDB.query(DATABASE_TABLE, null, null, null, null, null, DATABASE_GROUP_1);
    }

    public Cursor getID(long rowID) {
            return mDB.query(DATABASE_TABLE, null, "_id" + " = "
                + rowID, null, null, null, null);

    }

    public class DatabaseHelper extends SQLiteAssetHelper {

        public DatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
            super(context, name, factory, version);

        }
    }


}

MainActivity.java

package ma.ac.iav.learn;

import android.content.Context;
import android.database.Cursor;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ExpandableListView;
import android.widget.SimpleCursorTreeAdapter;

public class MainActivity extends AppCompatActivity {
    ExpandableListView expandableListView;
    Database mDatabase;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
             /*
        "Called when the database has been opened."
        https://developer.android.com/reference/android/database/sqlite/SQLiteOpenHelper.html#onOpen(android.database.sqlite.SQLiteDatabase)
         */
        mDatabase = new Database(this);
        mDatabase.open();

        /*
        "This interface provides random read-write access to the result set returned by a database query."
        https://developer.android.com/reference/android/database/Cursor.html
         */

        Cursor cursor = mDatabase.getDatabase();
        startManagingCursor(cursor);

        /*
        "A list of column names that will be used to display the data for a group."
        "The group views (from the group layouts) that should display column in the "from" parameter."
        "A list of column names that will be used to display the data for a child."
        "The resource identifier of a layout file that defines the views for a child."
        https://developer.android.com/reference/android/widget/SimpleCursorTreeAdapter.html#SimpleCursorTreeAdapter(android.content.Context, android.database.Cursor, int, java.lang.String[], int[], int, java.lang.String[], int[])
         */

        String[] groupFrom = {
                Database.DATABASE_GROUP_1,
        };

        int[] groupTo = {
                R.id.group1,
        };
        String[] childFrom = new String[]{
                Database.DATABASE_CHILD_1,
        };
        int[] childTo = {
                R.id.child1,
        };

        /*
        "An easy adapter to map columns from a cursor to TextViews or ImageViews defined in an XML file."
        https://developer.android.com/reference/android/widget/SimpleCursorTreeAdapter.html
        */
        SimpleCursorTreeAdapter simplecursortreeAdapter = new ExpandableListViewAdapter(
                this,
                cursor,
                R.layout.list_group,
                groupFrom,
                groupTo,
                R.layout.list_child,
                childFrom,
                childTo
        );

        /*
        "Finds a view that was identified by the android:id XML attribute that was processed in onCreate(Bundle)."
        "Sets the adapter that provides data to this view."
        https://developer.android.com/reference/android/app/Activity.html#findViewById(int)
        https://developer.android.com/reference/android/widget/ExpandableListView.html#setAdapter(android.widget.ExpandableListAdapter)
         */
        expandableListView = findViewById(R.id.expandableListview);
        expandableListView.setAdapter(simplecursortreeAdapter);

    }

    /*
    "Closes the Cursor, releasing all of its resources and making it completely invalid."
    https://developer.android.com/reference/android/database/Cursor.html#close()
     */
    protected void onDestroy() {
        super.onDestroy();
        mDatabase.close();
    }

    private class ExpandableListViewAdapter extends SimpleCursorTreeAdapter {
        private ExpandableListViewAdapter(
                Context context,
                Cursor cursor,
                int groupLayout,
                String[] groupFrom,
                int[] groupTo,
                int childLayout,
                String[] childFrom,
                int[] childTo) {
            super(context, cursor, groupLayout, groupFrom, groupTo, childLayout, childFrom, childTo);
        }

        /*
        "Gets the Cursor for the children at the given group."
        https://developer.android.com/reference/android/widget/CursorTreeAdapter.html#getChildrenCursor(android.database.Cursor)
         */
        protected Cursor getChildrenCursor(Cursor groupCursor) {
            return mDatabase.getID(groupCursor.getInt(groupCursor.getColumnIndex(Database.DATABASE_ID)));
        }

    }
}

【问题讨论】:

    标签: android sqlite expandablelistview


    【解决方案1】:

    我相信您需要基于以下内容的查询:-

    SELECT * FROM OrganAnatomy WHERE Pharynx IS NOT NULL ORDER BY Organ_features;
    

    这将导致:-

    要将其转换为SQLiteDatabase query 方法使用,代码而不是:-

        public Cursor getDatabase() {
            return mDB.query(DATABASE_TABLE, null, null, null, null, null, DATABASE_GROUP_1);
        }
    

    可能是:-

        public Cursor getDatabase() {
            String whereclause = DATABASE_CHILD_1 + " IS NOT NULL";
            return mDB.query(DATABASE_TABLE, null, whereclause, null, null, null, DATABASE_GROUP_1);
        }
    
    • 注意以上代码为in-principal code,未经测试,可能存在一些错误。
    • 请注意,没有尝试将其应用于 ExpanableListView,假设适配器正确处理了光标。
    • 没有这样的 IF 语句(IF ??? EXISTS/NOT EXISTS 是您看到使用 IF 的地方),而是一个 WHERE 子句,它确定要包含哪些行(因此在这种情况下只包含行 WHERE Pharynx 列不为空)。
      • 真正等效于 IF 的是 CASE WHEN .... ELSE .... END AS 构造,尽管这不是必需的,也不适合这种情况。
      • 还有 HAVING 子句,但这需要一个 GROUP BY 子句并作用于结果而不是输入行,并且真正用于后处理,例如您可以拥有SELECT * FROM OrganAnatomy GROUP BY _id HAVING Pharynx IS NOT NULL ORDER BY Organ_features;(作为_id 工作将是唯一的,因此每一行都将在它自己的组中)。
      • 在这种情况下,使用 HAVING 子句过于矫枉过正/过于复杂。

    【讨论】:

    • 哥们,我欠你太多了!你无法想象在你回答之前我有多沮丧!如果你去过摩洛哥,我很乐意邀请​​你,非常感谢!你应该知道你帮了我一个大忙!
    • 只有两件事:如果我想用 expendablelistview 制作两个活动,但内容不同(第一个使用带有 Not null 语句的列,另一个使用另一列但没有任何内容,)..应该我创建了两个数据库类(我已经完成了,它工作得很好,但我不知道是否还有其他合适的方法)
    • 第二个问题是:在每个子视图中,都会有文字和一些图像(数字因子而异)。我该如何调整或做到这一点? - 我知道解释起来会很长..我只需要指导我应该使用什么..就像你在回答中所做的那样,因为你解释得很好!再次感谢!即使你没有回答这两个问题,你仍然是我的英雄!
    • 1) 可能使用相同的适配器,但可能使用传递给它的参数来区分(因此可以使用相同的光标/布局)。 2) typeOf(column_name) 可用于检查是否 BLOB 或不是 SQL As Understood By SQLite- Core Functions
    • 很抱歉,我不明白你的意思,我是安卓新手,所以我不知道所有的术语。我尝试的是在其他列(larynx_pitcures)中添加图片,并在相应案例中添加图片(有些案例是空的,每个案例我只能添加一张图片)。我在 child_list 布局中添加了 imageview! .. 这是我在 Database.java 中添加的内容:DATABASE_CHILD_2 = "pictures";'然后这就是我在 Mainactivity.java 中所做的更改: String[] childFrom = new String[]{Database.DATABASE_CHILD_1, Database.DATABASE_CHILD_2}; int[] childTo = {R.id.child1, R.id.child2};
    猜你喜欢
    • 2017-01-20
    • 2014-04-07
    • 2013-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多