0

I have one ListView. It contains text with images. I know that if I click the ListView item a Toast will appear, but what I want is for a click on the item to open their respective activity.

 @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            ArrayList<Recipedetails> image_details = GetSearchResults();

            final ListView lv1 = (ListView) findViewById(R.id.listV_main);
            lv1.setAdapter(new ItemListBaseAdapter(this, image_details));

            lv1.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> a, View v, int position, long id) { 
                    Object o = lv1.getItemAtPosition(position);
                    Recipedetails obj_itemDetails = (Recipedetails)o;
                    Toast.makeText(SouthIndian.this, "You have chosen : " + " " + obj_itemDetails.getName(), Toast.LENGTH_LONG).show();

                     switch(obj_itemDetails.getName())
                     {
                     case "Vegterian":  
                        Intent newActivity = new Intent(SouthIndian.this, 
                                                     SouthIndianvegrecipes.class);     
                        startActivity(newActivity);
                       break;
                     case "Non-Vegterian":  
                        Intent new1Activity = new Intent(SouthIndian.this, 
                                SouthIndiannonvegrecipes.class);     
                        startActivity(new1Activity);
                       break;
                     //same for other Activity
                  }
                }  
            });
        }

        private ArrayList<Recipedetails> GetSearchResults(){
            ArrayList<Recipedetails> results = new ArrayList<Recipedetails>();

            Recipedetails item_details = new Recipedetails();
            item_details.setName("Vegterian");
            item_details.setItemDescription("Recipes made by raw materials");
            //item_details.setPrice("RS 310.00");
            item_details.setImageNumber(1);
            results.add(item_details);

            item_details = new Recipedetails();
            item_details.setName("Non-Vegterian");
            item_details.setItemDescription("Flesh of sweet animals");
            //item_details.setPrice("RS 350.00");
            item_details.setImageNumber(2);
            results.add(item_details);

            item_details = new Recipedetails();
            item_details.setName("Pickels");
            item_details.setItemDescription("Touchable dish  by Homemade");
            //item_details.setPrice("RS 250.00");
            item_details.setImageNumber(3);
            results.add(item_details);

            item_details = new Recipedetails();
            item_details.setName("Soups");
            item_details.setItemDescription("Startup for our food");
            //item_details.setPrice("RS 350.00");
            item_details.setImageNumber(4);
            results.add(item_details);




            return results;
        }

What I want is if I click the first image it has to go their Activity. Like that it has to go their respective activity, not a same one. How can I do this?

My AndroidManifest.xml

 <activity
        android:name="com.example.recipestutors.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="RecipeActivity" android:label="@string/activity_recipeview" android:theme="@style/AppTheme"></activity>
    <activity android:name="SouthIndian" android:label="@string/activity_SouthIndianrecipeview" android:theme="@style/AppTheme"></activity>
    <activity android:name="NorthIndian" android:label="@string/activity_NorthIndianrecipeview" android:theme="@style/AppTheme"></activity>
    <activity android:name="Western" android:label="@string/activity_Westernrecipeview" android:theme="@style/AppTheme"></activity>
    <activity android:name="Chinese" android:label="@string/activity_Chineserecipeview" android:theme="@style/AppTheme"></activity>
     <activity android:name="SouthIndianvegrecipes" android:label="@string/activity_Chineserecipeview" android:theme="@style/AppTheme"></activity>
     <activity android:name="SouthIndiannonvegrecipes" android:label="@string/activity_Chineserecipeview" android:theme="@style/AppTheme"></activity>
</application>

logcat showing error

    03-05 10:47:36.091: E/AndroidRuntime(3485): FATAL EXCEPTION: main
03-05 10:47:36.091: E/AndroidRuntime(3485): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.recipestutors/com.example.recipestutors.MainActivity}: java.lang.ClassNotFoundException: Didn't find class "com.example.recipestutors.MainActivity" on path: /data/app/com.example.recipestutors-1.apk
03-05 10:47:36.091: E/AndroidRuntime(3485):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
03-05 10:47:36.091: E/AndroidRuntime(3485):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
03-05 10:47:36.091: E/AndroidRuntime(3485):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-05 10:47:36.091: E/AndroidRuntime(3485):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
03-05 10:47:36.091: E/AndroidRuntime(3485):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-05 10:47:36.091: E/AndroidRuntime(3485):     at android.os.Looper.loop(Looper.java:137)
03-05 10:47:36.091: E/AndroidRuntime(3485):     at android.app.ActivityThread.main(ActivityThread.java:5039)
03-05 10:47:36.091: E/AndroidRuntime(3485):     at java.lang.reflect.Method.invokeNative(Native Method)
03-05 10:47:36.091: E/AndroidRuntime(3485):     at java.lang.reflect.Method.invoke(Method.java:511)
03-05 10:47:36.091: E/AndroidRuntime(3485):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-05 10:47:36.091: E/AndroidRuntime(3485):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-05 10:47:36.091: E/AndroidRuntime(3485):     at dalvik.system.NativeStart.main(Native Method)
03-05 10:47:36.091: E/AndroidRuntime(3485): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.recipestutors.MainActivity" on path: /data/app/com.example.recipestutors-1.apk
03-05 10:47:36.091: E/AndroidRuntime(3485):     at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:65)
03-05 10:47:36.091: E/AndroidRuntime(3485):     at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
03-05 10:47:36.091: E/AndroidRuntime(3485):     at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
03-05 10:47:36.091: E/AndroidRuntime(3485):     at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
03-05 10:47:36.091: E/AndroidRuntime(3485):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
03-05 10:47:36.091: E/AndroidRuntime(3485):     ... 11 more
Shiv
  • 4,569
  • 4
  • 25
  • 39
kkarthickk
  • 99
  • 4
  • 13

4 Answers4

0

you can use intent in onClickListener

like

    lv1.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> a, View v, int position, long id) { 
            Intent intent = new Intent(firstActivity.this, SecondActivity.class);

            startActivity(intent);
        }  
    });

You can use Switch or if else statement inside onClick to go to perticular activites according to clicked position

like

   switch (Array[position]) {

    case Array[1]: //if position1 is clicked
            Intent intent = new Intent(MainActivity.this, FirstActivity.class);

            startActivity(intent);
            break;
            case Array[2]: //if 2nd position is clicked..so on
            Intent intent = new Intent(MainActivity.this, SecondActivity.class);

           startActivity(intent);
           break;

So on.....

Shiv
  • 4,569
  • 4
  • 25
  • 39
  • no i know that one,if i clicked all the image it will goes to the second activity,but what i need is if i click the first text it has to go second activity,if i click the second text it has to go third activity like this i want – kkarthickk Mar 05 '13 at 10:29
  • Dude,i didn't get ur answer correctly,first i will explain clearly,having 5 items in the list view,if i click the first item it has to open firstactivity,and next if i click the second item it has to open second and if i click 3 items it has to open 3 activity like that i want. – kkarthickk Mar 05 '13 at 10:51
  • ya thats what i have written in my answer Array is ur list view items which are you displaying in list (take your specified name for that)ok and in onClickListener Array[position] will give you clicked item position and according to that position open your relative activity like if position 2 is clicked Array[2] and Case 2 will open your activity specified in that – Shiv Mar 05 '13 at 11:00
  • Array[2] is the item you clicked at position 2 use case "item at 2nd position": //open 2nd activity – Shiv Mar 05 '13 at 11:05
0

use switch-case to open Activity according to ListView click as :

lv1.setOnItemClickListener(new OnItemClickListener() {
   @Override
     public void onItemClick(AdapterView<?> a, View v, int position, long id) { 
       Object o = lv1.getItemAtPosition(position);
        Recipedetails obj_itemDetails = (Recipedetails)o;
        Toast.makeText(SouthIndian.this, "You have chosen : " + " " + 
                           obj_itemDetails.getName(), Toast.LENGTH_LONG).show();
          switch(obj_itemDetails.getName())
           {
           case "Vegterian":  
              Intent newActivity = new Intent(SouthIndian.this, 
                                           Vegterian_Activity.class);     
              startActivity(newActivity);
             break;
           case "Non-Vegterian":  
              Intent newActivity = new Intent(SouthIndian.this, 
                                        Non_Vegterian_Activity.class);     
              startActivity(newActivity);
             break;
           .....//same for other Activity
        }
       }  
  });
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • 03-05 10:41:29.611: E/AndroidRuntime(3334): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.recipestutors/com.example.recipestutors.MainActivity}: java.lang.ClassNotFoundException: Didn't find class "com.example.recipestutors.MainActivity" on path: /data/app/com.example.recipestutors-1.apk 03-05 10:41:29.611: E/AndroidRuntime(3334): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.recipestutors.MainActivity" on path: /data/app/com.example.recipestutors-1.apk it shows error like that in logcat – kkarthickk Mar 05 '13 at 10:52
  • @user2118898 : make sure u have added all Activities in AndroidManifest.xml. also check for MainActivity Activity – ρяσѕρєя K Mar 05 '13 at 10:55
  • @user2118898 : in which case u are starting MainActivity Activity ? – ρяσѕρєя K Mar 05 '13 at 10:57
  • yes Mainactivity,can you look at my code and edit your answer – kkarthickk Mar 05 '13 at 10:57
  • @user2118898 : ok plz edit your question with latest code and also AndroidManifest xml – ρяσѕρєя K Mar 05 '13 at 11:01
  • Can you see my edited code right now and you can tell now the error showing in logcat – kkarthickk Mar 05 '13 at 11:05
  • @user2118898 : everything is fine just use full class path including package name as : `....` for all Activities inside AndroidManifest and before running app uninstall prevision one from device – ρяσѕρєя K Mar 05 '13 at 11:10
  • @user2118898 : i mean previously installed app on device then install new app for testing – ρяσѕρєя K Mar 05 '13 at 11:15
  • @user2118898 : also see this post [Android Activity ClassNotFoundException - tried everything](http://stackoverflow.com/questions/10866431/android-activity-classnotfoundexception-tried-everything) because i think u have same issue – ρяσѕρєя K Mar 05 '13 at 11:18
  • switch(obj_itemDetails.getName()) it shows error in this line it shows change project compilance and JRE to 1.7 – kkarthickk Mar 05 '13 at 11:25
  • dude whats up,whats the problem,they are telling we have to use int or enum only] – kkarthickk Mar 05 '13 at 11:36
  • we can't use switch functionality here – kkarthickk Mar 05 '13 at 12:37
  • how to put if else statment in my function to matches the string – kkarthickk Mar 05 '13 at 12:37
  • @user2118898 : but we can also match strings in switch case. what issue u are facing – ρяσѕρєя K Mar 05 '13 at 12:40
0

Looking at your logcat output there is problem in androidmenifest file, app is unable to find activity class. You should change declaration of activity.

From:

<activity android:name="RecipeActivity" android:label="@string/activity_recipeview"  android:theme="@style/AppTheme"></activity>

To:

<activity android:name=".RecipeActivity" android:label="@string/activity_recipeview" android:theme="@style/AppTheme"></activity>

OR:

<activity android:name="FULL_PACKAGE_PATH.RecipeActivity" android:label="@string/activity_recipeview" android:theme="@style/AppTheme"></activity>

Note: there is missing . in declaration of activity

Rakesh Bhalani
  • 668
  • 4
  • 10
0

Like logcat says, error is on your manifest file..You are not registered the activity properly. In your manifest add "." before all activity name like this

<activity android:name=".RecipeActivity" android:label="@string/activity_recipeview" android:theme="@style/AppTheme"></activity>

or you have to specify like this

<activity android:name="com.example.recipestutors.RecipeActivity" android:label="@string/activity_recipeview" android:theme="@style/AppTheme"></activity>

You need to edit each one.

Alex Chengalan
  • 8,211
  • 4
  • 42
  • 56