gpt4 book ai didi

android - 根据日期选择器更改从数据库重新加载 ListView 内容

转载 作者:行者123 更新时间:2023-11-30 03:58:03 36 4
gpt4 key购买 nike

首先我会说这是一个由两部分组成的问题。我有一个带有 3 个按钮和一个 ListView 的 Activity 。一个按钮打开带有日期选择器的对话框 fragment ,按钮文本显示所选日期。其他 2 个按钮可让您分别转到前几天/后几天。 ListView 应显示与所选日期对应的记录。

目前,加载 Activity 时,按钮显示今天的日期, ListView 显示数据库中今天的记录。我设法创建了一个重新启动加载程序并在上一个/下一个按钮中调用它的方法,因此当一次导航一天时, ListView 会更新以显示正确的记录,但是当我通过调用日期选择器更改日期时对话框,我似乎找不到让它正确更新的方法 - 这是问题的第一部分。

第二部分有点难解释。当我选择一个有 4 条记录( ListView 中的 4 行)的日期时,所有 4 条记录都会显示,但是例如,如果我转到只有 2 条记录( ListView 中的 2 行)的第二天,它将正确显示第一个2 条记录,但接下来的 2 行仍然显示前一个日期的数据。为了更好地解释这一点,让我们假设 ListView 中的每一行都显示 DB 记录的 _id 号。在日期 1(有 4 条记录)上,我显示 ID 1、2、3、4,但是当更改为日期 2(有 2 条记录)时,它显示 ID 5、6、3、4。

我假设通过采用一种完全清除 ListView 内容然后重新填充新日期的方法,我可以同时解决这两个问题。我只是不确定该怎么做。我的代码如下:

public class FuncLogbook extends ListActivity implements
LoaderManager.LoaderCallbacks<Cursor>, OnItemClickListener {

Button btn_logbook_date;

LogDateDialogFragment fragDate;
Calendar now;
Date nowD, newD, PrevDate, NextDate;

int lMonth;
String strDate;

private SimpleCursorAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView (R.layout.dash_logbook);
this.getListView().setDividerHeight(2);

ListView list = getListView();
list.setOnItemClickListener(this);

btn_logbook_date = (Button) findViewById(R.id.btn_logbook_date);
now = Calendar.getInstance();
nowD = now.getTime();

SimpleDateFormat dFormatter = new SimpleDateFormat("dd MMM yyyy");
strDate = dFormatter.format(nowD);
btn_logbook_date.setText(strDate);

fillData();
}

public void modifyDate(View v) {
int id = v.getId ();
switch (id) {
case R.id.btn_logbook_date :
showDialogDate();
break;
case R.id.btn_logbook_prev :
updateDatePrev();
break;
case R.id.btn_logbook_next :
updateDateNext();
break;
}
}

public interface LogDateDialogFragmentListener {
public void logbookChangedDate(int year, int month, int day);
}

public void showDialogDate() {
FragmentTransaction ftLogDate = getFragmentManager().beginTransaction();
fragDate = LogDateDialogFragment.newInstance(this, new LogDateDialogFragmentListener() {
public void logbookChangedDate(int year, int month, int day) {
now.set(Calendar.YEAR, year);
now.set(Calendar.MONTH, month);
now.set(Calendar.DAY_OF_MONTH, day);
newD = now.getTime();
SimpleDateFormat newDFormatter = new SimpleDateFormat("dd MMM yyyy");
strDate = newDFormatter.format(newD);
btn_logbook_date.setText(strDate);
}}, now);
fragDate.show(ftLogDate, "DateDialogFragment");
}

public void updateDatePrev() {
String prevVar[] = btn_logbook_date.getText().toString().split(" ");

if (prevVar[1].equalsIgnoreCase("Jan")) {
lMonth = 0;
} else if (prevVar[1].equalsIgnoreCase("Feb")) {
lMonth = 1;
} else if (prevVar[1].equalsIgnoreCase("Mar")) {
lMonth = 2;
} else if (prevVar[1].equalsIgnoreCase("Apr")) {
lMonth = 3;
} else if (prevVar[1].equalsIgnoreCase("May")) {
lMonth = 4;
} else if (prevVar[1].equalsIgnoreCase("Jun")) {
lMonth = 5;
} else if (prevVar[1].equalsIgnoreCase("Jul")) {
lMonth = 6;
} else if (prevVar[1].equalsIgnoreCase("Aug")) {
lMonth = 7;
} else if (prevVar[1].equalsIgnoreCase("Sep")) {
lMonth = 8;
} else if (prevVar[1].equalsIgnoreCase("Oct")) {
lMonth = 9;
} else if (prevVar[1].equalsIgnoreCase("Nov")) {
lMonth = 10;
} else if (prevVar[1].equalsIgnoreCase("Dec")) {
lMonth = 11;
}

int lYear = Integer.parseInt(prevVar[2]);
int lDay = Integer.parseInt(prevVar[0]);
now = Calendar.getInstance();
now.set(Calendar.YEAR, lYear);
now.set(Calendar.MONTH, lMonth);
now.set(Calendar.DAY_OF_MONTH, lDay - 1);
PrevDate = now.getTime();
SimpleDateFormat prevDFormatter = new SimpleDateFormat("dd MMM yyyy");
strDate = prevDFormatter.format(PrevDate);
btn_logbook_date.setText(strDate);

refillData();
}

public void updateDateNext() {
// Same as updateDatePrev() method, but moves forward 1 day, instead of backward
}

protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
}

private void fillData() {
String[] from = new String[] { AddDBHelper.KEY_BGL, AddDBHelper.KEY_ROWID,
AddDBHelper.KEY_CATEG, AddDBHelper.KEY_TIME };
int[] to = new int[] {R.id.logBGL, R.id.logRowID, R.id.logCateg, R.id.logTime };

getLoaderManager().initLoader(0, null, this);
adapter = new SimpleCursorAdapter(this, R.layout.logbook_item, null, from, to, 0);

setListAdapter(adapter);
}

private void refillData() {
getLoaderManager().restartLoader(0, null, this);
}

public Loader<Cursor> onCreateLoader(int id, Bundle args) {
String selection = AddDBHelper.KEY_DATE + "=?";
String[] selectionArgs = { String.valueOf(btn_logbook_date.getText().toString()) };

CursorLoader cl = new CursorLoader(this, DBProvider.CONTENT_URI,
null, selection, selectionArgs, null);
return cl;
}

public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
adapter.swapCursor(data);
}

public void onLoaderReset(Loader<Cursor> loader) {
// data is not available anymore; delete reference
adapter.swapCursor(null);
}
}

更新

显示来自 ContentProvider 的查询信息:

@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
String sortOrder) {

// Using SQLiteQueryBuilder instead of query() method
SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();

// Set the DB table to query
queryBuilder.setTables(AddDBHelper.DB_TABLE);

int uriType = sURIMatcher.match(uri);
switch (uriType) {
case DBAllEntries:
//no filter or WHERE clause
break;
case DBSingleEntry:
//add ROWID filter when selecting individual item
queryBuilder.appendWhere(AddDBHelper.KEY_ROWID + "="
+ uri.getLastPathSegment());
break;
default:
throw new IllegalArgumentException("Unknown URI: " + uri);
}

SQLiteDatabase sqlDB = mDBP.getWritableDatabase();
Cursor cursor = queryBuilder.query(sqlDB, projection, selection,
selectionArgs, null, null, sortOrder);
// Ensure potential listeners are getting notified
cursor.setNotificationUri(getContext().getContentResolver(), uri);

return cursor;
}

更新

下面对 refillData() 的调用在第一次日期更改时起作用,但没有再次起作用。重新启动模拟器并再次尝试后,我能够看到 2 个单独日期更改的记录,但随后它不会再次更改数据。

public void showDialogDate() {
FragmentTransaction ftLogDate = getFragmentManager().beginTransaction();
fragDate = LogDateDialogFragment.newInstance(this, new LogDateDialogFragmentListener() {
public void logbookChangedDate(int year, int month, int day) {
now.set(Calendar.YEAR, year);
now.set(Calendar.MONTH, month);
now.set(Calendar.DAY_OF_MONTH, day);
newD = now.getTime();
SimpleDateFormat newDFormatter = new SimpleDateFormat("dd MMM yyyy");
strDate = newDFormatter.format(newD);
btn_logbook_date.setText(strDate);

refillData();

}}, now);
fragDate.show(ftLogDate, "DateDialogFragment");
}

通过下面的调用,日期更改后数据不会更改,但是当我再次按下按钮打开日期选择器时,我可以在后台(对话框 fragment 后面)看到 ListView 更新。比如我选择Date 2,数据没有变化。我打开日期选择器,日期 2 数据显示在对话框后面。我将日期选择器设置为日期 3,但 ListView 仍显示日期 2 的数据。如果再次打开日期选择器,日期 3 的数据会在对话框 fragment 后面的后台加载。

如果我在 FragDate.show(...) 行之后或 FragmentTransaction ftLogDate... 之前调用 refillData(),我会得到相同的结果。 行。

public void showDialogDate() {
FragmentTransaction ftLogDate = getFragmentManager().beginTransaction();
fragDate = LogDateDialogFragment.newInstance(this, new LogDateDialogFragmentListener() {
public void logbookChangedDate(int year, int month, int day) {
now.set(Calendar.YEAR, year);
now.set(Calendar.MONTH, month);
now.set(Calendar.DAY_OF_MONTH, day);
newD = now.getTime();
SimpleDateFormat newDFormatter = new SimpleDateFormat("dd MMM yyyy");
strDate = newDFormatter.format(newD);
btn_logbook_date.setText(strDate);
}}, now);
refillData();
fragDate.show(ftLogDate, "DateDialogFragment");
}

最佳答案

FuncLogbook Activity 实现日期设置事件的监听器,LogDateDialogFragmentListener:

public class FuncLogbook extends ListActivity implements
LoaderManager.LoaderCallbacks<Cursor>, OnItemClickListener, LogDateDialogFragmentListener {

//...

然后您需要在 Activity 中实现logbookChangedDate:

@Override
public void logbookChangedDate(int year, int month, int day) {
now.set(Calendar.YEAR, year);
now.set(Calendar.MONTH, month);
now.set(Calendar.DAY_OF_MONTH, day);
newD = now.getTime();
SimpleDateFormat newDFormatter = new SimpleDateFormat("dd MMM yyyy");
strDate = newDFormatter.format(newD);
btn_logbook_date.setText(strDate);
refillData();
}

显示对话框的方法是:

public void showDialogDate() {
FragmentTransaction ftLogDate = getFragmentManager().beginTransaction();
fragDate = LogDateDialogFragment.newInstance(this, now);
fragDate.show(ftLogDate, "DateDialogFragment");
}

然后您可以在 onAttach 回调中的 LogDateDialogFragment 中或使用其他 setter 方法将该 Activity 注册为监听器。

关于android - 根据日期选择器更改从数据库重新加载 ListView 内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13031983/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com