Mihir
Mihir

Reputation: 557

Open xml file on listview item click

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

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

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.

Upvotes: 0

Views: 2108

Answers (3)

GB_Bhayani ツ
GB_Bhayani ツ

Reputation: 368

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;

Upvotes: 2

wblaschko
wblaschko

Reputation: 3262

Based on the assumptions mentioned in the conversation on @Raghunandan's answer, here is how I would complete this task. This assumes that each of your different XMLs (Android, iPhone, etc) doesn't require unique functions (there would be ways to do this to an extent using onClicks in the layout), but that the intention is to display information.

This is the class you posted:

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

This is the new class I would use to display the information:

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

    }
}

And the manifest

<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"/>

Upvotes: 0

Raghunandan
Raghunandan

Reputation: 133560

In onItemClick

Follow

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

Which seems to be good tutorial for a start.

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.

The below should work

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

Edit:

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

Upvotes: 2

Related Questions