我有一个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
答案 0 :(得分:0)
您可以在onClickListener中使用intent
像
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;
所以......
答案 1 :(得分: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
}
}
});
答案 2 :(得分:0)
看看你的logcat输出androidmenifest文件有问题,app无法找到活动类。你应该改变活动声明。
自:
<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>
OR:
<activity android:name="FULL_PACKAGE_PATH.RecipeActivity" android:label="@string/activity_recipeview" android:theme="@style/AppTheme"></activity>
注意:缺少。在活动声明中
答案 3 :(得分: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>
您需要编辑每个。