2010-05-31 23 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(); 


    } 
相關問題