package demo.weatherforecast.main;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import org.xmlpull.v1.XmlSerializer;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.util.Xml;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;



public class EditCity extends Activity {
String xmlFilepath = "/sdcard/searchcity.xml";
ArrayList<String> allListCity = new ArrayList<String>();
SimpleAdapter tempListAdapter;
ArrayList<HashMap<String, String>> weatherlist = new 
ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();
ListView listViewInfo;
 public void tempUpdate(){
XmlHelper xmlHp = new XmlHelper();
if(xmlHp.readAllCity().isEmpty()){
 }
else{
ArrayList<String> tmpweatherArr = new ArrayList<String>();
WeatherHelper whp = new WeatherHelper();
tmpweatherArr = xmlHp.readAllCity();
Iterator<String> itr = tmpweatherArr.iterator();
String[] tmpResult = new String[3];
while(itr.hasNext()){
tmpResult = whp.getCurrentTemp(itr.next().toString());
if(tmpResult[0].equalsIgnoreCase("error")){
Toast.makeText(this, "Cannot retieve weather infomation from WebService", 
Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(this, "City "+ tmpResult[0] +" Lowest:"+ tmpResult[1] +" 
Highest:"+ tmpResult[2], Toast.LENGTH_LONG).show();
map = new HashMap<String, String>();
map.put("city", tmpResult[0]);
map.put("low", tmpResult[1]);
map.put("high", tmpResult[2]);
weatherlist.add(map);
//weatherArr.add(tmpResult[0]);
listViewInfo.setAdapter(tempListAdapter); 
}
}
}
}
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
        setContentView(R.layout.edit);
        listViewInfo = (ListView)findViewById(R.id.listView1);
        tempListAdapter = new SimpleAdapter(this, weatherlist, 
R.layout.list_row,
            new String[] {"city", "low", "high"}, new int[] {R.id.CITY_CELL, 
R.id.LOW_CELL, R.id.HIGH_CELL});
listViewInfo.setAdapter(tempListAdapter);
tempUpdate(); 
listViewInfo.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> a, View v,
 final int position, long id) {
// TODO Auto-generated method stub
AlertDialog.Builder adb = new AlertDialog.Builder(
EditCity.this);
adb.setTitle("Delete?");
adb.setMessage("Are you sure you want to delete ");
// final int positionToRemove = position;
adb.setNegativeButton("Cancel", null);
adb.setPositiveButton("Ok",
new AlertDialog.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
// TODO Auto-generated method stub
try
{ 
weatherlist.remove(position); 
listViewInfo.setAdapter(tempListAdapter); 
System.out.println(position); 
WriteIntoXML(weatherlist);
}catch(Exception e)
{
// 
}
}

private void WriteIntoXML(ArrayList<HashMap<String, String>> weatherlist) {
// TODO Auto-generated method stub
System.out.println(weatherlist);
 File newxmlfile = new File(xmlFilepath);
 try{
         newxmlfile.createNewFile();
        }catch(IOException e){
         Log.e("IOException", "exception in createNewFile() method");
        }
        FileOutputStream fileos = null;       
        try{
         fileos = new FileOutputStream(newxmlfile);
        }catch(FileNotFoundException e){
         Log.e("FileNotFoundException", "can't create FileOutputStream");
        }
        try{
         fileos = new FileOutputStream(newxmlfile);
        }catch(FileNotFoundException e){
         Log.e("FileNotFoundException", "can't create FileOutputStream");
        }
        XmlSerializer serializer = Xml.newSerializer();
//        allListCity.contains(weatherlist);
       try
       {
       Iterator<String> itr = allListCity.iterator();
       serializer.setOutput(fileos, "UTF-8");
serializer.startDocument(null, Boolean.valueOf(true));
serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output";, 
true); 
serializer.startTag(null, "root"); 
while(itr.hasNext()){
serializer.startTag(null, "city");
serializer.startTag(null, "cityname");
serializer.text(itr.next().toString());
serializer.endTag(null, "cityname");
serializer.startTag(null, "low");
serializer.text("0");
serializer.endTag(null, "low");
serializer.startTag(null, "high");
//serializer.attribute(null, "user", user_arr[i]);
serializer.text("0");
serializer.endTag(null, "high");
serializer.endTag(null, "city");
} 
serializer.endTag(null, "root");
serializer.endDocument();
serializer.flush();
fileos.close();
       }catch(Exception e){
       Log.e("Exception","error occurred while creating xml file"); 
       }
}
});
adb.show();
}
});
    }
}

Problem  --->    When i delete item in listview , How can i remove that tag 
in XmlSerializer in SD card ?

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to