0
ExpandableListView 만들기 : JSON 정보를 기반으로 expandablelistview을 만들 로드 JSON 데이터와 다음 작업을 수행 할 필요가
나는 것을 알고있는 URL
- 부하 JSON 데이터를 http 요청은 비동기 적이어야하므로 AsyncTask를 사용하여 작업을 수행하지만 데이터를 얻은 후에는 확장형을 만들 수 없습니다.
json 데이터를 올바르게 가져오고 있지만, 현재 정적 정보를 사용하여 확장 가능 빌드를하고 있습니다.
어제 안드로이드를 코딩하기 시작했고, 내가하고있는 일에 대해 확실하게 알고 싶습니다.
미리 감사드립니다.
MainActivity.java :
package com.hp.package1; import android.app.Activity; import android.os.Bundle; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); new getJson().execute(); } }
getJson.java :
package com.hp.package1; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient; import org.json.JSONArray; import org.json.JSONObject; import android.os.AsyncTask; public class getJson extends AsyncTask<String, Float, Integer> { public Integer doInBackground(String[] url) { JSONArray names; JSONArray values; String uri = "http://www.domain.com/json.php"; try { HttpClient httpclient = new DefaultHttpClient(); HttpGet httpget = new HttpGet(uri); HttpResponse response = httpclient.execute(httpget); HttpEntity entity = response.getEntity(); InputStream instream = entity.getContent(); String result = convertStreamToString(instream); JSONObject json = new JSONObject(result); names = json.names(); values = json.toJSONArray(names); for (int m = 0; m < values.length(); m++) System.out.println(names.getString(m) + " - " + values.getString(m)); return 1; } catch (Exception e) { System.out.println("error: " + e.toString()); return 0; } } private String convertStreamToString(InputStream is) { BufferedReader reader = new BufferedReader(new InputStreamReader(is)); StringBuilder sb = new StringBuilder(); String line = null; try { while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } } catch (IOException e) { } return sb.toString(); } protected void onPostExecute(Integer bytes) { System.out.println("now comes the creation of expandable"); try { new expandable(); // here is where I try to create the expandable } catch (Exception e) { System.out.println("error while creating the expandable"); } } }
expandable.java : 장소에서
package com.hp.package1; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import android.app.ExpandableListActivity; import android.view.Menu; import android.view.View; import android.widget.ExpandableListView; import android.widget.SimpleExpandableListAdapter; public class expandable extends ExpandableListActivity { @SuppressWarnings("unchecked") public expandable() { System.out.println("constructor"); SimpleExpandableListAdapter expListAdapter = new SimpleExpandableListAdapter( this, createGroupList(), R.layout.group_row, new String[] { "Group Item" }, new int[] { R.id.row_name }, createChildList(), R.layout.child_row, new String[] {"Sub Item"}, new int[] { R.id.grp_child} ); setListAdapter(expListAdapter); } @SuppressWarnings("unchecked") private List createGroupList() { ArrayList result = new ArrayList(); for (int i = 0; i < 15; ++i) { HashMap m = new HashMap(); m.put("Group Item","Group Item " + i); result.add(m); } return (List)result; } @SuppressWarnings("unchecked") private List createChildList() { ArrayList result = new ArrayList(); for (int i = 0; i < 15; ++i) { ArrayList secList = new ArrayList(); for (int n = 0; n < 3; n++) { HashMap child = new HashMap(); child.put("Sub Item", "Sub Item " + n); secList.add(child); } result.add(secList); } return result; } public void onContentChanged() { System.out.println("onContentChanged"); super.onContentChanged(); } public boolean onChildClick(ExpandableListView parent, View v, int groupPosition,int childPosition,long id) { return true; } public void onGroupExpand (int groupPosition) { try { } catch(Exception e) { } } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } }
그래서 두 개의 다른 자습서를 복사/붙여 넣기하고 앱을 만들길 원하십니까? 특정 질문이 있으면 질문 할 수는 있지만 너무 애매합니다. –