-1

当我单击列表项时,没有任何反应。为什么会这样,我该如何解决?

public class SecondActivity extends Activity {
    TextView selection ;
    private String[] menus = {"MainActivity","spam"};
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        ListView listview = (ListView)findViewById(R.id.list) ;
        listview.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, menus));  
    }

    public void onListItemClick(ListView parent, View v,int position, long id) {
        Intent intent = new Intent (SecondActivity.this,ThirdActivity.class);
        startActivity(intent);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_second, menu);
        return true;
    }
}
4

3 回答 3

0

您的活动应该扩展 ListActivity

http://developer.android.com/reference/android/app/ListActivity.html

于 2012-08-19T15:42:46.537 回答
0

您必须覆盖 onListItemClick 方法:

@Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        //start activity
    }

也不要忘记在您的活动类中扩展 ListActivity。

编辑:

你的班级应该是这样的:

public class SecondActivity extends ListActivity{

//onCreate {...}

@Override
        protected void onListItemClick(ListView l, View v, int position, long id) {
            //start activity
        }

它已经完成了。

于 2012-08-19T15:43:18.337 回答
0

将侦听器设置为列表视图。这可能有助于解决您的问题

于 2012-08-19T15:50:51.250 回答