0

I'm using Eclipse ADT to build an Address Book App, and am having issues with some of the code. I am receiving @SuppressWarnings for SimpleCursorAdapter and deactivate.

Add @SuppressWarnings 'depresiation' to onCreate()

Add @SuppressWarnings 'depresiation' to onStop()

How do I fix this?

// AddressBook.java
// Main activity for the Address Book app.
package com.deitel.addressbook;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.CursorAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class AddressBook extends ListActivity 
{
   public static final String ROW_ID = "row_id"; // Intent extra key
   private ListView contactListView; // the ListActivity's ListView
   private CursorAdapter contactAdapter; // adapter for ListView

   // called when the activity is first created
   @Override
   public void onCreate(Bundle savedInstanceState) >
   {
      super.onCreate(savedInstanceState); // call super's onCreate
      contactListView = getListView(); // get the built-in ListView
      contactListView.setOnItemClickListener(viewContactListener);      

      // map each contact's name to a TextView in the ListView layout
      String[] from = new String[] { "name" }; 
      int[] to = new int[] { R.id.contactTextView }; 
      contactAdapter = new SimpleCursorAdapter(AddressBook.this, R.layout.contact_list_item, null, from, to); 
      setListAdapter(contactAdapter); // set contactView's adapter
   } // end method onCreate

   @Override
   protected void onResume() 
   { 
      super.onResume(); // call super's onResume method

       // create new GetContactsTask and execute it 
       new GetContactsTask().execute((Object[]) null); 
    } // end method onResume

   @Override
   protected void onStop() 
   { 
      Cursor cursor = contactAdapter.getCursor(); // get current Cursor

      if (cursor != null) 
         cursor.deactivate(); // deactivate it

      contactAdapter.changeCursor(null); // adapted now has no Cursor
      super.onStop(); 
   } // end method onStop

   // performs database query outside GUI thread
   private class GetContactsTask extends AsyncTask<Object, Object, Cursor> 
   { 
      DatabaseConnector databaseConnector = 
         new DatabaseConnector(AddressBook.this); 

      // perform the database access
      @Override
      protected Cursor doInBackground(Object... params) 
      { 
         databaseConnector.open(); 

         // get a cursor containing call contacts
         return databaseConnector.getAllContacts(); 
      } // end method doInBackground

      // use the Cursor returned from the doInBackground method
      @Override
      protected void onPostExecute(Cursor result) 
      { 
         contactAdapter.changeCursor(result); // set the adapter's Cursor
         databaseConnector.close(); 
      } // end method onPostExecute
   } // end class GetContactsTask

   // create the Activity's menu from a menu resource XML file
   @Override
   public boolean onCreateOptionsMenu(Menu menu) 
   { 
      super.onCreateOptionsMenu(menu); 
      MenuInflater inflater = getMenuInflater(); 
      inflater.inflate(R.menu.addressbook_menu, menu); 
      return true; 
   } // end method onCreateOptionsMenu

   // handle choice from options menu
   @Override
   public boolean onOptionsItemSelected(MenuItem item) 
   { 
      // create a new Intent to launch the AddEditContact Activity
      Intent addNewContact = 
         new Intent(AddressBook.this, AddEditContact.class); 
      startActivity(addNewContact); // start the AddEditContact Activity
      return super.onOptionsItemSelected(item); // call super's method
   } // end method onOptionsItemSelected

   // event listener that responds to the user touching a contact's name
   // in the ListView
   OnItemClickListener viewContactListener = new OnItemClickListener() 
   { 
      @Override
      public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, 
         long arg3) 
      { 
         // create an Intent to launch the ViewContact Activity
         Intent viewContact = 
            new Intent(AddressBook.this, ViewContact.class); 

         // pass the selected contact's row ID as an extra with the Intent
         viewContact.putExtra(ROW_ID, arg3); 
         startActivity(viewContact); // start the ViewContact Activity
      } // end method onItemClick
   }; // end viewContactListener
} // end class AddressBook
Cœur
  • 37,241
  • 25
  • 195
  • 267
  • please read [how to write proper format](http://stackoverflow.com/editing-help) question – Baby Apr 02 '14 at 01:22

2 Answers2

0

Please refer this SO questions for the list of all warning eclipse provide.

and to answer your question. You do have to add the @SuppressWarnings before that method.

'unchecked' and 'deprecation' are required by the Java Language Specification, and so should be valid with all compilers.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Ashish
  • 14,295
  • 21
  • 82
  • 127
0

The deprecation warning means that the class, method, or variable you used has been marked by the author of the library as not suitable for future use, and you should avoid using the item in question. The documentation usually specifies what the problem with it was and what to use instead. For example, the doc for SimpleCursorAdapter says:

This constructor was deprecated in API level 11. This option is discouraged, as it results in Cursor queries being performed on the application's UI thread and thus can cause poor responsiveness or even Application Not Responding errors. As an alternative, use LoaderManager with a CursorLoader.

If there's a situation where you absolutely have to use a deprecated feature, you can simply add the @SuppressWarnings("deprecation") to the line with the warning, but you should avoid doing so if at all possible.

chrylis -cautiouslyoptimistic-
  • 75,269
  • 21
  • 115
  • 152