Android 在listview项上打开xml文件单击
当用户在listview上选择一个项目时,我试图打开一个新的xml或文本文件。下面是我的代码: 原始问题--> MainActivity.javaAndroid 在listview项上打开xml文件单击,android,eclipse,listview,Android,Eclipse,Listview,当用户在listview上选择一个项目时,我试图打开一个新的xml或文本文件。下面是我的代码: 原始问题--> MainActivity.java package com.example.listview; import android.app.ListActivity; import android.os.Bundle; import android.view.View; import android.widget.ArrayAdapter; import android.widget.Li
package com.example.listview;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends ListActivity {
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
"Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
"Linux", "OS/2" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, values);
setListAdapter(adapter);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
String item = (String) getListAdapter().getItem(position);
Toast.makeText(this, item + " selected", Toast.LENGTH_LONG).show();
}
}
package com.example.listview;
导入android.app.ListActivity;
导入android.os.Bundle;
导入android.view.view;
导入android.widget.ArrayAdapter;
导入android.widget.ListView;
导入android.widget.Toast;
公共类MainActivity扩展了ListActivity{
创建公共空间(捆绑冰柱){
超级冰柱;
字符串[]值=新字符串[]{“Android”、“iPhone”、“WindowsMobile”,
“黑莓”、“WebOS”、“Ubuntu”、“Windows7”、“Max OS X”,
“Linux”、“OS/2”};
ArrayAdapter=新的ArrayAdapter(此,
android.R.layout.simple_list_item_1,值);
setListAdapter(适配器);
}
@凌驾
受保护的void onListItemClick(列表视图l、视图v、整数位置、长id){
字符串项=(字符串)getListAdapter().getItem(位置);
Toast.makeText(此项+选定项),Toast.LENGTH_LONG.show();
}
}
当触发onListItemClick
时,我想打开包含一些数据的xml文件。
当选择“Android”时,将显示Android.xml。
当选择“iPhone”时,iPhone.xml将显示。在
onItemClick中
跟随
这似乎是一个很好的开始教程
您不需要打开xml。您可以根据列表项单击的位置导航到其他活动。每个活动都有自己的布局集
下面的方法应该有效
try
{
String val = values[postion];
Class ourClass = Class.forName("com.example.listview."+val);
Intent intent = new Intent(MainActivity.this,ourClass);
startActivity(intent);
}catch(Exception e){
e.prinStacktrace();
}
确保在清单文件中为活动
创建一个条目
编辑:
public类MainActivity扩展了ListActivity{
字符串[]值=新字符串[]{“Android”、“iPhone”、“WindowsMobile”,
“黑莓”、“WebOS”、“Ubuntu”、“Windows7”、“Max OS X”,
“Linux”、“OS/2”};
创建公共空间(捆绑冰柱){
超级冰柱;
ArrayAdapter=新的ArrayAdapter(此,
android.R.layout.simple_list_item_1,值);
setListAdapter(适配器);
}
@凌驾
受保护的void onListItemClick(列表视图l、视图v、整数位置、长id){
尝试
{
字符串val=值[位置];
Class-ourClass=Class.forName(“com.example.listview.+val”);
意向意向=新意向(MainActivity.this,ourClass);
星触觉(意向);
}捕获(例外e){
e、 prinStacktrace();
}
}
}
在中单击
跟随
这似乎是一个好的教程开始
您不需要打开xml。您可以根据列表项单击的位置导航到其他活动。每个活动都有自己的布局集
下面的方法应该有效
try
{
String val = values[postion];
Class ourClass = Class.forName("com.example.listview."+val);
Intent intent = new Intent(MainActivity.this,ourClass);
startActivity(intent);
}catch(Exception e){
e.prinStacktrace();
}
确保在清单文件中为活动
创建一个条目
编辑:
public类MainActivity扩展了ListActivity{
字符串[]值=新字符串[]{“Android”、“iPhone”、“WindowsMobile”,
“黑莓”、“WebOS”、“Ubuntu”、“Windows7”、“Max OS X”,
“Linux”、“OS/2”};
创建公共空间(捆绑冰柱){
超级冰柱;
ArrayAdapter=新的ArrayAdapter(此,
android.R.layout.simple_list_item_1,值);
setListAdapter(适配器);
}
@凌驾
受保护的void onListItemClick(列表视图l、视图v、整数位置、长id){
尝试
{
字符串val=值[位置];
Class-ourClass=Class.forName(“com.example.listview.+val”);
意向意向=新意向(MainActivity.this,ourClass);
星触觉(意向);
}捕获(例外e){
e、 prinStacktrace();
}
}
}
根据@Raghunandan答案对话中提到的假设,我将如何完成这项任务。这假设您的每个不同的XML(Android、iPhone等)都不需要唯一的功能(在某种程度上可以通过在布局中使用onclick来实现),但其目的是显示信息
这是您发布的类:
public class MainActivity extends ListActivity {
...
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
String item = (String) getListAdapter().getItem(position);
Intent intent = new Intent(this, DisplayInformation.class);
Bundle bundle = new Bundle();
int layout;
//figure out which layout we want to display based on our human-readable String[]
if(item.equals("Android")){
layout=R.id.android;
}else if(item.equals("iPhone")){
layout=R.id.iphone;
}else if{
...
}
//attach the bundle with the layout to the intent
bundle.putInt("layout",layout);
intent.putExtras(bundle);
//start our intent
startActivity(intent);
}
}
这是我用来显示信息的新类:
public class DisplayInformation extends Activity{
@Override
public void onCreate(Bundle icicle){
this.super(icicle);
Bundle extras = getIntent().getExtras();
if(extras.containsKey("layout")){
setContentView(extras.getInt("layout"));
}else{
//we tried to start the activity with no layout defined
this.finish();
}
}
}
舱单呢
<activity android:name=".MainActivity" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name= "android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".DisplayInformation" android:label="@string/app_name"/>
根据@Raghunandan答案对话中提到的假设,我将如何完成这项任务。这假设您的每个不同的XML(Android、iPhone等)都不需要唯一的功能(在某种程度上可以通过在布局中使用onclick来实现),但其目的是显示信息
这是您发布的类:
public class MainActivity extends ListActivity {
...
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
String item = (String) getListAdapter().getItem(position);
Intent intent = new Intent(this, DisplayInformation.class);
Bundle bundle = new Bundle();
int layout;
//figure out which layout we want to display based on our human-readable String[]
if(item.equals("Android")){
layout=R.id.android;
}else if(item.equals("iPhone")){
layout=R.id.iphone;
}else if{
...
}
//attach the bundle with the layout to the intent
bundle.putInt("layout",layout);
intent.putExtras(bundle);
//start our intent
startActivity(intent);
}
}
这是我用来显示信息的新类:
public class DisplayInformation extends Activity{
@Override
public void onCreate(Bundle icicle){
this.super(icicle);
Bundle extras = getIntent().getExtras();
if(extras.containsKey("layout")){
setContentView(extras.getInt("layout"));
}else{
//we tried to start the activity with no layout defined
this.finish();
}
}
}
舱单呢
<activity android:name=".MainActivity" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name= "android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".DisplayInformation" android:label="@string/app_name"/>
lv.setOnItemClickListener(新的OnItemClickListener(){
public void onItemClick(AdapterView父级、视图、,
内部位置,长id){
开关(位置){
案例0:
Intent firstIntent=newintent(AndroidListViewActivity.this,SingleListItem.class);
星触觉(第一意图);
中断;
案例1:
Intent secondintent=newintent(AndroidListViewActivity.this,crooks.class);
星触觉(第二意图);
打破
lv.setOnItemClickListener(新的OnItemClickListener(){
public void onItemClick(AdapterView父级、视图、,
内部位置,长id){
开关(位置){
案例0:
Intent firstIntent=newintent(AndroidListViewActivity.this,SingleListItem.class);
星触觉(第一意图);
中断;
案例1:
Intent secondintent=newintent(AndroidListViewActivity.this,crooks.class);
星触觉(第二意图);
打破
那有什么问题?你在问什么?我不知道怎么问