在listview项目上单击打开xml文件

时间:2014-02-21 17:30:29

标签: 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.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”android.xml时,显示出来。 当选择“iPhone”iphone.xml时,显示。

3 个答案:

答案 0 :(得分:2)

  

lv.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View view, int position, long id) { switch(position){ case 0: Intent firstIntent = new Intent(AndroidListViewActivity.this, SingleListItem.class); startActivity(firstIntent); break;
case 1: Intent secondintent = new Intent(AndroidListViewActivity.this, jokes.class); startActivity(secondintent); break;

答案 1 :(得分:1)

onItemClick

http://www.youtube.com/watch?v=zjHYyAJQ7Vw&list=PL3D7BFF1DDBDAAFE5

这似乎是一个很好的教程。

您没有打开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();
}

确保在清单文件

中输入Activities

编辑:

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();
    }
  }
} 

答案 2 :(得分:0)

基于@ Raghunandan回答的对话中提到的假设,以下是我将如何完成此任务。这假设您的每个不同的XML(Android,iPhone等)都不需要独特的功能(在布局中使用onClicks会有一些方法可以做到这一点),但目的是显示信息。

这是你发布的课程:

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"/>
相关问题