将数组从一个活动传递到android中的另一个活动 [英] passing an array from one activity to another activity in android
本文介绍了将数组从一个活动传递到android中的另一个活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想从sqlite获取数据到一个数组并将该数组传递给下一个activity.i尝试如下。但是我得到一个错误..请帮助我
SQLiteDatabase db = openOrCreateDatabase( MyDB,MODE_PRIVATE,空值);
Cursor c = db.rawQuery( SELECT timeslots FROM medprofile,null);
字符串 [] array = new 字符串跨度> [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 ],数组[ 2 ]});
意图=新意图(MainActivity。此,profileview。 class ) ;
in.putExtras(b);
startActivity(in);
}
在下一个活动中,我在开始时通过了那个数组,
public class profileview扩展ListActivity {
Bundle b = this .getIntent()。getExtras();
String [] array = b.getStringArray( null );
但它不起作用..我把null作为一个关键。我的错是什么?
解决方案
< blockquote>你可以通过使用字符串的分割功能使它变得更简单。
按照以下步骤操作:
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("#");
这篇关于将数组从一个活动传递到android中的另一个活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文