2

我必须从 xml 提要中获取数据并显示在 android 应用程序上。

我的 xml 提要看起来像:

<root>
 <Categories>
  <Category name="Books">
   <Articles>
  <article articleid="4537" title="Android" />
  <article articleid="4560" title="Java" />
   <article articleid="4585" title="PHP" />
   <article articleid="4731" title="MySql" />
  </Articles>
 </Category>
  <Category name="Names">
   <Articles> 
      <article articleid="266" title="Dita" />
       <article articleid="268" title="Frieda" />
        <article articleid="269" title="Demi" />
        <article articleid="271" title="Shyamalan" />
          <article articleid="269" title="Krish" /></Articles>
         </Category>
          </Categories></root>

在这里我必须显示类别名称。还必须显示属于类别的文章标题。我希望显示的输出是:

      Books

      Android  Java  PHP  MySql

     Names

     Dita  Frieda  Demi  Shyamalan Krish

我使用以下代码:

static final String KEY_TITLE = "Category";
   static final String KEY_ARTICLE = "article";
      @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
        XMLParser parser = new XMLParser();
        String xml = parser.getXmlFromUrl(URL); // getting XML from URL
        Document doc = parser.getDomElement(xml); // getting DOM element

        NodeList nl = doc.getElementsByTagName(KEY_TITLE);
        NodeList nl1 = doc.getElementsByTagName(KEY_ARTICLE);
                System.out.println(nl.getLength());
        // looping through all song nodes &lt;song&gt;
        for (int i = 0; i < nl.getLength(); i++) {
            // creating new HashMap
            System.out.println(nl.getLength());
            HashMap<String, String> map = new HashMap<String, String>   ();
            map.put( KEY_TITLE,((Element)nl.item(i)).getAttribute("name"));
           for (int j = 0; j < nl1.getLength(); j++) {
              map.put( KEY_ARTICLE,((Element)nl1.item(j)).getAttribute("title"));
                }
           songsList.add(map);
          }

在这里我必须运行应用程序意味着显示文章标题。但文章标题没有正确显示。我当前的 o/p 如下所示:

     Books

      Krish  Krish  

     Names

     Krish  Krish 

最后一篇文章标题显示在所有类别中。为什么会出现这些错误???请帮助我...我该如何解决这些错误???请给我一些想法???

编辑:

我已经改变了我的代码,如下所示:

     public class MainActivity extends Activity {
      static final String URL = "xxx";

 static final String KEY_TITLE = "Category";
 static final String KEY_ARTICLE = "article";
 static final String KEY_IMAGE = "image";
 LazyAdapter adapter;

 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

     final ArrayList<HashMap<String, List<String>>> songsList = new ArrayList<HashMap<String, List<String>>>();
        XMLParser parser = new XMLParser();
        String xml = parser.getXmlFromUrl(URL); // getting XML from URL
        Document doc = parser.getDomElement(xml); // getting DOM element

        NodeList nl = doc.getElementsByTagName(KEY_TITLE);
        NodeList nl1 = doc.getElementsByTagName(KEY_ARTICLE);
        NodeList nl2 = doc.getElementsByTagName(KEY_IMAGE);
        System.out.println(nl.getLength());
        HashMap<String, List<String>> map = new HashMap<String, List<String>>();
        for(int i = 0; i < nl.getLength(); i++) {
            String name = ((Element) nl.item(i)).getAttribute("name");
             populateMap(map, name, KEY_TITLE);

           for(int j = 0; j < nl1.getLength(); j++) {
                String title=((Element) nl1.item(j)).getAttribute("title");
                populateMap(map, title, KEY_ARTICLE);
                String url=((Element) nl2.item(j)).getAttribute("url");
                populateMap(map, url, KEY_IMAGE);
                } 
           songsList.add(map); 
       } 
    GridView list = (GridView) findViewById(R.id.listView1);
         adapter = new LazyAdapter(this, songsList);
         list.setAdapter(adapter);
   private void populateMap(HashMap<String, List<String>> map, String value,
        String key) {
    // TODO Auto-generated method stub
     List<String> myList;
     if(!map.containsKey(key)) {
         myList = new ArrayList<String>();
         myList.add(value);
         map.put(key, myList);
     } else {
         myList = map.get(key);
         myList.add(value);
     }

现在我必须运行该应用程序,这意味着我得到了如下所示的 o/p:

[Books,Names]
[Android,Java,PHP,Mysql,Dita,Frieda,Demi,Shyamalan,Krish]  [Android,Java,PHP,Mysql,Dita,Frieda,Demi,Shyamalan,Krish]

[Books,Names]
[Android,Java,PHP,Mysql,Dita,Frieda,Demi,Shyamalan,Krish]  [Android,Java,PHP,Mysql,Dita,Frieda,Demi,Shyamalan,Krish]

请为我提供一些想法来获得正确的 o/p(我需要的设计)。

这是我的 LazyAdpter 文件:

         public class LazyAdapter extends BaseAdapter {
            private Activity activity;
            private ArrayList<HashMap<String, List<String>>> data;
            private static LayoutInflater inflater = null;
           public ImageLoader imageLoader;
          ListAdapter listViewAdapter;
            public LazyAdapter(Activity a, ArrayList<HashMap<String, List<String>>> songsList) {
                   activity = a;
                  data = songsList;
                  inflater = (LayoutInflater) activity
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                            imageLoader = new ImageLoader(activity.getApplicationContext());
                }

        public int getCount() {
                    return data.size();
                   }

             public Object getItem(int position) {
              return position;
            }

         public long getItemId(int position) {
            return position;
            }

            public View getView(int position, View convertView, ViewGroup parent) {
            View vi = convertView;
                vi = inflater.inflate(R.layout.list_row,parent, false);


                TextView title = (TextView) vi.findViewById(R.id.title); 
                Gallery listview = (Gallery) vi .findViewById(R.id.model);

            HashMap<String, List<String>> item = new HashMap<String, List<String>>();
             item = data.get(position);
             title.setText(item.get(MainActivity.KEY_TITLE).toString());
                  listViewAdapter = new ListAdapter(activity, data);
                 listview.setAdapter(listViewAdapter);
                     return vi;
            }

这是 ListAdapter 类:

      public View getView(int position, View convertView, ViewGroup parent) {
           View vi = convertView;
            vi = inflater.inflate(R.layout.listrow,parent, false);
            TextView title = (TextView) vi.findViewById(R.id.textView1); 
            HashMap<String, List<String>> item = new HashMap<String, List<String>>();
            item = data.get(position);
        title.setText(item.get(MainActivity.KEY_ARTICLE).toString());
         return vi;
            }
4

2 回答 2

3
Map<String,List<String>> categoryToArticlesMap = new HashMap<String,List<String>>();
NodeList categoryNd = doc.getElementsByTagName("Category");
for (int i = 0; i < categoryNd.getLength(); i++) 
{
    Element e = (Element) categoryNd.item(i);
    String categoryName = e.getAttribute("name"); 
    NodeList articlesNd = e.getElementsByTagName("Articles");
    Element e1 = (Element) articlesNd.item(0);
    NodeList articleNd = e1.getElementsByTagName("article");
    List<String> articles = new ArrayList<String>();
    for (int j = 0; j < articleNd.getLength(); j++) {
        Element articleNode = (Element) articleNd.item(j);
        String articleName = articleNode.getAttribute("title");
        articles.add(articleName);
    }
    categoryToArticlesMap.put(categoryName, articles);
}

这将为您提供所需的输出。

于 2013-04-01T16:16:17.510 回答
0

在这一行map.put( KEY_ARTICLE,((Element)nl1.item(j)).getAttribute("title"));中,您将重写地图中存在的先前值。更改ArrayList<HashMap<String, String>> songsListArrayList<HashMap<String, List<String>>> songsList添加每个值以列出并将其放入地图。
即使你这样做,你也会得到输出,

Books    
Android Java PHP MySQL Dita Frieda Demi Shyamalan Krish     
Names     
Android Java PHP MySQL Dita Frieda Demi Shyamalan Krish 

要获得所需的输出,您应该考虑article每个下的节点列表,Category而不是获取article文档中的所有标签。

于 2013-04-01T05:13:11.030 回答