简体   繁体   English

如何在listdialog的click事件中获取选定的项目?

[英]How can I get selected items on listdialog's click event?

I am implementing listdialog. 我正在实现listdialog。 I want to print a toast message when I click on a particular listitem. 当我单击特定列表项时,我想打印一条祝酒消息。 I want to print message on toast or else I want to perform some action. 我想在烤面包上打印消息,否则我想执行一些操作。 My code is like this: 我的代码是这样的:

ListView lv;
DbHelper dbh;
final String ar[]={"Delete","Update"};
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_view_all);
    lv = (ListView) findViewById(R.id.listView);
    dbh = new DbHelper(ViewAllActivity.this);

    ArrayList<DoctorPojo> arraylist = dbh.getData();
    ArrayAdapter<DoctorPojo> adapter=new ArrayAdapter<DoctorPojo>(ViewAllActivity.this,android.R.layout.simple_list_item_1,arraylist);
    lv.setAdapter(adapter);

    lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener()
    {
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id)
        {

            final AlertDialog.Builder alert=new AlertDialog.Builder(ViewAllActivity.this);
            alert.setTitle("Which Action You Want to Perform...!!!");
            alert.setItems(ar, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    if(ar[0] == alert.) {
                        Toast.makeText(ViewAllActivity.this, " Delete is pressed", Toast.LENGTH_LONG).show();
                    }
                    else
                    {
                        Toast.makeText(ViewAllActivity.this, " Update is pressed", Toast.LENGTH_LONG).show();
                    }
                }
            });
            alert.create().show();
            return false;
        }
    });
    }

}

Please help; 请帮忙; I am confused about the if condition. 我对if条件感到困惑。

If I'm understanding your question correctly, you need to implement ListView's setOnItemClickListener(AdapterView.OnItemClickListener listener) instead. 如果我正确理解了您的问题,则需要实现ListView的setOnItemClickListener(AdapterView.OnItemClickListener监听器) setOnItemLongClickListener is used for pressing and holding down on the list item. setOnItemLongClickListener用于按住列表项。

For ListView's setOnItemClickListener() implementation, you could do something like the following: 对于ListView的setOnItemClickListener()实现,可以执行以下操作:

lv.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
             //YOUR ACTION HERE
             //or show a toast instead:
             Toast.makeText(ViewAllActivity.this, "Your message here", Toast.LENGTH_LONG).show();
        }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM