0

我想从 Api level-8Content Provider开始使用。Content Observer我可以在 Api-15 (ICS) 中创建它。但我必须从 Api-8 支持我的应用程序。

如果我像这样提到 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);
}
}
4

2 回答 2

0

两者contentobservercontentprovider存在于 api 8 中。因此您可以毫无问题地使用它们。看到您的错误,您正在尝试使用Loader. 您可以安全地AsyncTask在 doinbackground 中使用和执行光标加载,而不是使用 Loader。加载程序是在 API 11 中引入的

所以删除 android.content.Loader 的使用,你应该能够构建

于 2012-10-06T07:19:38.120 回答
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 教程,对我来说 - 我更喜欢 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 工具)

于 2012-10-06T08:06:15.977 回答