public class MainActivity extends Activity implements OnTouchListener {
private ViewFlipper m_viewFlipper;
private int m_nPreTouchPosX = 0;
private Button btn;
private static Parxing parxing;
private TextView text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (TextView)findViewById(R.id.sub_text);
m_viewFlipper = (ViewFlipper)findViewById(R.id.viewFlipper1);
m_viewFlipper.setOnTouchListener(this);
LinearLayout sub1 = (LinearLayout)View.inflate(this, R.layout.sub_view1, null);
LinearLayout sub2 = (LinearLayout)View.inflate(this, R.layout.sub_view2, null);
LinearLayout sub3 = (LinearLayout)View.inflate(this, R.layout.sub_view3, null);
m_viewFlipper.addView(sub1);
m_viewFlipper.addView(sub2);
m_viewFlipper.addView(sub3);
parxing = new Parxing(this);
parxing.execute();
btn = (Button)findViewById(R.id.btn1);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(v.getContext(), "sub_view1.btn1", Toast.LENGTH_SHORT).show();
}
});
}
private void MoveNextView(){
m_viewFlipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.appear_from_rigth));
m_viewFlipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.disappear_to_left));
m_viewFlipper.showNext();
}
private void MovewPreviousView(){
m_viewFlipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.appear_from_left));
m_viewFlipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.disappear_to_right));
m_viewFlipper.showPrevious();
}
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if(event.getAction()== MotionEvent.ACTION_DOWN){
m_nPreTouchPosX = (int)event.getX();
}
if(event.getAction()==MotionEvent.ACTION_UP){
int nTouchPosX = (int)event.getX();
if(nTouchPosX < m_nPreTouchPosX){
MoveNextView();
}
else if(nTouchPosX>m_nPreTouchPosX){
MovewPreviousView();
}
m_nPreTouchPosX = nTouchPosX;
}
return true;
}
public class Parxing extends AsyncTask<Void, Void, List<Content>>{
Context context;
public Parxing(Context context){
this.context = context;
}
@Override
protected List<Content> doInBackground(Void... params) {
// TODO Auto-generated method stub
List<Content> list = new ArrayList<Content>();
String order = "http://openAPI.seoul.go.kr:8088";
String userKey = "userkey";
String service = "SearchSTNBySubwayLineService";
String statin_num = "1";
try {
URL url = new URL(order+"/"+userKey+"/xml/"+service+"/1/100/"+statin_num);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.connect();
if (conn.getResponseCode() != -1) {
InputStream is = conn.getInputStream();
XmlPullParserFactory factory = XmlPullParserFactory
.newInstance();
XmlPullParser parser = factory.newPullParser();
parser.setInput(is, "UTF-8");
int eventType = parser.getEventType();
boolean check_station = false;
boolean check_stationNm = false;
boolean check_line_nm = false;
boolean check_fr_code = false;
Content content = null;
while(eventType != XmlPullParser.END_DOCUMENT){
if (eventType == XmlPullParser.START_TAG) {
String tagName = parser.getName();
if (tagName.equals("row")) {
content = new Content();
}
else if(tagName.equals("STATION_CD")){
check_station = true;
}
else if(tagName.equals("STATION_NM")){
check_stationNm = true;
}
else if(tagName.equals("LINE_NUM")){
check_line_nm = true;
}
else if(tagName.equals("FR_CODE")){
check_fr_code = true;
}
}
else if (eventType == XmlPullParser.TEXT) {
if(check_station){
content.setStation_cd("STATION_CODE: "+parser.getText());
check_station = false;
}else if(check_stationNm){
content.setStation_nm("STATION_NAME: "+parser.getText());
check_stationNm = false;
}else if(check_line_nm){
content.setLine_num("LINE_NUM: "+parser.getText());
check_line_nm = false;
}
else if(check_fr_code){
content.setFr_code("FR_CODE: "+ parser.getText());
check_fr_code = false;
}
}else if (eventType == XmlPullParser.END_TAG){
String tagName = parser.getName();
if(tagName.equals("row")){
list.add(content);
}
}
eventType = parser.next();
}
}
conn.disconnect();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
@Override
protected void onPostExecute(List<Content> result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
for(Content content : result){
text.append(content.getStation_cd()+"\n");
text.append(content.getLine_num()+"\n");
text.append(content.getStation_nm()+"\n");
text.append(content.getFr_code()+"\n");
text.append("------------------------\n");
}
}
}
}
위처럼 파씽을 하여 데이터를 다른 슬라이드에서 출력하려하는데 text의 값이 계속 null이 뜹니다.
sub_view1, sub_view2, sub_view3는 각각 xml이구요. main_activity는 따로 있습니다.
sub_view2가 다음과 같이 textView를 가지고 있습니다.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/sub_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="sub_view2" />
</LinearLayout>
왜 text값이 계속 null 이 나온는 것일까요?