从ArrayList <object>获取指定项(在由解析的XML数据填充的列表之后)</object>

时间:2014-01-20 16:11:17

标签: android xml arraylist

我从这里获得了解析XML所需的代码:http://androidcookbook.com/Recipe.seam?recipeId=2217&title=Parsing%20an%20XML%20document%20using%20an%20XmlPullParser

我对代码做了一点修改,这里是代码:

public class CobaXMLPullParser extends Activity {

protected static final int DIALOG_KEY = 0;
ListView mListView;
ProgressDialog mProgressDialog;

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

    requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
    this.setProgressBarIndeterminateVisibility(false);

    setContentView(R.layout.main);
    mListView = (ListView) findViewById(R.id.listView1);
    mListView.setTextFilterEnabled(true);


            LoadRecipesTask2 mLoadRecipesTask = new LoadRecipesTask2();
            String url = "http://androidcookbook.com/seam/resource/rest/recipe/list";
            showDialog(DIALOG_KEY);                                                     // 1
            mLoadRecipesTask.execute(url, url, url, url, url);                          // 2


}

@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
    case DIALOG_KEY:                                                               // 1
        mProgressDialog = new ProgressDialog(this);
        mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);         // 2
        mProgressDialog.setMessage("Retrieving recipes...");                       // 3
        mProgressDialog.setCancelable(false);                                      // 4
        return mProgressDialog;
    }
    return null;
}


public static ArrayList<Datum> parse(String url) throws IOException, XmlPullParserException {
    final ArrayList<Datum> results = new ArrayList<Datum>();

    URL input = new URL(url);

    XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
    factory.setNamespaceAware(true);
    XmlPullParser xpp = factory.newPullParser();

    xpp.setInput(input.openStream(), null);
    int eventType = xpp.getEventType();
    String currentTag = null;
    Integer id = null;
    String title = null;
    while (eventType != XmlPullParser.END_DOCUMENT) {
        if (eventType == XmlPullParser.START_TAG) {
            currentTag = xpp.getName();
        } else if (eventType == XmlPullParser.TEXT) {
            if ("id".equals(currentTag)) {
                id = Integer.valueOf(xpp.getText());
            }
            if ("title".equals(currentTag)) {
                title = xpp.getText();
            }
        } else if (eventType == XmlPullParser.END_TAG) {
            if ("recipe".equals(xpp.getName())) {
                results.add(new Datum(id, title));
            }
        }
        eventType = xpp.next();
    }
    return results;
}


public static ArrayList<Datum> parse2(String url) throws IOException, XmlPullParserException {
    final ArrayList<Datum> results = new ArrayList<Datum>();

    URL input = new URL(url);

    XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
    factory.setNamespaceAware(true);
    XmlPullParser xpp = factory.newPullParser();

    xpp.setInput(input.openStream(), null);
    xpp.nextTag();
    xpp.require(XmlPullParser.START_TAG, null, "recipes");
    while (xpp.nextTag() == XmlPullParser.START_TAG) {
        xpp.require(XmlPullParser.START_TAG, null, "recipe");

        xpp.nextTag();
        xpp.require(XmlPullParser.START_TAG, null, "id");
        Integer id = Integer.valueOf(xpp.nextText());
        xpp.require(XmlPullParser.END_TAG, null, "id");

        xpp.nextTag();
        xpp.require(XmlPullParser.START_TAG, null, "title");
        String title = xpp.nextText();
        xpp.require(XmlPullParser.END_TAG, null, "title");

        xpp.nextTag();
        xpp.require(XmlPullParser.END_TAG, null, "recipe");

        results.add(new Datum(id, title));
    }
    xpp.require(XmlPullParser.END_TAG, null, "recipes");

    return results;
}


protected class LoadRecipesTask2 extends AsyncTask<String, Integer, ArrayList<Datum>> {

    @Override
    protected void onPreExecute() {
        mProgressDialog.show();                                                          // 1
    }

    @Override
    protected ArrayList<Datum> doInBackground(String... urls) {
        ArrayList<Datum> datumList = new ArrayList<Datum>();
        for (int i = 0; i < urls.length; i++) {                                          // 2
            try {
                datumList = parse(urls[i]);
                publishProgress((int) (((i+1) / (float) urls.length) * 100));            // 3
            } catch (IOException e) {
                e.printStackTrace();
            } catch (XmlPullParserException e) {
                e.printStackTrace();
            }
        }
        return datumList;
    }


    @Override
    protected void onProgressUpdate(Integer... values) {                                 // 4
        mProgressDialog.setProgress(values[0]);                                          // 5
    }

    @Override
    protected void onPostExecute(ArrayList<Datum> result) {
        mListView.setAdapter(new ArrayAdapter<Datum>(CobaXMLPullParser.this, R.layout.list_item, result));
        mProgressDialog.dismiss();                                                       // 6
    }
}

}

这是基准类:

public class Datum {

int id;
String title;

public Datum(int id, String title) {
    this.id = id;
    this.title = title;
}

public String toString() {
    return title;
}

public int getId() {
    return id;
}

public String getTitle() {
    return title;
}

}

现在,我需要的是从该ArrayList获取item的值(例如:获取第三个标题) 我很困惑得到它,因为如果我使用datumlist.get(3),它不能在textview中打印,因为不返回一个字符串,任何人都可以帮忙吗?

2 个答案:

答案 0 :(得分:1)

它将返回一个Datum Object,其中包含一个String。

但首先datumlist.get(3)将返回第4个对象而不是第3个对象,因为索引是从零开始的。

如果要检索第3个字符串,则必须使用datumlist.get(2).getTitle()

答案 1 :(得分:0)

ArrayList包含datum对象,因此您只需调用getTitle

即可
datumlist.get(3).getTitle()