2012-05-15 3 views
0

안녕하세요 친구 목록보기에서 데이터보기를 원합니다. 비동기 작업을 사용하고 json에서 데이터를 가져 와서 ID와 제목으로 필터링하면 이제 ID와 제목이 표시됩니다. 목록보기 미리 감사드립니다.AsyncTask에서 목록보기의 데이터를 원합니다

public class runActivity extends Activity implements OnClickListener { 
    String returnString=""; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    findViewById(R.id.my_button).setOnClickListener(this); 
} 

@Override 
public void onClick(View arg0) { 
    Button b = (Button)findViewById(R.id.my_button); 
    b.setClickable(false); 
    new LongRunningGetIO().execute(); 
} 

private class LongRunningGetIO extends AsyncTask <Void, Void, String> { 

    protected String getASCIIContentFromEntity(HttpEntity entity) throws IllegalStateException, IOException { 
     InputStream in = entity.getContent(); 
     StringBuffer out = new StringBuffer(); 
     int n = 1; 
     while (n>0) { 
      byte[] b = new byte[4096]; 
      n = in.read(b); 
      if (n>0) out.append(new String(b, 0, n)); 
     } 
     return out.toString(); 
    } 

    @Override 
    protected String doInBackground(Void... params) { 
     HttpClient httpClient = new DefaultHttpClient(); 
     HttpContext localContext = new BasicHttpContext(); 
     HttpGet httpGet = new HttpGet("http://192.168.1.156/recess/document/document.json"); 
     HttpClient client = new DefaultHttpClient(); 
     HttpResponse response=null; 
     try{ 
      response = client.execute(httpGet);} 
     catch(Exception e){} 
     System.out.println(response.getStatusLine()); 
     String text = null; 
     try { 
       response = httpClient.execute(httpGet, localContext); 
       HttpEntity entity = response.getEntity(); 
       text = getASCIIContentFromEntity(entity); 
     } catch (Exception e) { 
      return e.getLocalizedMessage(); 
     } 
     String var =text;    
     try{ 
      JSONObject jObj = new JSONObject(var); 
      JSONArray jArray = jObj.getJSONArray("document"); 
      for(int i=0;i<jArray.length();i++){ 

        JSONObject json_data = jArray.getJSONObject(i); 
        Log.i("log_tag","id: "+json_data.getString("id")+ 
          ", title: "+json_data.getString("title") 
        ); 
        returnString += "\n" +"id:"+ json_data.getString("id")+" "+"Title:"+    json_data.getString("title"); 

        } 


    } 
    catch(JSONException e){ 
      Log.e("log_tag", "Error parsing data "+e.toString()); 
    } 
     return returnString; 
    } 

    protected void onPostExecute(String results) { 
     if (results!=null) { 
      ListView listView = (ListView) findViewById(R.id.mylist); 
      listView.setFilterText(results); 
     } 
     Button b = (Button)findViewById(R.id.my_button); 
     b.setClickable(true); 
    } 
} 
} 

답변

0

최선의 해결책은 활동에 처리기를 만드는 것입니다. 그런 다음 처리기에 메시지를 보내고 데이터를 가져 와서 ListView에 넣을 수 있습니다.

0

"for"루프 : 여기

구글에서 가이드 1- 옵션,174 들어 http://www.java-samples.com/showtutorial.php?tutorialid=1516 http://www.ezzylearning.com/tutorial.aspx?tid=1659127 & Q = 바인딩 안드로이드리스트 뷰 - 스트링과 어레이 - 이용-ArrayAdapter와

위한

2 옵션

http://www.ezzylearning.com/tutorial.aspx?tid=1763429&q=customizing-android-listview-items-with-custom-arrayadapter

+0

에 대한 dthanks 노나 노력할 것입니다하지만 난 목록보기에 – Mohit

+0

setListAdapter (새 ArrayAdapter와 ( 이, 안드로이드를 표시 할 수 없습니다 .R.layout.simple_expandable_list_item_1, 개 항목)); 여기서 item은 문자열의 배열입니다. –

+0

setListAdapter (새 ArrayAdapter (this, android.R.layout.showalbooks, list1))); 그것 showalbooks에 오류가 XML 파일 이름과 오류 showalbooks는 해결할 수 없거나 필드가 아닙니다 – Mohit

관련 문제