如何写在Android中的ListView onclick事件? [英] How to write onclick event for ListView in Android?

查看:205
本文介绍了如何写在Android中的ListView onclick事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我有ListView控件在它sixitems,但是当我呼吁事件ALET功能,它不工作?让我知道如何写一篇关于项目事件的函数上的点击?

 公共类PhotoListView扩展ListActivity {
     的String [] = listItems中{爆头,BodyShot,ExtraShot,视频Take1,视频TAKE2,视频Take3};
    @覆盖
     公共无效的onCreate(捆绑savedInstanceState){
         super.onCreate(savedInstanceState);
         的setContentView(R.layout.main);
         setListAdapter(新ArrayAdapter(这一点,android.R.layout.simple_list_item_1,listItems中));
     }

OnListclick

 的ListView射击= getListView();
  保护无效onListItemClick(查看视图){    如果(查看==射击){        AlertDialog.Builder alertbox =新AlertDialog.Builder(本);        //设置为显示该消息
          alertbox.setMessage(请做好准备);    }


解决方案

ListView的射击= getListView();

在射击你有id为列表视图,而不是对列表中的每个项目。

  @覆盖
    保护无效onListItemClick(ListView中升,视图V,INT位置,长的id){
        // TODO自动生成方法存根
        super.onListItemClick(L,V,位置ID); AlertDialog.Builder alertbox =新AlertDialog.Builder(本);        //设置为显示该消息
          alertbox.setMessage(请做好准备)显示()。    }

或者你可以使用的ListView :: setOnItemClickListener

 公共类PhotoListView扩展ListActivity实现OnItemClickListener
ListView的投篮= getListView();
        shot.setOnItemClickListener(本);
@覆盖
    公共无效onItemClick(适配器视图<>为arg0,ARG1观,诠释ARG2,长ARG3){
        AlertDialog.Builder alertbox =新AlertDialog.Builder(本);    //设置为显示该消息
      alertbox.setMessage(请做好准备)显示()。
    }

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

    }    

解决方案

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


    }

这篇关于如何写在Android中的ListView onclick事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆