开发者

Moving from one ListView Activity to another ListView Activiy

开发者 https://www.devze.com 2023-02-26 13:30 出处:网络
I am using a ListView in Android by creating this class public class HBSListView extends ListActivity;

I am using a ListView in Android by creating this class

public class HBSListView extends ListActivity;

When I click on item in list and I want to开发者_C百科 go to next ListActivity showing relative details of clicked item in previous list.

lv.setOnItemClickListener( new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {              
        @SuppressWarnings("unchecked")
        HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(position);                   

        Intent a = new Intent(iTeam.Ufinder.Application.HBS.HBSDetailView.class.getName()); 
        a.putExtra("store_id", o.get("id")); 
        startActivity(a); 

        // When I use above code it is not working. I want to pass ID also.

        // This works but i do not know how to pass ID this way.
        // startActivity(new Intent("iTeam.Ufinder.Application.HBSDETAILVIEW"));
    }
});

public class HBSDetailView extends ListActivity this is class in which I want to move.

Mainfest File

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="iTeam.Ufinder.Application"
  android:versionCode="1"
  android:versionName="1.0">
<uses-sdk android:minSdkVersion="10" />

<application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".Startup"
              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=".main"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="iTeam.Ufinder.Application.CLEARSCREEN" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    <activity android:name="iTeam.Ufinder.Application.MANAGEMENT.Management"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="iTeam.Ufinder.Application.MANAGEMENT" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    <activity android:name="iTeam.Ufinder.Application.HBS.HBSListView"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="iTeam.Ufinder.Application.HBSLISTVIEW" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    <activity android:name="iTeam.Ufinder.Application.HBS.HBSDetailView"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="iTeam.Ufinder.Application.HBSDETAILVIEW" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

</application>
<uses-permission android:name="android.permission.INTERNET" />

This is the Exception:

04-15 13:04:43.901: ERROR/AndroidRuntime(1417): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{iTeam.Ufinder.Application/iTeam.Ufinder.Application.HBS.HBSDetailView}: java.lang.NullPointerException


When you creating Intent like this

Intent a = new Intent(iTeam.Ufinder.Application.HBS.HBSDetailView.class.getName()); 

you creating it with action name of your class name. You need to create Intent like that:

Intent a = new Intent(HBSListView.this, iTeam.Ufinder.Application.HBS.HBSDetailView.class); 

The difference is in call signature. First one is of one argument of type String. It is stands for creating Intent with specified action. Second one is about Context and Class arguments. It is for creating an Intent to call a specified class in specified context.

Also check what o is not null.

EDIT

Well, if you'd like to start activity this way...

Intent a = new Intent("iTeam.Ufinder.Application.HBSDETAILVIEW");
a.putExtra("store_id", o.get("id")); 
startActivity(a); 

The code above must work as you are expecting....


Bundle bundle = new Bundle();
ListAdapter adapter = (new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,list1));
list.setAdapter(adapter);

list.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> arg0, View v,int position, long id) {
        HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(position);

        if (position == 0) {
            bundle.putString("store_id", o.get("id"));
            Intent inten = new Intent("Activity name which is you must define in manifest file"); 
            inten.putExtras(bundle);

            startActivity(inten);
        }

        if(position==1) {
            //write your code for when second item of listview clicked
        }

        .....

    }
});

Add your Activity to the manifest file:

<activity
    android:name=".className"
    android:label="@string/app_name" >
    <intent-filter>
        <action android:name="com.test.ACTIVITY" />
        //use this name when you want to run this activty

        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>
0

精彩评论

暂无评论...
验证码 换一张
取 消