嗨我有列表视图中的六个项目,但是当我在事件上调用alet函数时它不起作用?让我知道如何在点击项目事件上编写函数?
public class PhotoListView extends ListActivity {
String[] listItems = {"HeadShot", "BodyShot ", "ExtraShot", "Video Take1", "Video Take2", "Video Take3", };
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setListAdapter(new ArrayAdapter(this,android.R.layout.simple_list_item_1, listItems));
}
OnListclick
ListView Shot = getListView();
protected void onListItemClick(View view) {
if(view == Shot){
AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
// set the message to display
alertbox.setMessage("Please Get Ready");
}
答案 0 :(得分:8)
ListView Shot = getListView();
在Shot中,您拥有列表视图的ID,而不是列表中的每个项目。
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
// set the message to display
alertbox.setMessage("Please Get Ready").show();
}
或者您可以使用ListView :: setOnItemClickListener
public class PhotoListView extends ListActivity implements OnItemClickListener
ListView shot = getListView();
shot.setOnItemClickListener(this);
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
// set the message to display
alertbox.setMessage("Please Get Ready").show();
}