获取更多关于arraylist的信息

时间:2014-11-13 21:57:09

标签: android xml arraylist xml-parsing

我正在开发一个Android应用程序,用于从Web上的XML文件中读取一些信息。起初,我有一个解析问题。现在我的问题是我的代码只读取一个信息。我创建了我的课程MyFilm。如何在我的arraylist中插入该文件中的所有信息。提前谢谢。

public class FilmListActivity extends ListActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    new WebService() {

        @Override
        protected ArrayList<MyFilm> doInBackground(Object... params) {
            ArrayList<MyFilm> arr = new ArrayList<MyFilm>();
            Intent intent = getIntent();
            String pkg = getPackageName();
            try {
                MyFilm parametriRicerca = new MyFilm();
                parametriRicerca = (MyFilm) intent.getSerializableExtra(pkg+".MyFilm");
                XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
                factory.setNamespaceAware(true);
                XmlPullParser xpp = factory.newPullParser();

                URL input = new URL("http://whatdowedo.altervista.org/griffith_list2.xml");
                xpp.setInput(input.openStream(), null);

                int eventType = xpp.getEventType();
                String currentTag = null;

                MyFilm tmp = new MyFilm();
                String title = null;
                String regista = null;
                String attore = null;
                String genere = null;
                String paese = null;
                while (eventType != XmlPullParser.END_DOCUMENT) {

                    if (eventType == XmlPullParser.START_TAG) {
                        currentTag = xpp.getName();
                    } else if (eventType == XmlPullParser.TEXT) {
                        if ("title".equals(currentTag)) {
                            title = xpp.getText().trim();

                        }
                        if("director".equals(currentTag)){
                            regista = xpp.getText().trim();

                        }
                        if("country".equals(currentTag)){
                            paese = xpp.getText().trim();

                        }
                        if("genre".equals(currentTag)){
                            genere = xpp.getText().trim();

                        }
                        if("cast".equals(currentTag)){
                            attore = xpp.getText().trim();

                        }
                        if(parametriRicerca.getTitle().equals(title)){
                            arr.add(parametriRicerca);
                        }
                    }
                    eventType = xpp.next();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return arr;
        }
    }.execute();
}

abstract class WebService extends AsyncTask<Object, MyFilm, ArrayList<MyFilm>> {

    public WebService() {
        super();
    }

    @Override
    protected void onPreExecute() {

    }

    @Override
    protected abstract ArrayList<MyFilm> doInBackground(Object... params);

    @Override
    protected void onPostExecute(ArrayList<MyFilm> result) {
        super.onPostExecute(result);
        FilmsAdapterView adapter = new FilmsAdapterView(FilmListActivity.this,result);
        setListAdapter(adapter);
    }

}

 @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

}

0 个答案:

没有答案