0

こんにちは、データをリスト ビューで表示したいだけです。非同期タスクを使用して、json でデータを取得し、ID とタイトルでフィルタリングします。リストビューで ID とタイトルを表示します。よろしくお願いします。

 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);
    }
 }
 }
4

3 に答える 3

1

ListAdapter で使用する配列を作成する必要があります。

Google からのガイドは次のとおりです: http://developer.android.com/resources/tutorials/views/hello-listview.html

于 2012-05-15T09:00:16.747 に答える
0

最善の解決策は、アクティビティで Handler を作成することだと思います。その後、ハンドラーにメッセージを送信し、データを取得して ListView に入れることができます。

于 2012-05-15T08:59:57.700 に答える
0

doInBackground の "for" ループでは、データの配列を作成するか、オブジェクトの配列リストにデータを配置します (カスタム アダプターを作成する必要があります)。

1- オプション http://www.java-samples.com/showtutorial.php?tutorialid=1516 http://www.ezzylearning.com/tutorial.aspx?tid=1659127&q=binding-android-listview-with-stringの場合-array-using-arrayadapter

2オプションの場合

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

于 2012-05-15T09:07:12.120 に答える