FileDocCategorySizeDatePackage
MyRssReader4.javaAPI DocAndroid 1.5 API7993Wed May 06 22:41:08 BST 2009com.example.codelab.rssexample

MyRssReader4

public class MyRssReader4 extends android.app.Activity

Fields Summary
android.widget.ListView
mRssList
android.database.Cursor
mCur
RssCursorAdapter
mAdap
private static final int
ADD_ELEMENT_REQUEST
Constructors Summary
Methods Summary
protected voidonActivityResult(int requestCode, int resultCode, android.content.Intent data)

        if(resultCode == RESULT_OK){
            switch (requestCode){
                case ADD_ELEMENT_REQUEST:
                    ContentValues vals = new ContentValues(4);
                    vals.put(RssContentProvider.TITLE, data.getStringExtra(RssContentProvider.TITLE));
                    vals.put(RssContentProvider.URL, data.getStringExtra(RssContentProvider.URL));
                    vals.put(RssContentProvider.CONTENT, data.getStringExtra(RssContentProvider.CONTENT));
                    vals.put(RssContentProvider.LAST_UPDATED, data.getIntExtra(RssContentProvider.LAST_UPDATED, 0));
                    Uri uri = getContentResolver().insert(
                            RssContentProvider.CONTENT_URI, 
                            vals);
                        if(uri != null){
                            mRssList.setSelection(mRssList.getCount() - 1);
                        }
                    break;
                default:
                    break;
            }
        }
    
public voidonCreate(android.os.Bundle savedInstanceState)

    
    
       
        super.onCreate(savedInstanceState);
        
        // Load screen layout.
        setContentView(R.layout.main_screen2);
       
         // Populate ArrayAdapter and bind it to ListView
        mRssList = (ListView)findViewById(R.id.rssListView);
        
        mCur = managedQuery(RssContentProvider.CONTENT_URI, // Query for all items.
                            null, 
                            null, 
                            RssContentProvider.DEFAULT_SORT_ORDER);
// BEGIN_INCLUDE(4_1)                           
        mAdap = new RssCursorAdapter(
                this,
                R.layout.list_element,                  // Our layout resource.
                mCur, 
                new String[]{RssContentProvider.TITLE}, // Columns to retrieve.
                new int[]{R.id.list_item});             // IDs of widgets to display
        mRssList.setAdapter(mAdap);                     //    the corresponding column.
// END_INCLUDE(4_1)
        
        // Set the last selected item.
        // (icicle is only set if this is being restarted).
        if(savedInstanceState != null && savedInstanceState.containsKey("lastIndexItem")){
            mRssList.setSelection(savedInstanceState.getInteger("lastIndexItem"));
        }
    
public booleanonCreateOptionsMenu(android.view.Menu menu)

        // Always call the superclass implementation to 
        // provide standard items.
        super.onCreateOptionsMenu(menu);
        
        menu.add(0, 0, R.string.menu_option_start, null);
        menu.add(0, 1, R.string.menu_option_stop, null);
        menu.add(0, 2, R.string.menu_option_add, null);
        menu.add(0, 3, R.string.menu_option_delete, null);
        menu.add(0, 4, R.string.menu_option_update, null);
        
        return true;
    
public booleanonOptionsItemSelected(Menu.Item item)

        super.onOptionsItemSelected(item);
        
        switch (item.getId()){
            case 0:     // Start service
                showAlert(null, "You clicked 'start'!", "ok", null, false, null);
                break;
            case 1:    // Stop service
              showAlert(null, "You clicked stop!", "ok", null, false, null);
              break;                    
            case 2:     // Add Item
                Intent addIntent = new Intent(AddRssItem.class);

                // Use an ID so that if we create a "remove item" form we
                // can tell which form is returning a value.
                startActivityForResult(addIntent, ADD_ELEMENT_REQUEST); 
                break; 
            case 3:     // Delete item.
                if(mRssList.hasFocus()){
                  int currentSelectionIndex = mRssList.getSelectedItemIndex();

                  // Create our content URI by adding the ID of the currently selected item using a 
                  // convenience method.
                  Long itemID = mAdap.getItemId(currentSelectionIndex);
                  getContentResolver().delete(RssContentProvider.CONTENT_URI.addId(itemID), null);
                }
                break;
            case 4:    // Update all
                showAlert(null, "You clicked 'Update'!", "ok", null, false, null);
                break;
            default:
                showAlert(null, "I have no idea what you clicked!", "ok", null, false, null);
                break;
        }
        return true;
    
public booleanonPrepareOptionsMenu(android.view.Menu menu)

        return true;
    
protected voidonSaveInstanceState(android.os.Bundle outState)

        int index = mRssList.getSelectedItemIndex();
        if(index > -1){
            outState.putInteger("lastIndexItem", index);
        }