XMLResource Parser With Custem Listiew From Local XML In Android.

XMLResource Parser With Custem Listiew From Local XML In Android.

XMLResource Parser With Custem Listiew From Local XML In Android.


 Xml Resource parser from local xml in android

it's parsing data from localxml


 anylocation.xml
from xml foder

<?xml version="1.0" encoding="UTF-8"?>
<jointData>
    <city>
            <option value="">Any Location</option>
            <option value="1">Salem</option>
            <option value="2">Chennai</option>
            <option value="3">Kovai</option>
            <option value="4">Bangalore</option>
            <option value="5">Triandrum</option>
            <option value="6">Madurai</option>
            <option value="7">Salem</option>
            <option value="8">Chennai</option>
            <option value="9">Kovai</option>
            <option value="10">Bangalore</option>
            <option value="11">Triandrum</option>
            <option value="12">Madurai</option>
         
    </city></jointData>


XMLResourceActivity.java


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.XmlResourceParser;
import android.net.Uri;
import android.os.Bundle;
import android.provider.SyncStateContract.Columns;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import android.widget.ArrayAdapter;

import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.TextView;

public class XMLResourceActivity extends Activity{
    LocationStruct location;
    ArrayList<LocationStruct> locationList=new ArrayList<LocationStruct>();
    private ListView list;
    public void onCreate(Bundle bundle){
        super.onCreate(bundle);
        setContentView(R.layout.listview);
      
      
        try {
            getData();
        } catch (XmlPullParserException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
  
    public void getData() throws XmlPullParserException, IOException{
        HotelArrayAdapter htlAdapt ;
        //ArrayList<String> todoItems = new ArrayList<String>();
XmlResourceParser todolistXml = getResources().getXml(R.xml.anylocation);
      
        int eventType = -1;
         while (eventType != XmlPullParser.END_DOCUMENT)
         {
             if(eventType == XmlPullParser.START_TAG) {
                 String strNode = todolistXml.getName();
                 if (strNode.equals("option")) {
                      location=new LocationStruct();
                     String str=todolistXml.getAttributeValue(null, "value").trim();
                     String cit=todolistXml.nextText().trim();
                     location.setValue(str);
                     location.setCity(cit);
                     locationList.add(location);
                 }
             }
          
                 try {
                    eventType = todolistXml.next();
                } catch (XmlPullParserException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
        }
 list=(ListView)findViewById(R.id.listView1);

     htlAdapt = new HotelArrayAdapter(this, android.R.layout.simple_list_item_1, locationList);
     list.setAdapter(htlAdapt);
  
    }
    public class HotelArrayAdapter extends ArrayAdapter<LocationStruct> {

        private ArrayList<LocationStruct> summaryitems =new ArrayList<LocationStruct>();
        private Context ctx1;
      
        public HotelArrayAdapter(Context context, int textViewResourceId,ArrayList<LocationStruct> summaryitems)
        {
        super(context, textViewResourceId, summaryitems);
        this.summaryitems = summaryitems;
        this.ctx1 = context;
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent)
        {
        View v = convertView;
        if (v == null)
        {
        LayoutInflater vi = (LayoutInflater) getContext().getSystemService(
                        Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(R.layout.location, null);
        }
        final LocationStruct o = summaryitems.get(position);
          
        if (o != null) {
        TextView dname = (TextView) v.findViewById(R.id.textView1);
        RadioButton select=(RadioButton)v.findViewById(R.id.select_btn);
        //Button btn=(Button)v.findViewById(R.id.select_btn);
        Log.i("city", "city===========  "+o.getCity());
        Log.i("city", "city===========>>>>>>>>>>  "+o.getValue());
          
              dname.setText(o.getCity());        
                
                 
           
        }     
             return v;
        }
        }
}

Comments

Post a Comment

Please post comments here:-)

Popular posts from this blog

Android Objective type Question and Answers

Android Questions and Answers for written exams

SCJP1.6 Question and Answers