Search code examples
androidlistviewandroid-alertdialogrss-reader

android get rssitem description from listview


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

Solution

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