-1

In my android app I'm using a ListView populated by RSSItems taken from a webpage url; the listview shows me only title and pubdate of rssitem.

I would realize that when I click on rssitem of the listview, app shows me an alert dialog showing me in message box the descritpion of the rssitem title.

How can I realize it?

Here the code:

public class MainActivity extends ActionBarActivity{

    private ListView listview;
    URL url = null;
    RssFeed feed = null;
    AlertDialog.Builder alert;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listview = (ListView) findViewById(R.id.listView);
        alert = new AlertDialog.Builder(MainActivity.this);

        try {
            url = new URL("http://www.unikore.it/index.php/ingegneria-informatica-home?format=feed");
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        new ReadRssTask().execute(url);
    }

    private class ReadRssTask extends AsyncTask<URL, Void, RssFeed> {

        @Override
        protected RssFeed doInBackground(URL... params) {
            RssFeed result = null;
            URL url = params[0];
            if (!TextUtils.isEmpty(url.toString())) {
                try {
                    result = RssReader.read(url);
                } catch (SAXException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return result;
        }

        @Override
        protected void onPostExecute(RssFeed result) {
            if (result != null) {
                ArrayList<RssItem> rssItems = (ArrayList<RssItem>) result.getRssItems();
                ArrayList<String> arrayList = new ArrayList<String>();

                for (final RssItem rssItem : rssItems) {
                    arrayList.add(rssItem.getTitle()+"\n"+rssItem.getPubDate()+"\n");
                    ArrayAdapter<String> lista = new ArrayAdapter<String>(getBaseContext(),android.R.layout.simple_list_item_1,arrayList);
                    listview.setAdapter(lista);
                    listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
                            alert.setTitle(listview.getItemAtPosition(position).toString());
                            alert.setMessage(); //HERE I SHOULD SET THE rssItem.getDescription()
                            alert.show();
                        }
                    });
                    Log.i("RSS Reader", rssItem.getTitle());
                }
            }
        }
    }
}
xXJohnRamboXx
  • 739
  • 3
  • 10
  • 24

1 Answers1

1

You need to change here

 alert.show();

to

 AlertDialog dialog = alert.create(); // You missed this
 dialog.show():

Edit:

Remove this from loop and move to onCreate() after your asynctask executed.

  listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
                        alert.setTitle(listview.getItemAtPosition(position).toString());
                        alert.setMessage(rssItems.get(position).getDescription()); //HERE YOU SHOULD SET THE rssItem.getDescription()
                        alert.show();
                    }
                });

Make this ArrayList<RssItem> rssItems public static and use as

rssItems = (ArrayList<RssItem>) result.getRssItems();
Piyush
  • 18,895
  • 5
  • 32
  • 63
  • the problem isn't show alertdialog but showing rssitem details into message box of the alertdialog – xXJohnRamboXx Apr 29 '15 at 08:28
  • i would show into message box of alertdialog the rssitem description..i have to write something like alert.setmessage(rssItem.getDescription()) – xXJohnRamboXx Apr 29 '15 at 08:32
  • Yes it will show you as a alert dialog message. And if you want to customize then you need to inflate your custom view to alert dialog. Are you able to get description in dialog message? – Piyush Apr 29 '15 at 08:33
  • i have getDescription() method but it can be invoked by an rssItem object. I don't know how call this method for getting detail information of the selected rssitem – xXJohnRamboXx Apr 29 '15 at 08:39
  • Have you created `getDescription()` method in `RssFeed` class? That method only accessible to the arraylist of `RssFeed` class. – Piyush Apr 29 '15 at 08:42
  • yeah i have getDescription() method in RssFeed class. This is the implementation: ['code']public String getDescription() { return description; } – xXJohnRamboXx Apr 29 '15 at 08:46
  • In my listview i see only rssItem Title. If i click one of rssItem Title i should show into the alerdialog's message box the details of that rssItem. I don't know how show the details of the selected rssItem title – xXJohnRamboXx Apr 29 '15 at 08:50
  • So use this `alert.setMessage(rssItem.getDescription());` – Piyush Apr 29 '15 at 08:53
  • i tried this way but when i click on rssItem title it shows me details of the last item found by for loop :/ – xXJohnRamboXx Apr 29 '15 at 08:57
  • Here you are using `ArrayList rssItems = (ArrayList) result.getRssItems();` so why are you use for loop again. Tell me the size for `rssItems` – Piyush Apr 29 '15 at 09:07
  • 1
    Wel come. Glad to help you!! – Piyush Apr 29 '15 at 09:21