2013-01-06 27 views
5

मैं लार्स Vogella से ट्यूटोरियल अनुसरण कर रहा हूं: http://www.vogella.com/articles/AndroidSQLite/article.html#todoएपीआई में लोडर का उपयोग करते हुए 8

लेकिन मैं इसे एपीआई 8 पर काम करने के लिए मैं इसे एपीआई पर काम कर 11+ और मिल चाहते थे उस पर काम कर पाने के लिए निम्नलिखित एपीआई 8 एंड्रॉयड उपकरण में मैं एक समर्थन लाइब्रेरी जोड़ा गया है और मैं निम्नलिखित आयात कहा:

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; 

लेकिन फिर भी ग्रहण मुझे त्रुटि देता है:

The method getLoaderManager() is undefined for the type TodosOverviewActivity 

विधि में:

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

क्या कोई इस त्रुटि पर कुछ प्रकाश डाल सकता है?


मैं निम्नलिखित 'समाधान' पाया, लेकिन मुझे यकीन है कि कैसे ट्यूटोरियल में इस लागू करने के लिए नहीं कर रहा हूँ, इस getSupportLoaderManager (रखने के लिए समाधान है) और ListView संबंधित तरीके उपलब्ध हैं और यदि हां, तो मैं कैसे करना है लागू इस सही ढंग से ?:

public class TodosOverviewActivity extends FragmentActivity { 

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

कुल वर्ग:

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

क्या आपकी गतिविधि 'फ्रैगमेंट एक्टिविटी' का विस्तार कर रही है? –

उत्तर

2

समस्या का उपयोग करना चाहिए कि आप 'ListFragment' के बजाय 'ListActivity' को विस्तारित कर रहे हैं।

यदि आप एक सूचीफलक का लाभ उठाते हैं, तो आपको समर्थन लाइब्रेरी से getLoaderManager() तक पहुंच प्राप्त होगी।

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

धन्यवाद, अगर मैं इसे सूचीफ्रेममेंट के साथ विस्तारित करता हूं तो मुझे exampleContentView उदाहरण के लिए उपयोग नहीं है। अगर मैं FragmentActivity के साथ विस्तार करता हूं तो मुझे ListView विधियों तक पहुंच नहीं है। कोई चाल है? – Diego

+0

यदि आपका लेआउट सुपर वूपर सरल नहीं है तो ListFragment या List Activities का उपयोग न करें। ऐसे मामलों में, सामान्य गतिविधि या खंड का उपयोग करें और स्वयं द्वारा ListView जोड़ें। – Snicolas

1

TodosOverview गतिविधि को फ्रैगमेंट एक्टिविटी का विस्तार करना चाहिए और आपको getSupportLoaderManager()

+0

हाय, उदाहरण के लिए setListAdapter() के लिए मैं तब तक पहुंच कैसे प्राप्त करूं? – Diego