0

我有一个ListView。它包含带有图像的文本。我知道如果我单击该ListView项目Toast会出现一个,但我想要的是单击该项目以打开它们各自的活动。

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

我想要的是,如果我点击它必须去他们的第一张图片Activity。就像它必须进行各自的活动,而不是同一个活动。我怎样才能做到这一点?

我的 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 显示错误

    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
4

4 回答 4

0

您可以在 onClickListener 中使用意图

喜欢

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

您可以在 onClick 中使用 Switch 或 if else 语句根据点击位置转到特定活动

喜欢

   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;

很快.....

于 2013-03-05T10:28:10.053 回答
0

用于switch-case根据 ListView 点击打开 Activity:

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
        }
       }  
  });
于 2013-03-05T10:31:40.330 回答
0

查看您的 logcat 输出,androidmenifest 文件中存在问题,应用程序无法找到活动类。您应该更改活动声明。

从:

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

至:

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

或者:

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

注意:缺少 . 在活动声明中

于 2013-03-05T12:06:42.960 回答
0

就像 logcat 说的那样,你的清单文件上有错误。你没有正确注册活动。在您的清单中添加“。” 在所有像这样的活动名称之前

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

或者你必须像这样指定

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

您需要编辑每一个。

于 2013-03-05T12:56:50.297 回答