开发者

How to extract selected item from ListView?

I have a Listview that can only select one item. When that item gets clicks, it runs an AsyncTask. In the onPostExecute(), an AlertBox Dialog pops up. 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. Any help would be appreciated, and thank you in advance.

Here is my ListView setup.

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

    list.setOnItemClickListener(new OnItemClickListener() 
    {
        pu开发者_开发百科blic 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. My goal is to get the selected item (or text from TextView associated with selected item) in the Title() method in the onPostExecute().

    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!


If your Task is defined within the scope of your Activity, you can use the final keyword:

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


You could use the position argument of your onItemClick listener to fetch the clicked item from your data source, then pass this data to the AsyncTask object, and consume it there (display it in the Alert box)


If the AsynTask is inner to your activity then you can access the listview member and get the selected item. Call

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

Or

You can pass the object to the AsyncTask through the parameters. Pass the title string to the Size constructor. Like this

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


if you just want to create an alert dialog you don't need the AsyncTask.. Just add the code getSelectedItem in your onListItemClick and create an alert from that..

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜