1

我首先要说这是一个两部分的问题。我有一个带有 3 个按钮和一个列表视图的活动。一个按钮打开一个带有日期选择器的对话框片段,按钮文本显示选定的日期。其他 2 个按钮可让您分别前往前/后几天。ListView 应显示与所选日期相对应的记录。

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

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

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

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");     
}

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

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

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");     
}
4

1 回答 1

1

FuncLogbook活动实现日期设置事件的侦听器LogDateDialogFragmentListener

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

//...

然后你需要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");     
}

LogDateDialogFragment然后,您可以在onAttach回调中或使用另一个 setter 方法将活动注册为侦听器。

于 2012-10-24T13:30:31.527 回答