当我点击列表项时,没有任何反应。为什么这样,我该如何解决?
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;
}
}
答案 0 :(得分:0)
您的活动应扩展ListActivity
http://developer.android.com/reference/android/app/ListActivity.html
答案 1 :(得分: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
}
已经完成了。
答案 2 :(得分:0)
将侦听器设置为listview。这可能有助于解决您的问题