2013-01-06 42 views
5

Tôi đang theo hướng dẫn từ Lars Vogella: http://www.vogella.com/articles/AndroidSQLite/article.html#todoSử dụng Loader trong API 8

Nhưng tôi muốn nó làm việc trên API 8. Tôi đã nhận nó làm việc trên API 11 + và đã làm như sau để làm cho nó làm việc trên API 8. Tại Công cụ Android tôi đã thêm một thư viện hỗ trợ và tôi thêm vào nhập khẩu như sau:

import android.support.v4.app.ListFragment; 
import android.support.v4.app.LoaderManager; 
import android.support.v4.content.CursorLoader; 
import android.support.v4.content.Loader; 
import android.support.v4.widget.CursorAdapter; 
import android.support.v4.widget.SimpleCursorAdapter; 

Nhưng vẫn Eclipse mang lại cho tôi những lỗi:

The method getLoaderManager() is undefined for the type TodosOverviewActivity 

Trong phương pháp này:

01.235.
private void fillData() { 

    // Fields from the database (projection) 
    // Must include the _id column for the adapter to work 
    String[] from = new String[] { TodoTable.COLUMN_SUMMARY }; 
    // Fields on the UI to which we map 
    int[] to = new int[] { R.id.label }; 

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

    setListAdapter(adapter); 
    } 

Ai cũng có thể làm sáng tỏ lỗi này?


Tôi tìm thấy sau 'giải pháp' nhưng tôi không chắc chắn làm thế nào để thực hiện điều này trong hướng dẫn, đây là giải pháp cho có getSupportLoaderManager() và phương pháp ListView liên quan có sẵn và nếu như vậy, làm thế nào tôi thực hiện điều này một cách chính xác ?:

public class TodosOverviewActivity extends FragmentActivity { 

public static class TodosOverviewListFragment extends ListFragment 
     implements LoaderManager.LoaderCallbacks<Cursor> { 

tổng class:

public class TodosOverviewActivity extends FragmentActivity implements 
    LoaderManager.LoaderCallbacks<Cursor> { 
    private static final int ACTIVITY_CREATE = 0; 
    private static final int ACTIVITY_EDIT = 1; 
    private static final int DELETE_ID = Menu.FIRST + 1; 
    // private Cursor cursor; 
    private SimpleCursorAdapter adapter; 


/** Called when the activity is first created. */ 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.todo_list); 
    this.getListView().setDividerHeight(2); 
    fillData(); 
    registerForContextMenu(getListView()); 
    } 

    // Create the menu based on the XML defintion 
    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
    MenuInflater inflater = getMenuInflater(); 
    inflater.inflate(R.menu.listmenu, menu); 
    return true; 
    } 

    // Reaction to the menu selection 
    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
    case R.id.insert: 
     createTodo(); 
     return true; 
    } 
    return super.onOptionsItemSelected(item); 
    } 

    @Override 
    public boolean onContextItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
    case DELETE_ID: 
     AdapterContextMenuInfo info = (AdapterContextMenuInfo) item 
      .getMenuInfo(); 
     Uri uri = Uri.parse(MyTodoContentProvider.CONTENT_URI + "/" 
      + info.id); 
     getContentResolver().delete(uri, null, null); 
     fillData(); 
     return true; 
    } 
    return super.onContextItemSelected(item); 
    } 

    private void createTodo() { 
    Intent i = new Intent(this, TodoDetailActivity.class); 
    startActivityForResult(i, ACTIVITY_CREATE); 
    } 

    // Opens the second activity if an entry is clicked 
    @Override 
    protected void onListItemClick(ListView l, View v, int position, long id) { 
    super.onListItemClick(l, v, position, id); 
    Intent i = new Intent(this, TodoDetailActivity.class); 
    Uri todoUri = Uri.parse(MyTodoContentProvider.CONTENT_URI + "/" + id); 
    i.putExtra(MyTodoContentProvider.CONTENT_ITEM_TYPE, todoUri); 

    // Activity returns an result if called with startActivityForResult 
    startActivityForResult(i, ACTIVITY_EDIT); 
    } 



    private void fillData() { 

    // Fields from the database (projection) 
    // Must include the _id column for the adapter to work 
    String[] from = new String[] { TodoTable.COLUMN_SUMMARY }; 
    // Fields on the UI to which we map 
    int[] to = new int[] { R.id.label }; 

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

    setListAdapter(adapter); 
    } 

    @Override 
    public void onCreateContextMenu(ContextMenu menu, View v, 
     ContextMenuInfo menuInfo) { 
    super.onCreateContextMenu(menu, v, menuInfo); 
    menu.add(0, DELETE_ID, 0, R.string.menu_delete); 
    } 

    // Creates a new loader after the initLoader() call 
    @Override 
    public Loader<Cursor> onCreateLoader(int id, Bundle args) { 
    String[] projection = { TodoTable.COLUMN_ID, TodoTable.COLUMN_SUMMARY }; 
    CursorLoader cursorLoader = new CursorLoader(this, 
     MyTodoContentProvider.CONTENT_URI, projection, null, null, null); 
    return cursorLoader; 
    } 

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

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

} 
+1

Hoạt động của bạn có đang mở rộng 'FragmentActivity' không? –

Trả lời

2

Vấn đề là bạn đang mở rộng 'ListActivity' thay vì 'ListFragment'.

Nếu bạn sử dụng ListFragment, bạn sẽ có quyền truy cập vào getLoaderManager() từ thư viện hỗ trợ.

public class TodosOverviewActivity extends ListFragment { 
    private void fillData() { 
    getLoaderManager().initLoader(...); 
    } 
} 
+0

Cảm ơn, nếu tôi mở rộng nó với ListFragment tôi không có quyền truy cập vào ví dụ setContentView. Nếu tôi mở rộng với FragmentActivity, tôi không có quyền truy cập vào các phương thức ListView. Bất kỳ thủ đoạn nào? – Diego

+0

không sử dụng ListFragment hoặc List Activities nếu bố cục của bạn không phải là siêu đơn giản. Trong những trường hợp như vậy, hãy sử dụng một hoạt động hoặc phân đoạn bình thường và tự thêm ListView. – Snicolas

1

TodosOverview Hoạt động nên mở rộng FragmentActivity và bạn nên sử dụng getSupportLoaderManager()

+0

Xin chào, làm cách nào để tôi có quyền truy cập vào ví dụ setListAdapter()? – Diego

Các vấn đề liên quan