为什么来自服务器的数据不是以listview的形式出现的?

时间:2015-07-27 07:30:33

标签: android json listview

在这段代码中,当我放置一个文本字段并在其上显示代码时,它显示得很好。但是当我想以Listview的形式使用JSON解析显示相同的代码时,没有人能告诉我这个问题。 检查代码......

package com.apna.datainlist;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.entity.StrictContentLengthStrategy;
import org.json.JSONArray;
import org.json.JSONObject;
import org.xml.sax.helpers.DefaultHandler;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class Data extends Activity {
    ListView lv;
    CustomAdapter cd;
    ArrayList<String> lst=new ArrayList<String>();
    ArrayList<String> sublst=new ArrayList<String>();
    TextView tv;
    String Line;
    int z;
    String userRating[];
    String pass[];
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_data);
        lv=(ListView) findViewById(R.id.listView1);
        tv=(TextView) findViewById(R.id.textView1);
    new GetData().execute();
//  Collections.addAll(lst, "abc","bc");
//  Collections.addAll(sublst,"pass","v");

    cd=new CustomAdapter(Data.this, lst, sublst);
    lv.setAdapter(cd);
    }
    public class GetData extends AsyncTask<String, String, String>
    {       @Override
        protected String doInBackground(String... params) {
            // TODO Auto-generated method stub
    HttpClient httpClient=new DefaultHttpClient();
    HttpPost httpPost=new HttpPost("http://10.0.2.2/MyService/Local/abcd.php");
try{
    HttpResponse httpResponse = httpClient.execute(httpPost);
    HttpEntity httpEntity=httpResponse.getEntity();
    InputStream is=httpEntity.getContent();
    BufferedReader br=new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
    StringBuilder sb=new StringBuilder();
    Line= null;
    while((Line=br.readLine())!=null)
    {
        sb.append(Line);
    }
Line=sb.toString();
JSONArray obj=new JSONArray(Line);
z=obj.length();
String userRating=null;
String pass=null;
for(int i=0;i<obj.length();i++)
{
    JSONObject user=obj.getJSONObject(i);
    userRating=user.getString("username");
    pass=user.getString("pass");
lst.add(userRating);
sublst.add(pass);
}
}
catch(Exception e)
{
Toast.makeText(Data.this,"Error occurs",Toast.LENGTH_LONG).show();  
}
    return null;
}

        @Override
        protected void onPostExecute(String result) {
            // TODO Auto-generated method stub

            super.onPostExecute(result);
            Toast.makeText(Data.this,"Progress",Toast.LENGTH_LONG).show();  
            tv.setText(z+"ab");

        }   
    }
}

本地服务器上用于jSON解析的数据,即 在http://localhost/myservice/local/abcd.php

 [{"username":"aaa","pass":"sss"},{"username":"aaaa","pass":"sss"},{"username":"abcd 1","pass":"abc"},{"username":"Ankir","pass":"Arora"},{"username":"Ankit","pass":"abcd"},{"username":"Ankita","pass":"abc"},{"username":"avx","pass":"cs"},{"username":"krish","pass":"kuta"},{"username":"sad","pass":"sd"}]

1 个答案:

答案 0 :(得分:4)

您正在异步任务中从服务器获取数据,并在调用异步任务后立即将数据分配给适配器,这是错误的。

您的代码:

new GetData().execute();
//  Collections.addAll(lst, "abc","bc");
//  Collections.addAll(sublst,"pass","v");

    cd=new CustomAdapter(Data.this, lst, sublst);//you will not get data here...
    lv.setAdapter(cd);

确保在aysnc任务的post exeute中创建适配器对象。

@Override
        protected void onPostExecute(String result) {
            // TODO Auto-generated method stub

            super.onPostExecute(result);
cd=new CustomAdapter(Data.this, lst, sublst);//add this line
    lv.setAdapter(cd);//add this line
            Toast.makeText(Data.this,"Progress",Toast.LENGTH_LONG).show();  
            tv.setText(z+"ab");

        }