我想通过从数据库中检索数据,将datatime显示为每个项(事件)的子项。但我不知道该怎么做(在mainactivity.java的updateui()函数下)。这是我目前得到的,我现在无法显示日期时间:
主活动.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();
}
}
活动\u main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp"
android:gravity="center_horizontal"
tools:context=".MainActivity">
<ListView
android:id="@+id/events"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
事件详细信息.java
public class EventDetails extends AppCompatActivity {
private eventDbHelper mHelper;
EditText event, dateTime;
Button done, cancel;
//mHelper = new eventDbHelper(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_event_details);
event = (EditText) findViewById(R.id.event_title);
dateTime = (EditText) findViewById(R.id.event_date_time);
done = (Button) findViewById(R.id.Done);
cancel = (Button) findViewById(R.id.Cancel);
mHelper = new eventDbHelper(this);
SQLiteDatabase db = mHelper.getWritableDatabase();
ContentValues eTitle = new ContentValues();
ContentValues eDateTime = new ContentValues();
Calendar calendar = Calendar.getInstance();
final int year = calendar.get(Calendar.YEAR);
final int month = calendar.get(Calendar.MONTH);
final int day = calendar.get(Calendar.DAY_OF_MONTH);
dateTime.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showDateTimeDialog(dateTime);
}
});
done.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String e = String.valueOf(event.getText());
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy HH:mm");
String dt = sdf.format(new Date());
if(e.equals("")||dt.equals(""))
Toast.makeText(EventDetails.this, "Please enter all the fields", Toast.LENGTH_SHORT).show();
else{
eTitle.put(eventContract.EventEntry.COL_EVENT_TITLE, e);
eDateTime.put(eventContract.EventEntry.COL_EVENT_DATE_TIME, dt);
db.insertWithOnConflict(eventContract.EventEntry.TABLE,
null,
eTitle,
SQLiteDatabase.CONFLICT_REPLACE);
Intent intent = new Intent(getBaseContext(), MainActivity.class);
intent.putExtra("EVENT_TITLE", e);
intent.putExtra("EVENT_DATE-TIME", dt);
startActivity(intent);
}
}
});
cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(EventDetails.this, "Event Cancelled!", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
}
});
}
private void showDateTimeDialog(final EditText date_time_in) {
final Calendar calendar=Calendar.getInstance();
DatePickerDialog.OnDateSetListener dateSetListener=new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
calendar.set(Calendar.YEAR,year);
calendar.set(Calendar.MONTH,month);
calendar.set(Calendar.DAY_OF_MONTH,dayOfMonth);
TimePickerDialog.OnTimeSetListener timeSetListener=new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
calendar.set(Calendar.HOUR_OF_DAY,hourOfDay);
calendar.set(Calendar.MINUTE,minute);
SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yy-MM-dd HH:mm");
date_time_in.setText(simpleDateFormat.format(calendar.getTime()));
}
};
new TimePickerDialog(EventDetails.this,timeSetListener,calendar.get(Calendar.HOUR_OF_DAY),calendar.get(Calendar.MINUTE),false).show();
}
};
new DatePickerDialog(EventDetails.this,dateSetListener,calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH)).show();
}
}
活动\事件\详细信息.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>
列出事件.xml
<?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>
事件合同.java
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";
}
}
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);
}
}
暂无答案!
目前还没有任何答案,快来回答吧!