package kr.co.moak.jeonjubus99;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
public class StopFragment extends Fragment {
ArrayList<HashMap<String, String>> m_arList;
StopAdapter m_adapter;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.frag_stop, container, false);
String apiKey = MyConst.API_KEY;
String searchFld = "stopnm";
String searchNm = "전주대";
// 승강장명 목록 조회
String url = "http://openapi.jeonju.go.kr/jeonjubus/openApi/traffic/bus_location2_stopnm_common.do"
+ "?ServiceKey=" + apiKey + "&searchFld=stopnm&searchNm=" + searchNm;
new MyAsyncTask(getContext()).execute(url);
ListView lstView = (ListView)view.findViewById(R.id.lstView);
m_arList = new ArrayList<>();
m_adapter = new StopAdapter(getContext(), m_arList);
lstView.setAdapter(m_adapter);
lstView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(getContext(), m_arList.get(i).get("stopkname"), Toast.LENGTH_SHORT).show();
}
});
return view;
}
class MyAsyncTask extends AsyncTask<String, Integer, Document> {
private Context m_context;
public MyAsyncTask(Context context){
this.m_context = context;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Document doInBackground(String... urls) {
URL url;
Document doc = null;
try{
url = new URL(urls[0]);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
doc = db.parse(new InputSource(url.openStream()));
doc.getDocumentElement().normalize();
}catch (Exception e){
Log.v("MYTAG", "Parsing Error");
}
Log.v("MYTAG", urls[0]);
return doc;
}
@Override
protected void onPostExecute(Document doc) {
m_arList.clear();
NodeList nodeList = doc.getElementsByTagName("list");
Log.v("MYTAG", "cnt: " + nodeList.getLength());
for(int i = 0; i<nodeList.getLength(); i++) {
HashMap<String, String> hash = new HashMap<>();
NodeList childList = nodeList.item(i).getChildNodes();
for(int j=0; j<childList.getLength(); j++){
hash.put(childList.item(j).getNodeName().toLowerCase(), childList.item(j).getTextContent());
Log.v("MYTAG", childList.item(j).getNodeName().toLowerCase() + " "+ childList.item(j).getTextContent());
}
m_arList.add(hash);
}
m_adapter.notifyDataSetChanged();
}
}
}