Saturday, 16 June 2012

Read an xml file in android


Read an xml file in android


package com.quiz;

import java.io.IOException;

import org.xmlpull.v1.XmlPullParserException;

import android.app.Activity;
import android.content.res.XmlResourceParser;

import android.os.Bundle;
import android.util.Log;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
public class QuizScoresActivity extends QuizActivity {
    /** Called when the activity is first created. */
   @Override
  public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.scores);
        TabHost host = (TabHost) findViewById(R.id.tabhost1);
        host.setup();
        TabSpec allscorestab = host.newTabSpec("alltab");
        allscorestab.setIndicator(getResources().getString(R.string.UserScores));
       allscorestab.setContent(R.id.scrollviewuser);
        host.addTab(allscorestab);
   
        TabSpec allscoresfriendstab = host.newTabSpec("allscoresfriend");
        allscoresfriendstab.setIndicator(getResources().getString(R.string.FriendScores));
        allscoresfriendstab.setContent(R.id.scrollviewfriends);
        host.addTab(allscoresfriendstab);
      //  host.setCurrentTabByTag("alltab");
        TableLayout allScoresTable = (TableLayout) findViewById(R.id.tablelayout1);
        TableLayout friendScoresTable = (TableLayout) findViewById(R.id.tablelayout2);
 

        initializeHeaderRow(allScoresTable);
        initializeHeaderRow(friendScoresTable);
     
        XmlResourceParser mockAllScores = getResources().getXml(R.xml.allscores);
        XmlResourceParser mockFriendScores = getResources().getXml(R.xml.friendscores);
        try {
            processScores(allScoresTable, mockAllScores);
            processScores(friendScoresTable, mockFriendScores);
        } catch (Exception e) {
         //   Log.e(DEBUG_TAG, "Failed to load scores", e);
        }
    }
 
 
   private void initializeHeaderRow(TableLayout scoreTable) {
       // Create the Table header row
       TableRow headerRow = new TableRow(this);
     //  int textColor = getResources().getColor(R.color.logo_color);
//       float textSize = getResources().getDimension(R.dimen.help_text_size);
       addTextToRowWithValues(headerRow, getResources().getString(R.string.username));
       addTextToRowWithValues(headerRow, getResources().getString(R.string.score));
       addTextToRowWithValues(headerRow, getResources().getString(R.string.rank));
       scoreTable.addView(headerRow);
   }
 
 
 
   private void processScores(final TableLayout scoreTable, XmlResourceParser scores) throws XmlPullParserException,
   IOException {
int eventType = -1;
boolean bFoundScores = false;
// Find Score records from XML
while (eventType != XmlResourceParser.END_DOCUMENT) {
   if (eventType == XmlResourceParser.START_TAG) {
       // Get the name of the tag (eg scores or score)
       String strName = scores.getName();
       if (strName.equals("score")) {
           bFoundScores = true;
           String scoreValue = scores.getAttributeValue(null, "score");
           String scoreRank = scores.getAttributeValue(null, "Rank");
           String scoreUserName = scores.getAttributeValue(null, "username");
           insertScoreRow(scoreTable, scoreValue, scoreRank, scoreUserName);
       }
   }
   eventType = scores.next();
}
 
 
if (bFoundScores == false) {
    final TableRow newRow = new TableRow(this);
    TextView noResults = new TextView(this);
    noResults.setText(getResources().getString(R.string.no_scores));
    newRow.addView(noResults);
    scoreTable.addView(newRow);
}
}
   private void insertScoreRow(final TableLayout scoreTable, String scoreValue, String scoreRank, String scoreUserName) {
       final TableRow newRow = new TableRow(this);
      // int textColor = getResources().getColor(R.color.title_color);
       //float textSize = getResources().getDimension(R.dimen.help_text_size);
       addTextToRowWithValues(newRow, scoreUserName);
       addTextToRowWithValues(newRow, scoreValue);
       addTextToRowWithValues(newRow, scoreRank);
       scoreTable.addView(newRow);
   }
 
   private void addTextToRowWithValues(final TableRow tableRow, String text) {
       TextView textView = new TextView(this);
   //    textView.setTextSize(textSize);
    //   textView.setTextColor(textColor);
       textView.setText(text);
       tableRow.addView(textView);
   }
 
 
}

Scores.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/index1"
    android:orientation="vertical" >

    
    <RelativeLayout
        android:id="@+id/relativeLayout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:src="@drawable/a" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:text="@string/menu"
            android:textColor="@color/white" />

        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:src="@drawable/b" />
    </RelativeLayout>

    <TabHost
        
        android:id="@+id/tabhost1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical" >

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >

            <ScrollView
                android:id="@+id/scrollviewuser"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:scrollbars="vertical" >

                <TableLayout
                    android:id="@+id/tablelayout1"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:stretchColumns="*" >
                </TableLayout>
            </ScrollView>
            <ScrollView
                android:id="@+id/scrollviewfriends"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:scrollbars="vertical" >

                <TableLayout
                    android:id="@+id/tablelayout2"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:stretchColumns="*" >
                </TableLayout>
            </ScrollView>
        </FrameLayout>
        </LinearLayout>
     
    </TabHost>
   </LinearLayout>


Place this by creating xml folder in android

allscores.xml

<?xml version="1.0" encoding="utf-8"?>
<scores>
<score username="A" score="1234" Rank="1" />
<score username="B" score="1235" Rank="2" />
<score username="C" score="1236" Rank="3" />
<score username="D" score="1237" Rank="4" />
</scores>
    

Friendscores.xml

<?xml version="1.0" encoding="utf-8"?>
<scores>
<score username="E" score="1238" Rank="11" />
<score username="F" score="1239" Rank="12" />
<score username="G" score="1230" Rank="13" />
<score username="H" score="1245" Rank="14" />
</scores>
    


No comments:

Post a Comment

Pass a HashMap from Angular Client to Spring boot API

This example is for the case where fileData is very huge and in json format   let map = new Map<string, string>()      map.set(this.ge...