android 从列表视图中获取 rssitem 描述

android get rssitem description from listview

在我的 android 应用程序中,我使用了一个 ListView,该 ListView 由从网页 url 获取的 RSSItems 填充;列表视图仅显示 rssitem 的标题和发布日期。

我会意识到,当我单击列表视图的 rssitem 时,应用程序会显示一个警告对话框,在消息框中显示 rssitem 标题的描述。

如何实现?

这里是代码:

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

这里需要修改

 alert.show();

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

编辑:

在执行异步任务后,将其从循环中移除并移动到 onCreate()

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

将此 ArrayList<RssItem> rssItems public 设为静态并用作

rssItems = (ArrayList<RssItem>) result.getRssItems();