0 Replies - 2678 Views - Last Post: 29 August 2010 - 09:29 AM

#1 Scorpiobuzz  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 66
  • Joined: 06-February 09

Android Copy a Database Entry to New Database

Posted 29 August 2010 - 09:29 AM

Using the Notepad tutorial to understand the various methods of working with a database. I have created a database and would like to have one Activity open a ListActivity for a result. When the ListActivity opens, it populates the screen with the title of all the entries already in the first database. When a person clicks on one, I would like to have that database entry copied into the new database, then pass back the rowId that was entered into the new Database so that the calling Activity can use it and assign that entries title to a corresponding button. Here is the code I have so far.

The Activity that calls the ListActivity.
package com.dinner.live;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.onclickListener;
import android.widget.Button;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;

public class PlanMenu extends Activity {
	
	static final private int CHOOSE_MONDAY = 0;
	static final private int CHOOSE_TUESDAY = 0;
	private int ButtonPushed = 0;
	private NotesDbAdapter mDbHelper;
	private MenuDbAdapter menuDbHelper;
	private Long mRowId;
	String menuTitle;
	String menuProtein;
	String menuBody;
	

	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
				
		setContentView(R.layout.plan_menu);

		Toast.makeText(this, "Choose a day to pick a meal for!", Toast.LENGTH_LONG).show();
		mDbHelper = new NotesDbAdapter(this);
        mDbHelper.open();
        menuDbHelper = new MenuDbAdapter(this);
        menuDbHelper.open();
        
		 
		 		 
	}
	
	public void mButtonHandler(View target)
    {
		switch(target.getId())
		{
		case R.id.monday:
			// Create new intent object and tell it to call the ColorPicker class
	    	Intent question = new Intent(this, PlanMenuList.class);	
	    	// Start ColorPicker as a new activity and wait for the result 
	    	startActivityForResult(question, CHOOSE_MONDAY);
	    	break;
		case R.id.tuesday:
			// Create new intent object and tell it to call the ColorPicker class
	    	Intent question1 = new Intent(this, PlanMenuList.class);	
	    	// Start ColorPicker as a new activity and wait for the result 
	    	startActivityForResult(question1, CHOOSE_TUESDAY);
	    	break;
		}
			
			

    }

	
	@Override
    protected void onActivityResult(int requestCode, int resultCode, 
                                    Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        if(resultCode == RESULT_OK){
        	switch(requestCode)
        	{
        		case CHOOSE_MONDAY:
        			Long label = intent.getExtras().getLong(NotesDbAdapter.KEY_ROWID);
        			//menuDbHelper.fetchMenu(mRowId);
        			mRowId = label;
        			Button tv = (Button)findViewById(R.id.monday);
        			Toast.makeText(this,MenuDbAdapter.KEY_TITLE , Toast.LENGTH_LONG).show();
        			tv.setText("Monday" + " - " + menuTitle);
        			break;

        		/*case CHOOSE_TUESDAY:
        			Long label = intent.getExtras().getLong(NotesDbAdapter.KEY_ROWID);
        			mRowId = label;
        			populateFields();
        			Button tv = (Button)findViewById(R.id.tuesday);
        			tv.setText("Tuesday" + " - " + menuTitle);*/
        	}

        }
	}
	
	private void populateFields() {
        if (mRowId != null) {
            Cursor note = mDbHelper.fetchNote(mRowId);
            startManagingCursor(note);
            menuTitle=(note.getString(
    	            note.getColumnIndexOrThrow(MenuDbAdapter.KEY_TITLE)));
            menuProtein=(note.getString(
                    note.getColumnIndexOrThrow(MenuDbAdapter.KEY_PROTEIN)));
            menuBody=(note.getString(
                    note.getColumnIndexOrThrow(MenuDbAdapter.KEY_BODY)));
        }
        
    }
}


This is the ListActivity that is called with startActivityForResult
package com.dinner.live;


import java.util.ArrayList;
import java.util.List;

import android.R.string;
import android.app.ListActivity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;


public class PlanMenuList extends ListActivity {
	
	private NotesDbAdapter mDbHelper;
	private MenuDbAdapter menuDbHelper;
	private List<Data>data;
	String menuTitle;
	String menuProtein;
	String menuBody;
    private Long mRowId;
	
	
	
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.notes_list);
        mDbHelper = new NotesDbAdapter(this);
        menuDbHelper = new MenuDbAdapter(this);
        mDbHelper.open();
        menuDbHelper.open();
        fillData(); 
    }
    
    private void fillData() {
        Cursor notesCursor = mDbHelper.fetchAllNotes();
        startManagingCursor(notesCursor);
        
        // Create an array to specify the fields we want to display in the list (only TITLE)
        String[] from = new String[]{NotesDbAdapter.KEY_TITLE};
        
        // and an array of the fields we want to bind those fields to (in this case just text1)
        int[] to = new int[]{R.id.text1};
        
        // Now create a simple cursor adapter and set it to display
        SimpleCursorAdapter notes = 
        	    new SimpleCursorAdapter(this, R.layout.notes_row, notesCursor, from, to);
        setListAdapter(notes);
        
    }
    
    private void populateFields() {
        if (mRowId != null) {
            Cursor note = mDbHelper.fetchNote(mRowId);
            startManagingCursor(note);
            menuTitle=(note.getString(
    	            note.getColumnIndexOrThrow(NotesDbAdapter.KEY_TITLE)));
            menuProtein=(note.getString(
                    note.getColumnIndexOrThrow(NotesDbAdapter.KEY_PROTEIN)));
            menuBody=(note.getString(
                    note.getColumnIndexOrThrow(NotesDbAdapter.KEY_BODY)));
        }
    }
    
     
    
    
   
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        mDbHelper.fetchNote(id);
        mRowId = id;
        populateFields();
        menuDbHelper.createMenu("monday", menuTitle, menuProtein, menuBody);
        Intent answer = new Intent();
        answer.putExtra(MenuDbAdapter.KEY_ROWID, id);
        setResult(RESULT_OK, answer);
        finish();



    }

}



But nothing seems to work when I try to copy the user-selection into a new database and then send back the id so I can use it in the original Activity. Any help would be appreciated.

Is This A Good Question/Topic? 0
  • +

Page 1 of 1