0

我想使用API​​ level-8中的Content ProviderContent Observer。我能夠在Api-15(ICS)中創建它。但我必須從Api-8支持我的應用程序。如何在API Level 8中使用Content Provider和Content Observer?

如果我提到android:minSdkVersion =「8」像這樣我得到這個錯誤 Class requires API level 11 (current min is 8): android.content.Loader

import android.app.ListActivity; 
import android.content.ContentValues; 
import android.support.v4.app.LoaderManager; 
import android.support.v4.content.CursorLoader; 
import android.support.v4.content.Loader; 
import android.content.Intent; 
import android.database.Cursor; 
import android.net.Uri; 
import android.os.Bundle; 
import android.view.ContextMenu; 
import android.view.ContextMenu.ContextMenuInfo; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.AdapterView.AdapterContextMenuInfo; 
import android.widget.EditText; 

public class TodosOverviewActivity extends ListActivity implements  LoaderManager.LoaderCallbacks<Cursor> { 

private static final int DELETE_ID = Menu.FIRST + 1;  
private ConverstationsAdapter adapter; 
private EditText Enter_Text; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.todo_list); 
    this.getListView().setDividerHeight(2); 
    fillData(); 
    registerForContextMenu(getListView()); 
    Enter_Text = (EditText) findViewById(R.id.msg); 

    findViewById(R.id.send_button).setOnClickListener(new OnClickListener() {   
     @Override 
     public void onClick(View v) { 
      SendInfo(); 
     } 
    }); 
} 

private void SendInfo() { 

    String msg = Enter_Text.getText().toString().trim(); 
    ContentValues values = new ContentValues(); 
    values.put(TodoTable.COLUMN_RECEIVER, "Reddy"); 
    values.put(TodoTable.COLUMN_SENDER, "RA"); 
    values.put(TodoTable.COLUMN_MSG, msg); 

    getContentResolver().insert(MyTodoContentProvider.CONTENT_URI, values); 
} 

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

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

private void fillData() { 
    getLoaderManager().initLoader(0, null, this); // error here 
    adapter = new ConverstationsAdapter(TodosOverviewActivity.this, null); 
    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_MSG }; 
    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); 
} 
} 
+0

我覺得問題應該是如何使用'Loader'在'API8'因爲沒有錯與'ContentProvider'和'ContentObserver'。 –

+0

雅它在API-15中工作正常我想從Minsdk -8支持我的應用程序; –

回答

0

考慮使用android.support.v4.content.Loader

編輯

import android.support.v4.app.FragmentActivity; 
import android.support.v4.app.LoaderManager; 
import android.support.v4.content.CursorLoader; 
import android.support.v4.content.Loader; 

... 

public class YourClass implements LoaderManager.LoaderCallbacks<Cursor> ... 
... 

@Override public Loader<Cursor> onCreateLoader(int id, Bundle args) { 
    String[] projection = { TodoTable.COLUMN_ID, TodoTable.COLUMN_MSG }; 
    CursorLoader cursorLoader = new CursorLoader(this, 
     MyTodoContentProvider.CONTENT_URI, projection, null, null, null); 
    return cursorLoader; 
} 

講究LoaderManager.LoaderCallbacks < 光標>


編輯
以任何方式閱讀本CursorLoader tutorial,至於我 - 我喜歡restartLoader()不initLoader()作爲教程。


編輯
重寫你的活動與碎片的幫助(有線版):

包de.vogella.android.todos;

import android.database.Cursor; 
import android.os.Bundle; 
import android.support.v4.app.FragmentActivity; 
import android.support.v4.app.FragmentManager; 
import android.support.v4.app.FragmentTransaction; 
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.widget.EditText; 

public class TodosOverviewActivity extends FragmentActivity { 

public static class TodosOverviewListFragment extends ListFragment 
     implements LoaderManager.LoaderCallbacks<Cursor> { 
    private ConverstationsAdapter adapter; 
    private EditText Enter_Text; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     getLoaderManager().initLoader(0, null, this); 
     adapter = new ConverstationsAdapter(getActivity(), null); 
     setListAdapter(adapter); 
    } 

    public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) { 
     String[] projection = { TodoTable.COLUMN_ID, TodoTable.COLUMN_MSG }; 
     CursorLoader cursorLoader = new CursorLoader(getActivity(), 
       MyTodoContentProvider.CONTENT_URI, projection, null, null, 
       null); 
     return cursorLoader; 
    } 

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

    public void onLoaderReset(Loader<Cursor> arg0) { 
     adapter.swapCursor(null); 
    } 
} 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    FragmentManager fragmentManager = getSupportFragmentManager(); 
    FragmentTransaction fragmentTransaction = fragmentManager 
      .beginTransaction(); 
    TodosOverviewListFragment fragment = new TodosOverviewListFragment(); 
    fragmentTransaction.add(fragment, "xx"); 
    fragmentTransaction.commit(); 
} 
} 

清單: uses-sdk android:minSdkVersion="8" android:targetSdkVersion="15"


添加支持包項目(項目 - > Android的工具)

+0

我被導入'android .support.v4.content.CursorLoader; android.support.v4.content.Loader;',仍然出現此錯誤返回類型與LoaderManager.LoaderCallbacks不兼容 .onCreateLoader(int,Bundle)' –

+0

您是否在談論android.support.v4.app .LoaderManager和它的東西? –

+0

也有問題,我用代碼和錯誤更新了我的問題。 –

0

兩個contentobservercontentprovider存在於API 8.所以,你可以使用他們沒有任何問題。 看到您的錯誤,您正在嘗試使用Loader。您可以安全地使用AsyncTask並在doinbackground中執行遊標加載,而不是使用Loader。裝載機是在API推出了11

所以刪除android.content.Loader的使用情況,你應該能夠建立

+0

是否可以使用API​​-8的Loader。 –

+0

像ra有說使用[支持庫](http://developer.android.com/tools/extras/support-library.html),你可以導入android.support.v4.content.Loader – nandeesh

相關問題