Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The constructor SimpleCursorAdapter(Context, int, Cursor, String[], int[]) is deprecated

How to fix this problem?

The constructor SimpleCursorAdapter(Context, int, Cursor, String[], int[]) is deprecated

and

The method managedQuery(Uri, String[], String, String[], String) from the type Activity is deprecated

Here's the code :

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;
        }
    }
}
like image 820
user3102141 Avatar asked Oct 02 '22 01:10

user3102141


1 Answers

Please note that the deprecation does not apply to all of SimpleCursorAdapter, just one of the constructors.

you can use the other constructor (if you are developing for api level >11)

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

if you are developing for api level < 11

If you wish to use the Loader from API 11, you can pull in the compatibility library (see: http://developer.android.com/sdk/compatibility-library.html). This provides a backport that is compatible with API level 4 or higher devices.

also

The managedQuery method is deprecated, meaning it should no longer be used and there is an updated method available. The replacement for this is getContentResolver().query():

cursor = activity.getContentResolver().query(imageUri, proj, null, null, null);
like image 172
gaurav5430 Avatar answered Oct 19 '22 23:10

gaurav5430