在RSS Feed中将图像添加到ListView

时间:2018-09-25 06:31:37

标签: android image listview parsing rss

这是我当前针对我的应用的RSS Feed。我希望它也显示来自RSS Feed的图像。目前,该图片位于RSS代码中的描述标签中。我希望能够从RSS中提取这些图像并将其保存在我的应用中。

public class MainActivity extends AppCompatActivity {
private Button button;
ListView lvRSS;
ArrayList<String> titles;
ArrayList<String> links;
ArrayList<String> img;

@Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
    this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);

    button = findViewById(R.id.navBar);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            openMenu();
        }
    });

    lvRSS = findViewById(R.id.lvRSS);
    titles = new ArrayList<>();
    links = new ArrayList<>();
    img = new ArrayList<>();

    lvRSS.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Uri uri = Uri.parse(links.get(position));
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            startActivity(intent);
        }
    });

    new ProcessInBackground().execute();
}
public void openMenu() {
    Intent intentMenu = new Intent(this, menuActivity.class);
    startActivity(intentMenu);
}
public InputStream getInputStream(URL url)
{
    try
    {
        return url.openConnection().getInputStream();
    }
    catch (IOException e)
    {
        return null;
    }
}
public class ProcessInBackground extends AsyncTask<Integer, Void, Exception>
{
    ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);
    Exception exception = null;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        progressDialog.setMessage("Loading");
        progressDialog.show();
    }
    @Override
    protected Exception doInBackground(Integer... params) {
        try
        {
            URL url = new URL("http://www.scotscollege.school.nz/category/sports-news/feed/");
            XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
            factory.setNamespaceAware(false);
            XmlPullParser xmlPullParser = factory.newPullParser();
            xmlPullParser.setInput(getInputStream(url), "UTF_8");
            boolean insideItem = false;
            int eventType =  xmlPullParser.getEventType();
            while (eventType != xmlPullParser.END_DOCUMENT)
            {
                if (eventType == XmlPullParser.START_TAG)
                {
                    if (xmlPullParser.getName().equalsIgnoreCase("item"))
                    {
                        insideItem = true;
                    }
                    else if (xmlPullParser.getName().equalsIgnoreCase("title"))
                    {
                        if (insideItem)
                        {
                            titles.add(xmlPullParser.nextText());
                        }
                    }
                    else if (xmlPullParser.getName().equalsIgnoreCase("guid"))
                    {
                        if (insideItem)
                        {
                            links.add(xmlPullParser.nextText());
                        }
                    }
                    else if (xmlPullParser.getName().equalsIgnoreCase("img"))
                    {
                        if (insideItem)
                        {
                            img.add(xmlPullParser.nextText());
                        }
                    }
                }
                else if (eventType == XmlPullParser.END_TAG && xmlPullParser.getName().equalsIgnoreCase("item"))
                {
                    insideItem = false;
                }
                eventType = xmlPullParser.next();
            }
        }
        catch (MalformedURLException e)
        {
            exception = e;
        }
        catch (XmlPullParserException e)
        {
            exception = e;
        }
        catch (IOException e)
        {
            exception = e;
        }


        return exception;
    }
    @Override
    protected void onPostExecute(Exception s) {
        super.onPostExecute(s);

        ArrayAdapter<String> adapter = new ArrayAdapter<>(MainActivity.this, android.R.layout.simple_list_item_1, titles);
        lvRSS.setAdapter(adapter);

        progressDialog.dismiss();
    }
}
}

任何帮助将不胜感激。 RSS提要的链接为http://www.scotscollege.school.nz/category/sports-news/feed/。 如果您想查看RSS代码。

感谢您的任何帮助。

0 个答案:

没有答案