在列表视图项点击Open XML文档 [英] Open xml file on listview item click
问题描述
我想打开一个新的XML文件或文本文件,当用户在列表视图中选择一个项目。下面是我的code: 原来的问题 - >
I'm trying to open a new xml or text file when the user select an item on listview. Below is my code: Original Question-->
MainActivity.java
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.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();
}
}
在 onListItemClick
触发我想打开里面有一些数据的XML文件。
当机器人被选中android.xml,显示出来。
当iPhone时iphone.xml,显示了。
When onListItemClick
is triggered I want to open xml file which has some data.
When "Android" is selected android.xml, shows up.
When "iPhone" is selected iphone.xml, shows up.
推荐答案
在 onItemClick
请
http://www.youtube.com/watch?v=zjHYyAJQ7Vw&名单= PL3D7BFF1DDBDAAFE5
这似乎是很好的教程开始。
Which seems to be good tutorial for a start.
您不要打开XML。您导航到基于列表项点击位置的不同的活动。每个活动都会有自己的布局设置。
You don't open a xml. You navigate to a different activity based on the position of List item click. Each activity will have its own layout set.
下面应该工作
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();
}
请确保您为活动
清单文件中的条目
Make sure you make an entry for Activities
in manifest file
编辑:
public class MainActivity extends ListActivity {
String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
"Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
"Linux", "OS/2" };
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
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) {
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();
}
}
}
这篇关于在列表视图项点击Open XML文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!