hashmap in android xml parsing

匆匆过客 提交于 2019-12-11 03:32:45

问题


I have to fetch the data from xml feed and display on android application.

my xml feed is looking like:

<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>

Here i have to display category name.also have to display the article title for belonging category.i wish to display the output is:

      Books

      Android  Java  PHP  MySql

     Names

     Dita  Frieda  Demi  Shyamalan Krish

I have using below code:

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

Here i have to run the app means the article title is displayed.but the article title is didn't display correctly.my current o/p is looking like below:

     Books

      Krish  Krish  

     Names

     Krish  Krish 

The last article title is dispalyed on all categories.Why these error is occurring ??? please help me...how can i resolve these error ??? please provide me some ideas ???

EDIT:

I have changed my code like below:

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

Now i have to run the app means am getting the o/p like below:

[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]

pls provide me some idea to get the correct o/p(my needed design).

This is my LazyAdpter file :

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

This is ListAdapter class:

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

回答1:


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

This will give you the required output.




回答2:


Here in this line map.put( KEY_ARTICLE,((Element)nl1.item(j)).getAttribute("title")); you are over writing the previous value present in the map. Change ArrayList<HashMap<String, String>> songsList to ArrayList<HashMap<String, List<String>>> songsList and add each value to list and put it to the map.
Even if you do this you will get output as,

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

To get the desired output, you should consider the nodelist for article under each Category instead of getting all the article tags in the document.



来源:https://stackoverflow.com/questions/15737756/hashmap-in-android-xml-parsing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!