package com.example.testnewsapp;
import java.io.InputStream;
import java.lang.reflect.Array;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Vector;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;
import android.R.string;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.database.DataSetObserver;
import android.os.Bundle;
import android.support.v4.app.TaskStackBuilder;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Spinner;
import android.widget.TextView;
public class MainActivity extends ListActivity implements OnItemClickListener{
/*TextView tv1;
TextView tv2;
*/
//title을 저장할 벡터
static Vector<String> newsTitleVector;
//기사 내용을 저장할 벡터
static Vector<String> newsDescriptionVector;
static Vector<String> newsAddress;
//사이트 주소 저장 변수
//실제 사이트에 접속하기 위한 객체 선언
URL newsURL;
String tagName = "" , title = "" , description ="" , address ="";
static int index =0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.subpage);
/*tv1 = (TextView)findViewById(R.id.titletxt);
tv2 = (TextView)findViewById(R.id.addresstxt1);*/
ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(this);
getData();
setListAdapter(new ArrayAdapter<String>(this, R.layout.listvalue, newsTitleVector));
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
// TODO Auto-generated method stub
index = position;
Intent intent = new Intent(MainActivity.this , NewsAppDetail.class);
startActivity(intent);
}
//title과 기사내용을 저장할 메소드 호출
public Vector getData()
{
newsTitleVector = new Vector<String>();
newsDescriptionVector = new Vector<String>();
newsAddress = new Vector<String>();
try{
//uri에 저장된 사이트에 접속
newsURL = new URL(uri);
//xml데이터를 읽어서 inputstream에 접속
InputStream in = newsURL.openStream();
//xmlpullparser를 사용하기 위해
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
//네임스페이스 사용여부
factory.setNamespaceAware(true);
XmlPullParser xpp = factory.newPullParser();
xpp.setInput(in , "UTF-8");
//이벤트 저장할 변수 선언
int eventType = xpp.getEventType();
boolean isItemTag = false;
//xml의 데이터의 끝까지 돌면서 원하는 데이터를 얻어옴
while(eventType != XmlPullParser.END_DOCUMENT)
{
if( eventType == XmlPullParser.START_TAG) //시작 태그를 만났을떄
{
//태그명 저장
tagName = xpp.getName();
if(tagName.equals("list"))
isItemTag = true;
}
else if(eventType == XmlPullParser.TEXT)//내용
{
//tagname에 저장된 태그명 title 태그일때 제목을 저장
if(isItemTag ==true && tagName.equals("dataTitle"))
{
title = xpp.getText();
Log.i("News App",title);
}
//기사의 내용을 저장
if(isItemTag ==true && tagName.equals("dataContent"))
description = xpp.getText();
Log.e("News App","e =>"+title);
if(isItemTag == true && tagName.equals("extensionValue"))
address = xpp.getText();
Log.e("News App","a =>"+address);
}
else if(eventType == XmlPullParser.END_TAG)//닫는 태그를 만났을때
{
tagName = xpp.getName();
if(tagName.equals("viewCount"))
{
newsDescriptionVector.add(description);
newsTitleVector.add(title);
newsAddress.add(address);
title ="";
description="";
address="";
isItemTag =false;
}
}
eventType = xpp.next();
}
}
catch(Exception e)
{
Log.e("NewsApp" , "예외발생했습니다 :"+e.getMessage());
}
//return newsTitleVector;
return null;
}
}
이렇게 하면 타이틀만 출력시키는 부분은 되는데요....
while문을 돌려서 얻은address부분도 같이 리스트에 뿌려주고싶은데 어떻게 해야할까요??