点击列表视图项

时间:2016-03-30 20:38:41

标签: android string listview android-activity onclicklistener

我正在尝试启动一项活动,当我点击listview项目时。但是当我运行该程序时,我收到的错误如下:

  

错误:(45,43)错误:找不到合适的构造函数   Intent(,Class)构造函数   Intent.Intent(String,Uri)不适用(参数不匹配;    无法转换为String)   构造函数Intent.Intent(Context,Class)不适用   (论证不匹配;不可能   转换为Context)错误:(51,44)错误:没有合适的构造函数   找到Intent(,Class)   构造函数Intent.Intent(String,Uri)不适用(参数   不匹配;无法转换为   字符串)构造函数Intent.Intent(Context,Class)不适用   (论证不匹配;不可能   转换为Context)错误:(57,53)错误:没有合适的构造函数   找到Intent(,Class)   构造函数Intent.Intent(String,Uri)不适用(参数   不匹配;无法转换为   字符串)构造函数Intent.Intent(Context,Class)不适用   (论证不匹配;不可能   转换为上下文)注意:某些消息已经简化;   使用-Xdiags重新编译:详细以获得完整输出错误:执行   任务失败':app:compileDebugJavaWithJavac'。

     

编译失败;有关详细信息,请参阅编译器错误输出。

Menu.java     public class 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

<?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

    <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>

3 个答案:

答案 0 :(得分:5)

试试这个:

 Intent intent=new Intent(Menu.this,ListOne.class);
 startActivity(intent);

因为this只是AdapterView.OnItemClickListener而不是Menu类。

答案 1 :(得分:0)

您可以使用Menu.thisgetApplicationContext()(两者都有效),希望有所帮助!

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

答案 2 :(得分:0)

您可以使用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);
            }
        }
    });
}