Click here to Skip to main content
15,920,111 members
Please Sign up or sign in to vote.
3.00/5 (3 votes)
See more:
I want to get data from sqlite to an array and pass that array to next activity.i tried it as follow.but iam getting an error..please help me

Java
SQLiteDatabase db = openOrCreateDatabase("MyDB",MODE_PRIVATE, null);
Cursor c=db.rawQuery("SELECT timeslots FROM medprofile",null);
	    
	    
String[] array = new String[c.getCount()];
int i=0;
while(c.moveToNext())
	{
		 String uname = c.getString(c.getColumnIndex("timeslots"));
			    array[i] = uname;
		  Toast.makeText(getApplicationContext(), array[i], Toast.LENGTH_SHORT).show();
		   i++;
	 }	
		 
	 Bundle b=new Bundle();
	b.putStringArray(null, new String[]{array[1], array[2]});

	Intent in=new Intent(MainActivity.this,profileview.class);
		
	 in.putExtras(b);
	 startActivity(in);
			
}


in next activity,i passed that array at the begining,


C#
public class profileview extends ListActivity {
    Bundle b=this.getIntent().getExtras();
    String[] array=b.getStringArray(null);


but it doesnt work..i put null as a key.what is my mistake?
Posted
Updated 16-Apr-13 0:44am
v4

1 solution

You can make it simpler by the use of string's split function.

Follow these steps:

1)Make a large string of timeslots separated by unique character

2)Pass the string to another activity

3)Fetch the string and split it into array

First Activity



Java
SQLiteDatabase db = openOrCreateDatabase("MyDB",MODE_PRIVATE, null);
Cursor c=db.rawQuery("SELECT timeslots FROM medprofile",null);
	    
	    
String timeslots="";
int i=0;
while(c.moveToNext())
	{
		 String uname = c.getString(c.getColumnIndex("timeslots"));
			    timeslots+=uname+"#";
		  Toast.makeText(getApplicationContext(), uname, Toast.LENGTH_SHORT).show();
	 }	
 
	Intent in=new Intent(MainActivity.this,profileview.class);
		
	 in.putExtra("timeslots",timeslots);
	 startActivity(in);
			
}


Second Activity



Add this code in onCreate method of second activity

Java
 Bundle extras=getIntent().getExtras();
String[] timeslots=extras.getString("timeslots").split("#");
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900