简体   繁体   English

如何从ListView中提取所选项?

[英]How to extract selected item from ListView?

I have a Listview that can only select one item. 我有一个只能选择一个项目的Listview。 When that item gets clicks, it runs an AsyncTask. 当该项获得点击时,它会运行AsyncTask。 In the onPostExecute(), an AlertBox Dialog pops up. 在onPostExecute()中,弹出一个AlertBox对话框。 But what I'm trying to do is get the selected item to display inside the alertBox and I've tried everything I could think of. 但我要做的是让所选项目显示在alertBox内部,我已经尝试了我能想到的一切。 Any help would be appreciated, and thank you in advance. 任何帮助将不胜感激,并提前感谢您。

Here is my ListView setup. 这是我的ListView设置。

    Public class MyClass extends Activity
    {
    list.setAdapter(new ArrayAdapter<String>(this, R.layout.vacation_tracks, vacation_menu));

    list.setOnItemClickListener(new OnItemClickListener() 
    {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id)
        {

            for(int i = 0; i<vacation_menu.length; i++)
            {

                if(((TextView) view).getText().equals(vacation_menu[i]))
                {

                    Sizes work = new Sizes();
                    work.execute(tempLink);
            } 
        }
    }); 
   }

And this is my AsyncTask class. 这是我的AsyncTask类。 My goal is to get the selected item (or text from TextView associated with selected item) in the Title() method in the onPostExecute(). 我的目标是在onPostExecute()的Title()方法中获取所选项(或TextView中与所选项关联的文本)。

    Private Class Sizes extends AsyncTask<URL, Void, Float>
    {
        protected float doInBackground(URL...urls)
        {
             //gets url.getContentLength();
        }

        protected void onPostExecute(Float result)
        {
            AlertDialog.Builder alertbox = new AlertDialog.Builder(Vacation.this);
            alertbox.setMessage( Title( ITEM FROM LISTVIEW     ) );
            alertbox.setPositiveButton("Yes", new DialogInterface.OnClickListener() 
            {
                public void onClick(DialogInterface arg0, int arg1) 
                {

                }
            });
            alertbox.setNegativeButton("No", new DialogInterface.OnClickListener()
            {
                public void onClick(DialogInterface arg0, int arg1)
                {

                }
            });
            alertbox.show();

    } 


}

Thank you again for any help! 再次感谢您的帮助!

您可以使用onItemClick侦听器的position参数从数据源中获取单击的项目,然后将此数据传递给AsyncTask对象,并在那里使用它(在Alert框中显示)

If your Task is defined within the scope of your Activity, you can use the final keyword: 如果您的任务是在Activity的范围内定义的,则可以使用final关键字:

final String alertBoxTitle = vacation_menu[i];
Sizes work = new Sizes();
work.execute(tempLink);

and

alertbox.setMessage(alertBoxTitle);

If your Task is not within the scope of your Activity you could pass the title as an argument or via a setter. 如果您的任务不在您的活动范围内,您可以将标题作为参数或通过设置者传递。 Setter seems easier in your case. 在你的情况下,塞特似乎更容易。

Within your Task: 在你的任务中:

String title;

public void setTitle(String title) {
  this.title = title;
}

protected void onPostExecute(Float result) {
  AlertDialog.Builder alertbox = new AlertDialog.Builder(Vacation.this);
  alertbox.setMessage(title);
  // ...
}

Use it like this: 像这样使用它:

Sizes work = new Sizes();
work.setTitle(vacation_menu[i]);
work.execute(tempLink);

If the AsynTask is inner to your activity then you can access the listview member and get the selected item. 如果AsynTask在您的活动内部,那么您可以访问listview成员并获取所选项目。 Call 呼叫

mListView.getSelectedItem(); // returns the object associated with this item.

Or 要么

You can pass the object to the AsyncTask through the parameters. 您可以通过参数将对象传递给AsyncTask Pass the title string to the Size constructor. 将标题字符串传递给Size构造函数。 Like this 像这样

Sizes work = new Sizes(mListView.getSelectedItem().getTitle());
work.execute(tempLink); 

如果你只是想创建一个警告对话框,你不需要AsyncTask ..只需在你的onListItemClick中添加代码getSelectedItem并从中创建一个警告..

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

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