且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将数组从一个活动传递到android中的另一个活动

更新时间:2023-01-08 15:50:43

你可以通过使用字符串的分割功能使它变得更简单。



按照以下步骤操作:



1)用独特字符分隔大串时隙



2)将字符串传递给另一个活动



3)获取字符串并将其拆分为数组



第一项活动





 SQLiteDatabase db = openOrCreateDatabase(  MyDB,MODE_PRIVATE,null); 
Cursor c = db.rawQuery( SELECT timeslots FROM medprofile,null);


字符串 timeslots = 跨度>;
int i = 0;
while (c.moveToNext())
{
String uname = c.getString(c.getColumnIndex( timeslots));
timeslots + = uname + ;
Toast.makeText(getApplicationContext(),uname,Toast.LENGTH_SHORT).show();
}

意图=新意图(MainActivity。,profileview。 class 跨度>);

in.putExtra( timeslots,timeslots);
startActivity(in);

}





第二项活动





在第二个活动的onCreate方法中添加此代码



 Bundle extras = getIntent()。getExtras() ; 
字符串 [] timeslots = extras.getString( timeslots)。split( );


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

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,


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?

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



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

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