Android 使用ListView从一个活动向另一个活动发送和检索数据
我正在做一个测试程序来练习listview的工作原理 我声明字符串Android 使用ListView从一个活动向另一个活动发送和检索数据,android,listview,android-listview,Android,Listview,Android Listview,我正在做一个测试程序来练习listview的工作原理 我声明字符串 final String[] topics= {"History" , "Arts","Civics","Transport","Technology","Science", "Social", "Architecture", enter code here"Health","Geography","Computers","Business","Celebrities","Sports","Lit
final String[] topics= {"History" , "Arts","Civics","Transport","Technology","Science", "Social",
"Architecture", enter code here"Health","Geography","Computers","Business","Celebrities","Sports","Literature", "Music", };
now i use listview to see the list of items i have declared
Lst.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
Bundle bundle = new Bundle();
bundle.putStringArray("Bundlekey", topics);
String Topicname = topics[position];
Intent intent = new Intent(QwizActivity.this,secondActivity.class);
intent.putExtras(bundle);
startActivity(intent);
}
第二个活动的xml
android:id="@+id/Question"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Question"
android:id="@+id/Topictxt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="History"
我的问题是为什么“检查”为空,以及如何将“检查”与“历史”sting“艺术”sting等值进行比较。这是我的第一个问题,如果我问的问题格式不正确,请告诉我。谢谢。您正在从OnItemClickListener发送一个带有putStringArray的数组,并在下一个活动中将其作为aString。改为发送Topicname
String Topicname = topics[position];
bundle.putString("Bundlekey", Topicname);
此外,您还可以将“Bundlekey”设置为公共静态变量,以访问您正在从OnItemClickListener使用putStringArray感知一个数组,并在下一个活动中将其作为aString获取。改为发送Topicname
String Topicname = topics[position];
bundle.putString("Bundlekey", Topicname);
您还可以将“Bundlekey”设置为公共静态变量,以便在第二个活动中获取数据时访问,您必须从Bundle中获取字符串数组
Bundle b=this.getIntent().getExtras();
String[] array=b.getStringArray(key);
然后从该数组中获取字符串值。在第二个活动中获取数据时,必须从绑定中获取字符串数组
Bundle b=this.getIntent().getExtras();
String[] array=b.getStringArray(key);
然后从该数组中获取字符串值。要检查从intent接收到的字符串,您可能需要使用for循环对数组中的每个值进行比较。下面的函数将返回您接收到的字符串 字符串compareValues(数组名称、已接收的值) {
字符串温度;
for(int i=0;i要检查从intent接收到的字符串,您可能需要使用for循环对数组中的每个值进行比较。下面的函数将返回您接收到的字符串
字符串compareValues(数组名称、已接收的值)
{
字符串温度;
对于(int i=0;我希望提问时更加简洁。+1表示轻松。+1表示轻松。+1表示感谢帮助。。我认为这只是程序中的错误。我将它改为putString而不是putStringArray。谢谢,它工作了。嘿,感谢帮助。。我认为这只是程序中的错误。我改变了d将其转换为putString而不是putStringArray谢谢,它成功了
String temp;
for(int i=0;i<array.length;i++)
{
if(value.equalsignorecase(array[i]))
temp = array[i];
break;
}
}
return temp;
}