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

在 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 回答
0

我认为最好的解决方案是在您的活动中创建一个处理程序。然后,您可以向处理程序发送消息并获取数据并将其放入 ListView。

于 2012-05-15T08:59:57.700 回答