不推荐使用构造函数SimpleCursorAdapter(Context,int,Cursor,String [],int [])

时间:2013-12-23 16:32:50

标签: android simplecursoradapter

如何解决这个问题?

  

构造函数SimpleCursorAdapter(Context,int,Cursor,String [],   不推荐使用int [])

  

方法managedQuery(Uri,String [],String,String [],String)来自   不推荐使用Activity类型

以下是代码:

package com.example.kamusdigital;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class MainActivity extends Activity {

    private TextView munculTulisan;
    private ListView barisanKata;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        munculTulisan = (TextView) findViewById(R.id.tulisanMuncul);
        barisanKata = (ListView) findViewById(R.id.barisDemiBaris);

        mengaturIntent(getIntent());
    }

    @Override
    protected void onNewIntent(Intent intent) {        
        mengaturIntent(intent);
    }

    private void mengaturIntent(Intent intent) {
        if (Intent.ACTION_VIEW.equals(intent.getAction())) {

            Intent agarKataDptDiKlik = new Intent(this, TampilanKata.class);
            agarKataDptDiKlik.setData(intent.getData());
            startActivity(agarKataDptDiKlik);
            finish();
        } else if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
            String query = intent.getStringExtra(SearchManager.QUERY);
            hasilPencarian(query);
        }
    }


    private void hasilPencarian(String query) {

        Cursor cursor = managedQuery(PenghubungData.CONTENT_URI, null, null,
                                new String[] {query}, null);

        if (cursor == null) {
            munculTulisan.setText(getString(R.string.tak_ada_hasil, new Object[] {query}));
        } else {
            int count = cursor.getCount();
            String countString = getResources().getQuantityString(R.plurals.hasil_pencarian,
                                    count, new Object[] {count, query});
            munculTulisan.setText(countString);

            String[] dari = new String[] { KamusDatabase.KATA,
                                           KamusDatabase.ARTI_NYA };

            int[] ke = new int[] { R.id.kolomKata,
                                   R.id.kolomArtinya };

            SimpleCursorAdapter letakanKataPadaTempatnya = new SimpleCursorAdapter(this,
                                          R.layout.hasil, cursor, dari, ke);
            barisanKata.setAdapter(letakanKataPadaTempatnya);

            barisanKata.setOnItemClickListener(new OnItemClickListener() {
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    Intent kataDiKlik = new Intent(getApplicationContext(), TampilanKata.class);
                    Uri data = Uri.withAppendedPath(PenghubungData.CONTENT_URI,
                                                    String.valueOf(id));
                    kataDiKlik.setData(data);
                    startActivity(kataDiKlik);
                }
            });
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater perpanjangTampilan = getMenuInflater();
        perpanjangTampilan.inflate(R.menu.untuk_menu, menu);

        SearchManager mengaturSearch = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchView tampilanSearch = (SearchView) menu.findItem(R.id.cari).getActionView();
        tampilanSearch.setSearchableInfo(mengaturSearch.getSearchableInfo(getComponentName()));
        tampilanSearch.setIconifiedByDefault(false);

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.cari:
                onSearchRequested();
                return true;
            default:
                return false;
        }
    }
}
  

package com.example.kamusdigital;

import android.app.ActionBar;
import android.app.Activity;
import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.SearchView;
import android.widget.TextView;

public class TampilanKata extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.kata);

        ActionBar tempatTampilKata = getActionBar();
        tempatTampilKata.setDisplayHomeAsUpEnabled(true);

        Uri uri = getIntent().getData();
        Cursor cursor = managedQuery(uri, null, null, null, null);

        if (cursor == null) {
            finish();
        } else {
            cursor.moveToFirst();

            TextView kataKata = (TextView) findViewById(R.id.kolomKata);
            TextView artinya = (TextView) findViewById(R.id.kolomArtinya);

            int indexKata = cursor.getColumnIndexOrThrow(KamusDatabase.KATA);
            int indexArtinya = cursor.getColumnIndexOrThrow(KamusDatabase.ARTI_NYA);

            kataKata.setText(cursor.getString(indexKata));
            artinya.setText(cursor.getString(indexArtinya));
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.untuk_menu, menu);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        SearchManager aturPencarian = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchView tampilanPencarian = (SearchView) menu.findItem(R.id.cari).getActionView();
        tampilanPencarian.setSearchableInfo(aturPencarian.getSearchableInfo(getComponentName()));
        tampilanPencarian.setIconifiedByDefault(false);
        }
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.cari:
                onSearchRequested();
                return true;
            case android.R.id.home:
                Intent intent = new Intent(this, MainActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
                return true;
            default:
                return false;
        }
    }
}

3 个答案:

答案 0 :(得分:3)

请注意,弃用不适用于所有SimpleCursorAdapter,只适用于其中一个构造函数。

你可以使用other constructor(如果你正在为api级别开发&gt; 11)

public SimpleCursorAdapter (Context context, int layout, Cursor c, String[] from, int[] to, int flags)

如果您正在开发api级别&lt; 11

  

如果您希望使用API​​ 11中的Loader,您可以拉入   兼容性库(参见:   http://developer.android.com/sdk/compatibility-library.html)。这个   提供与API级别4或更高级别兼容的backport   设备

不推荐使用managedQuery方法,这意味着不应再使用它,并且有可用的更新方法。替换为getContentResolver().query()

cursor = activity.getContentResolver().query(imageUri, proj, null, null, null);

答案 1 :(得分:1)

您使用的简单光标适配器构造函数已弃用。将其更改为:

SimpleCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to, int flags)

另请在此处查看您的managedQuery问题

的解决方案

Deprecated ManagedQuery() issue

答案 2 :(得分:0)

SimpleCursorAdapter已弃用。您应该使用CursorAdapterCursorLoader来管理数据库。

这是关于自定义CursorAdapter的一个很好的教程:link

CursorLoaders的另一个:link