Java 使用sqlite数据库中的数据在ListView上显示子项

Java 使用sqlite数据库中的数据在ListView上显示子项,java,android,sqlite,android-studio,android-listview,Java,Android,Sqlite,Android Studio,Android Listview,我希望通过从数据库检索数据,将dataTime显示为每个项(事件)的子项。但是我不知道如何做(在MainActivity.java中的updateUI()函数下)。 这是我目前得到的,我现在无法显示日期时间: MainActivity.java public class MainActivity extends androidx.appcompat.app.AppCompatActivity { private static final String TAG = "MainA

我希望通过从数据库检索数据,将dataTime显示为每个项(事件)的子项。但是我不知道如何做(在MainActivity.java中的updateUI()函数下)。 这是我目前得到的,我现在无法显示日期时间:

MainActivity.java

public class MainActivity extends androidx.appcompat.app.AppCompatActivity {
    private static final String TAG = "MainActivity";
    private eventDbHelper mHelper;
    private ListView mEventListView;
    private ArrayAdapter<String> mAdapter;
    //EditText event, dateTime;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mHelper = new eventDbHelper(this);
        mEventListView = (ListView) findViewById(R.id.events);


        updateUI();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main_menu, menu);
        return super.onCreateOptionsMenu(menu);
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_add_event:
                Intent intent = new Intent(getApplicationContext(), EventDetails.class);
                startActivity(intent);
                updateUI();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    public void deleteEvent(View view) {
        View parent = (View) view.getParent();
        TextView eventTextView = (TextView) parent.findViewById(R.id.event_title);
        String event = String.valueOf(eventTextView.getText());
        SQLiteDatabase db = mHelper.getWritableDatabase();
        db.delete(eventContract.EventEntry.TABLE,
                eventContract.EventEntry.COL_EVENT_TITLE + " = ?",
                new String[]{event});
        db.close();
        updateUI();
    }


    private void updateUI() {
        ArrayList<String> eventList = new ArrayList<>();
        SQLiteDatabase db = mHelper.getReadableDatabase();
        Cursor cursor = db.query(eventContract.EventEntry.TABLE,
                new String[]{eventContract.EventEntry._ID, eventContract.EventEntry.COL_EVENT_TITLE},
                //new String[]{eventContract.EventEntry._ID, eventContract.EventEntry.COL_EVENT_DATE},
                null, null, null, null, null);
        while (cursor.moveToNext()) {
            int idx = cursor.getColumnIndex(eventContract.EventEntry.COL_EVENT_TITLE);
            eventList.add(cursor.getString(idx));
        }

        if (mAdapter == null) {
            mAdapter = new ArrayAdapter<>(this,
                    R.layout.new_event,
                    R.id.event_title,
                    eventList);
            mEventListView.setAdapter(mAdapter);
        } else {
            mAdapter.clear();
            mAdapter.addAll(eventList);
            mAdapter.notifyDataSetChanged();
        }

        cursor.close();
        db.close();
    }
}
activity\u event\u details.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_gravity="center_vertical"
    android:orientation="vertical"
    android:padding="10dp"
    android:gravity="center_horizontal"
    tools:context=".EventDetails">

    <TextView
        android:id="@+id/pageTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="false"
        android:layout_alignParentLeft="false"
        android:layout_centerHorizontal="true"
        android:layout_marginStart="130dp"
        android:layout_marginLeft="0dp"
        android:layout_marginTop="10dp"
        android:layout_marginEnd="10dp"
        android:layout_marginBottom="10dp"
        android:hint="Enter Event Details"
        android:inputType="text"
        android:textColor="#000000"
        android:textSize="30sp" />

    <EditText
        android:id="@+id/event_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/pageTitle"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_marginStart="10dp"
        android:layout_marginLeft="1dp"
        android:layout_marginTop="57dp"
        android:layout_marginEnd="10dp"
        android:layout_marginBottom="10dp"
        android:hint="Event Title"
        android:inputType="text"
        android:textSize="20sp" />

    <EditText
        android:id="@+id/event_date_time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/event_title"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_marginStart="10dp"
        android:layout_marginLeft="22dp"
        android:layout_marginTop="80dp"
        android:layout_marginEnd="10dp"
        android:layout_marginBottom="10dp"
        android:hint="Date and Time"
        android:inputType="text"
        android:textSize="20sp" />

    <Button
        android:id="@+id/Done"
        android:layout_width="600dp"
        android:layout_height="wrap_content"
        android:layout_alignWithParentIfMissing="true"
        android:layout_below="@+id/event_date_time"
        android:layout_marginTop="30dp"
        android:layout_marginBottom="5dp"
        android:text="Done" />

    <Button
        android:id="@+id/Cancel"
        android:layout_width="600dp"
        android:layout_height="wrap_content"

        android:layout_alignWithParentIfMissing="true"
        android:layout_below="@+id/Done"

        android:layout_marginTop="30dp"
        android:layout_marginBottom="5dp"
        android:text="Cancel" />

</RelativeLayout>
eventDbHelper.java

public class eventDbHelper extends SQLiteOpenHelper {


    public eventDbHelper(Context context) {
        super(context, eventContract.DB_NAME, null, eventContract.DB_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase myDB) {
        String createTable = "CREATE TABLE " + eventContract.EventEntry.TABLE + " ( " +
                eventContract.EventEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                eventContract.EventEntry.COL_EVENT_TITLE + " TEXT NOT NULL," +
                eventContract.EventEntry.COL_EVENT_DATE_TIME + " TEXT NOT NULL,);";

        myDB.execSQL(createTable);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + eventContract.EventEntry.TABLE);
        onCreate(db);
    }
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_gravity="center_vertical"
    android:orientation="vertical"
    android:padding="10dp"
    android:gravity="center_horizontal"
    tools:context=".EventDetails">

    <TextView
        android:id="@+id/pageTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="false"
        android:layout_alignParentLeft="false"
        android:layout_centerHorizontal="true"
        android:layout_marginStart="130dp"
        android:layout_marginLeft="0dp"
        android:layout_marginTop="10dp"
        android:layout_marginEnd="10dp"
        android:layout_marginBottom="10dp"
        android:hint="Enter Event Details"
        android:inputType="text"
        android:textColor="#000000"
        android:textSize="30sp" />

    <EditText
        android:id="@+id/event_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/pageTitle"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_marginStart="10dp"
        android:layout_marginLeft="1dp"
        android:layout_marginTop="57dp"
        android:layout_marginEnd="10dp"
        android:layout_marginBottom="10dp"
        android:hint="Event Title"
        android:inputType="text"
        android:textSize="20sp" />

    <EditText
        android:id="@+id/event_date_time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/event_title"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_marginStart="10dp"
        android:layout_marginLeft="22dp"
        android:layout_marginTop="80dp"
        android:layout_marginEnd="10dp"
        android:layout_marginBottom="10dp"
        android:hint="Date and Time"
        android:inputType="text"
        android:textSize="20sp" />

    <Button
        android:id="@+id/Done"
        android:layout_width="600dp"
        android:layout_height="wrap_content"
        android:layout_alignWithParentIfMissing="true"
        android:layout_below="@+id/event_date_time"
        android:layout_marginTop="30dp"
        android:layout_marginBottom="5dp"
        android:text="Done" />

    <Button
        android:id="@+id/Cancel"
        android:layout_width="600dp"
        android:layout_height="wrap_content"

        android:layout_alignWithParentIfMissing="true"
        android:layout_below="@+id/Done"

        android:layout_marginTop="30dp"
        android:layout_marginBottom="5dp"
        android:text="Cancel" />

</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/eventTitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="#000000"
        android:textSize="21sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/eventDateTime"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="#000000"
        android:textSize="16sp"
        android:textStyle="bold" />


</LinearLayout>
public class eventContract {
    public static final String DB_NAME = "event.db";
    public static final int DB_VERSION = 1;

    public class EventEntry implements BaseColumns {
        public static final String TABLE = "events";

        public static final String COL_EVENT_TITLE = "title";
        public static final String COL_EVENT_DATE_TIME = "datetime";
    }
}
public class eventDbHelper extends SQLiteOpenHelper {


    public eventDbHelper(Context context) {
        super(context, eventContract.DB_NAME, null, eventContract.DB_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase myDB) {
        String createTable = "CREATE TABLE " + eventContract.EventEntry.TABLE + " ( " +
                eventContract.EventEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                eventContract.EventEntry.COL_EVENT_TITLE + " TEXT NOT NULL," +
                eventContract.EventEntry.COL_EVENT_DATE_TIME + " TEXT NOT NULL,);";

        myDB.execSQL(createTable);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + eventContract.EventEntry.TABLE);
        onCreate(db);
    }
}