开发者

How to write onclick event for ListView in Android?

开发者 https://www.devze.com 2023-01-01 10:17 出处:网络
Hi i have the listview the sixitems in it, butwhen i call alet function on event it doesnt work ? let me know how to write a function on item event on click?

Hi i have the listview the sixitems in it, but when i call alet function on event it doesnt work ? let me know how to write a function on item event on click?

     public class PhotoListView extends ListAc开发者_如何学编程tivity {
     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");

    }    


ListView Shot = getListView();

In Shot you have the id for the listview and not for each item in the list.

    @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();

    }

Or you could use 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();


    }
0

精彩评论

暂无评论...
验证码 换一张
取 消