2013-04-26 42 views
0

總結:我確實有一個簡單的應用程序(演示/原型)與顯示項目列表(這裏是客戶)的活動。這些項目從應用程序SQLite數據庫中檢索。我正在使用ContentProvider方法,並使用LoaderManagerSimpleCursorAdapter。我需要將用戶的菜單項選擇轉換爲選擇的列表排序方式。通常的做法是什麼?未來應如何保存用戶選擇? (我關於Android的編程初學者。)如何通過菜單更改活動中列表項目的順序?

詳情:在我的活動onCreate方法,該方法fillData(見下面的代碼,從教程中學)被調用來填充列表。它調用getLoaderManager().initLoader(0, null, this);,這又導致調用返回CursorLoader實例的onCreateLoader。遊標加載器使用內容提供者並傳遞定義排序的參數。到目前爲止,我正在使用一個固定的參數來排序列表。我的猜測是,在處理菜單項點擊時,我應該調用fillData();。它應該導致創建另一個加載器和另一個適配器但如何將這些信息傳遞給onCreateLoader

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
    case R.id.insert_customer: // this already works for me 
     createCustomer(); 
     return true; 

    case R.id.customers_orderby_name_asc: 
     ???      // What should be here? 
     fillData();    // I should probably call this. 
     return true: 

    case R.id.customers_orderby_name_desc: 
     ??? 
     fillData(); 
     return true: 
    } 
    return super.onOptionsItemSelected(item); 
} 
... 
private void fillData() { 
    String[] from = new String[] { CustomerTable.COLUMN_CODE, 
            CustomerTable.COLUMN_NAME, 
            CustomerTable.COLUMN_TOWN, 
            CustomerTable.COLUMN_STREET}; 
    int[] to = new int[] { R.id.code, R.id.name, R.id.town, R.id.street }; 

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

// After initLoader()... 
@Override 
public Loader<Cursor> onCreateLoader(int id, Bundle args) { 
    String[] projection = { CustomerTable.COLUMN_ID, 
          CustomerTable.COLUMN_CODE, 
          CustomerTable.COLUMN_NAME, 
          CustomerTable.COLUMN_STREET, 
          CustomerTable.COLUMN_TOWN }; 
    CursorLoader cursorLoader = new CursorLoader(this, 
      DemoContentProvider.CUSTOMERS_CONTENT_URI, projection, null, null, 
      CustomerTable.COLUMN_NAME); // here fixed order by the column 
    return cursorLoader; 
} 

回答

1

它可以是您活動中的局部變量。

列名可以寫在SharedPrefs中供以後使用(或立即使用)。

如果您將LoaderCallbacks移動到您的活動之外的類中,則可以將其設置爲LoaderCallbacks實現的字段,並在構造函數或設置器中傳遞它。

+0

+1了SharedPreferences - 這是我想要的,但我不知道:)在活動之外移動LoaderCallbacks有什麼好處嗎?到目前爲止,我使用'公共類CustomersOverviewActivity擴展ListActivity實現LoaderManager.LoaderCallbacks ',即使使用私有變量,它也能正常工作。 – pepr 2013-04-26 13:16:29

+0

如果你的LoaderCallbacks做了很多事情,並且如果你想明確區分,那麼把它放在你的活動之外是有道理的。 – njzk2 2013-04-26 15:13:17

+0

感謝您的想法。 – pepr 2013-04-26 19:39:09

1

此CustomerTable.COLUMN_NAME意味着訂購,對嗎?因此,創建幾種方法與像

在這個地方唯一的變化創建一個將得到排序列作爲參數的方法:

public Loader<Cursor> onCreateLoader(int id, Bundle args, String orderByColumn) 
... 
    CursorLoader cursorLoader = new CursorLoader(this, 
       DemoContentProvider.CUSTOMERS_CONTENT_URI, projection, null, null, 
       orderByColumn == null ? CustomerTable.COLUMN_NAME : orderByColumn); 

,或者使用一些類的領域像

... 
private String orderByColumn; 
... 
    public Loader<Cursor> onCreateLoader(int id, Bundle args) 
    ... 
     CursorLoader cursorLoader = new CursorLoader(this, 
        DemoContentProvider.CUSTOMERS_CONTENT_URI, projection, null, null, 
        orderByColumn == null ? CustomerTable.COLUMN_NAME : orderByColumn); 
+0

+1感謝您的私人字符串變量提示。 – pepr 2013-04-26 13:17:06