列表项在android studio中不起作用

时间:2015-07-08 09:38:10

标签: java android android-listview

使用android studio运行应用程序后,当我点击列表项时没有任何反应,我的代码出了什么问题?

package com.asaad.siag.asaad;


import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import static java.lang.String.valueOf;

public class Menu extends ListActivity{

String classes[] = {"MainActivity", "example 1", "example 2", "example 3", "example 4"
        , "example 5", "example 6"};
@Override
protected void onCreate(Bundle v){
    super.onCreate(v);
    setListAdapter(new ArrayAdapter<String>(Menu.this, android.R.layout.simple_list_item_1,classes));
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {


    super.onListItemClick(l, v, position, id);
    String cheese = classes [position];
    try {
        Class ourClass = Class.forName("com.asaad.siag.asaad "+ cheese);
        Intent ourIntent = new Intent(Menu.this, ourClass);
        startActivity(ourIntent);
    }catch (ClassNotFoundException e){
        e.printStackTrace();
    }
}}

我认为是因为class.forName

1 个答案:

答案 0 :(得分:-1)

你这里没有列表视图。在xml文件中实现listview,在活动中获取它并按照:

ListView lv = (ListView)findViewById(R.id.your_list_view);

lv.setOnItemClickListener(new OnItemClickListener()
{
@Override 
public void onItemClick(AdapterView<?> arg0, View arg1,int position, long arg3)
{ 
   //do st
}
});