2010-05-31 24 views
1

嗨,我有listview中的sixitems,但是当我在事件上调用alet函数它不工作?让我知道如何在点击项目事件上编写函数?如何在Android中编写ListView的onclick事件?

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

    }  

回答

8
ListView Shot = getListView(); 

在射击你有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(); 


    } 
相关问题