xml android

운영자 | 기사입력 2017/09/21 [02:46]
안드로이드 >
필자의 다른기사 보기 인쇄하기 메일로 보내기 글자 크게 글자 작게
xml android
 
운영자   기사입력  2017/09/21 [02:46]

 

package com.example.su.myapplication;

import android.os.Build;
import android.os.StrictMode;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;

import java.io.InputStream;
import java.net.URL;
import android.widget.Toast;
import java.util.ArrayList;
import android.app.ListActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import static android.R.attr.tag;


public class MainActivity extends AppCompatActivity {


String xml_url="http://www.newsview.kr/app_newsview/app_main_list_xml.html";
// String xml_url="https://gdata.youtube.com/feeds/api/standardfeeds/top_rated";
ArrayList<String> items = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.d("zz","2");
if(Build.VERSION.SDK_INT>9){

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
};
Log.d("zz","3");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


try {

URL url= new URL(xml_url);
InputStream in=url.openStream();
XmlPullParserFactory factory= XmlPullParserFactory.newInstance();
XmlPullParser parser= factory.newPullParser();
parser.setInput(in,"UTF-8");
Log.d("zz","1");
int parserEvent = parser.getEventType();
int tagIdentifier = 0;
String tag;
String busStationCode = null, busStationName = null,busStationLati=null;

while(parserEvent != XmlPullParser.END_DOCUMENT){
switch(parserEvent){
case XmlPullParser.START_DOCUMENT:
break;
case XmlPullParser.END_DOCUMENT:
break;
case XmlPullParser.START_TAG:
tag = parser.getName();
// items.add(tag);
if(tag.equals("t")){
tagIdentifier = 1;
}else if(tag.equals("url")){
tagIdentifier = 2;
}else if(tag.equals("LATITUDE")){
tagIdentifier = 3;
}else if(tag.equals("LONGITUDE")){
tagIdentifier = 4;
}
break;
case XmlPullParser.END_TAG:
break;
case XmlPullParser.TEXT:
if(tagIdentifier == 1){
items.add(parser.getText().trim());
}else if(tagIdentifier == 2){
items.add(parser.getText().trim());
//busStationName = parser.getText().trim();
}else if(tagIdentifier == 3){
busStationLati = parser.getText().trim();
}else if(tagIdentifier == 4){
// busStationLongi = parser.getText().trim();
// BusStationDatas data = new BusStationDatas(busStationCode, busStationName, busStationLati, busStationLongi);
// items.add(data);
}
tagIdentifier = 0;
break;
}
parserEvent = parser.next();
}


}catch(Throwable t) {
Log.d("bb","error");


}
items.add("감자");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);
ListView list=(ListView)findViewById(R.id.list) ;
//list.setListAdapter(adapter);
list.setAdapter(adapter);


}

public void onListItemClick(ListView parent, View v, int position, long id) {
Toast.makeText(this, items.get(position).toString(), Toast.LENGTH_LONG ).show();
Log.d("cc",items.get(position).toString());
}
}
트위터 트위터 페이스북 페이스북 카카오톡 카카오톡
기사입력: 2017/09/21 [02:46]  최종편집: ⓒ iwav
 
 
안드로이드 많이 본 기사