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

前端之家收集整理的这篇文章主要介绍了android – 不推荐使用构造函数SimpleCursorAdapter(Context,int,Cursor,String [],int [])前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决这个问题?

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

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

这是代码

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

        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;
        }
    }
}
最佳答案
请注意,弃用不适用于所有SimpleCursorAdapter,只适用于其中一个构造函数.

您可以使用other constructor(如果您正在开发api级别> 11)

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

如果你正在开发api级别< 11

If you wish to use the Loader from API 11,you can pull in the
compatibility library (see:
07001). This
provides a backport that is compatible with API level 4 or higher
devices.

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

cursor = activity.getContentResolver().query(imageUri,proj,null);
原文链接:https://www.f2er.com/android/430979.html

猜你在找的Android相关文章