import
java.io.*;
import
java.util.*;
import
org.apache.http.*;
import
org.apache.http.client.*;
import
org.apache.http.client.methods.*;
import
org.apache.http.client.utils.*;
import
org.apache.http.impl.client.*;
import
org.apache.http.message.*;
import
org.xmlpull.v1.*;
import
android.app.Activity;
import
android.app.AlertDialog;
import
android.content.*;
import
android.os.Bundle;
import
android.util.*;
import
android.view.*;
import
android.widget.*;
import
android.widget.AdapterView.OnItemLongClickListener;
public
class
MainActivity
extends
Activity {
Button btn;
ListView lv;
ArrayList<EmpDTO> list;
CustomAdapter adapter;
public
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.main);
lv = (ListView)findViewById(R.id.listView1);
btn = (Button)findViewById(R.id.button1);
btn.setOnClickListener(
new
View.OnClickListener() {
public
void
onClick(View v) {
Intent intent =
new
Intent(getApplicationContext(), InsertActivity.
class
);
startActivity(intent);
}
});
}
public
InputStream requestGet(String requestURL) {
Log.i(
"xxx"
,
"requestGet start"
);
try
{
HttpClient client =
new
DefaultHttpClient();
List<NameValuePair> List =
new
ArrayList<NameValuePair>();
HttpGet get =
new
HttpGet(requestURL);
HttpResponse response = client.execute(get);
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
return
is ;
}
catch
(Exception e) {
e.printStackTrace();
return
null
;
}
}
public
String streamToString(InputStream is) {
StringBuffer buffer =
new
StringBuffer();
try
{
BufferedReader reader =
new
BufferedReader(
new
InputStreamReader(is,
"utf-8"
));
String str = reader.readLine();
while
(str !=
null
) {
buffer.append(str+
"\n"
);
str = reader.readLine();
}
reader.close();
}
catch
(Exception e) {
e.printStackTrace();
}
return
buffer.toString();
}
public
ArrayList<EmpDTO> getXML(InputStream is) {
ArrayList<EmpDTO> list =
new
ArrayList<EmpDTO>();
Log.i(
"xxx"
,
"getXML start!"
);
try
{
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
XmlPullParser parser = factory.newPullParser();
parser.setInput(is,
"utf-8"
);
int
eventType = parser.getEventType();
EmpDTO dto =
null
;
while
( eventType != XmlPullParser.END_DOCUMENT) {
switch
(eventType) {
case
XmlPullParser.START_TAG:
String startTag = parser.getName();
if
(startTag.equals(
"record"
)){ dto=
new
EmpDTO();}
if
(dto !=
null
){
if
(startTag.equals(
"loc"
)){ dto.setLoc(parser.nextText()); }
}
else
{ Log.i(
"xxx"
,
"dto = null"
); }
break
;
case
XmlPullParser.END_TAG:
String endTag = parser.getName();
if
(endTag.equals(
"record"
)){
list.add(dto);
}
}
eventType = parser.next();
}
for
( EmpDTO xx : list){
Log.i(
"xxx"
,
""
+xx.getLoc());
}
}
catch
(XmlPullParserException e) {
e.printStackTrace();
}
catch
(IOException ie) {
ie.printStackTrace();
}
return
list;
}
@Override
protected
void
onResume() {
super
.onResume();
InputStream is = requestGet(requestURL);
final
ArrayList<EmpDTO> list = getXML(is);
adapter =
new
CustomAdapter(getApplicationContext(),list,R.layout.custom_list);
lv.setAdapter(adapter);
lv.setOnItemLongClickListener(
new
OnItemLongClickListener() {
@Override
public
boolean
onItemLongClick(AdapterView<?> parent, View view,
final
int
position,
long
id) {
AlertDialog diaBox=
new
AlertDialog.Builder(MainActivity.
this
)
.setTitle(
"삭제"
)
.setMessage(
"해당 조를 삭제하시겠습니까?"
)
.setPositiveButton(
"YES"
,
new
DialogInterface.OnClickListener() {
public
void
onClick(DialogInterface dialog,
int
whichButton) {
list.remove(position);
lv.clearChoices();
adapter.notifyDataSetChanged();
}
})
.setNegativeButton(
"NO"
,
null
)
.create();
diaBox.show();
return
false
;
}
});
}}