[英]How to start a new activity when we click on listview item
I am trying to start an activity,when I click on listview item.But when I run the program,I am getting some error like: 当我单击列表视图项时,我试图开始一项活动。但是,当我运行该程序时,出现了一些错误,例如:
Error:(45, 43) error: no suitable constructor found for Intent(,Class) constructor Intent.Intent(String,Uri) is not applicable (argument mismatch; cannot be converted to String) constructor Intent.Intent(Context,Class) is not applicable (argument mismatch; cannot be converted to Context) Error:(51, 44) error: no suitable constructor found for Intent(,Class) constructor Intent.Intent(String,Uri) is not applicable (argument mismatch; cannot be converted to String) constructor Intent.Intent(Context,Class) is not applicable (argument mismatch; cannot be converted to Context) Error:(57, 43) error: no suitable constructor found for Intent(,Class) constructor Intent.Intent(String,Uri) is not applicable (argument mismatch; cannot be converted to String) constructor Intent.Intent(Context,Class) is not applicable (argument mismatch; cannot be converted to Context) Note: Some messages have been simplified;
错误:(45,43)错误:没有为Intent(,Class)构造函数找到合适的构造函数Intent.Intent(String,Uri)不适用(参数不匹配;无法转换为String)构造函数Intent.Intent(Context,Class)不适用(参数不匹配;无法转换为上下文)错误:(51,44)错误:找不到适用于Intent(,Class)构造函数Intent.Intent(String,Uri)的合适构造函数(参数不匹配;不能为转换为String)构造函数Intent.Intent(Context,Class)不适用(参数不匹配;无法转换为Context)错误:(57,43)错误:找不到适合Intent(,Class)的构造函数Intent.Intent( String,Uri)不适用(参数不匹配;不能转换为String)构造函数Intent.Intent(Context,Class)不适用(参数不匹配;不能转换为Context)注意:一些消息已经简化; recompile with -Xdiags:verbose to get full output Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
用-Xdiags:verbose重新编译以获取完整的输出错误:任务':app:compileDebugJavaWithJavac'的执行失败。
Compilation failed;
编译失败; see the compiler error output for details.
有关详细信息,请参见编译器错误输出。
Menu.java public class Menu extends AppCompatActivity { Menu.java公共类Menu扩展了AppCompatActivity {
ListView lv;
private static String[] menu_list = { "list one", "list 2", "list 3" };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
listview();
}
public void listview() {
lv = (ListView) findViewById(R.id.lv_Menu_List);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.list_layout, menu_list);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// String value =(String)lv.getItemAtPosition(position);
if (position == 0) {
Intent intent = new Intent(this, ListOne.class);
startActivity(intent);
} else if (position == 1) {
Intent intent = new Intent(this, List2.class);
startActivity(intent);
} else if (position == 2) {
Intent intent = new Intent(this, List3.class);
startActivity(intent);
}
}
});
}
}
activity_menu.xml activity_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.examplemenu.Menu">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/lv_Menu_List"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true" />
</RelativeLayout>
list_layout.xml list_layout.xml
<xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:soundEffectsEnabled="true">
</TextView>
Try this: 尝试这个:
Intent intent=new Intent(Menu.this,ListOne.class);
startActivity(intent);
Because just this
refers to AdapterView.OnItemClickListener
not Menu
class. 因为仅
this
是指AdapterView.OnItemClickListener
而不是Menu
类。
You can use Menu.this
or getApplicationContext()
(both works), hope it helps! 您可以使用
Menu.this
或getApplicationContext()
(都可以),希望对您Menu.this
帮助!
public void listview()
{
lv=(ListView)findViewById(R.id.lv_Menu_List);
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,R.layout.list_layout,menu_list);
lv.setAdapter(adapter);
lv.setOnItemClickListener(
new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//String value =(String)lv.getItemAtPosition(position);
if(position==0){
Intent intent=new Intent(getApplicationContext(),ListOne.class);
startActivity(intent);
}
else {
if (position == 1) {
Intent intent = new Intent(Menu.this, List2.class);
startActivity(intent);
} else if (position == 2) {
Intent intent = new Intent(Menu.this, List3.class);
startActivity(intent);
}
}
}
});
}
You can use getApplicationContext()
and also switch
conditions: 您可以使用
getApplicationContext()
并switch
条件:
public void listview() {
lv = (ListView) findViewById(R.id.lv_Menu_List);
ArrayAdapter<String> adapter = new ArrayAdapter<String> (
this,
R.layout.list_layout,
menu_list);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
final Intent intent;
switch (position) {
case 0:
intent = new Intent(getApplicationContext, listone.class);
break;
case 1:
intent = new Intent(getApplicationContext, list2.class);
break;
case 2:
intent = new Intent(getApplicationContext, list3.class);
break;
default:
intent = new Intent(getApplicationContext, YourDefaultActivity.class);
break;
}
context.startActivity(intent);
}
}
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.