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
}
});
adb.show();
}
});
}
}
tawisak 0 Newbie Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.