Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 将数据从json加载到列表视图_Android_Json_Listview - Fatal编程技术网

Android 将数据从json加载到列表视图

Android 将数据从json加载到列表视图,android,json,listview,Android,Json,Listview,我有一个将数据从json加载到android的应用程序,如下所示 public class MainActivity extends Activity { private final String URL_SERVICE = "http://92.253.101.239:81/sudandoctors/api.aspx?op=5&GovernorateId=&SpecializationId=&DoctorName=&LastDoctorId=0";

我有一个将数据从json加载到android的应用程序,如下所示

public class MainActivity extends Activity {
    private final String URL_SERVICE = "http://92.253.101.239:81/sudandoctors/api.aspx?op=5&GovernorateId=&SpecializationId=&DoctorName=&LastDoctorId=0";
    private final String URL_IMAGE_BASE = "http://92.253.101.239:81/sudandoctors/UploadedFiles/"; 
    TextView tv;
    DoctorInfo doctorObj = new DoctorInfo(); 
    ArrayList<DoctorInfo> doctorsInfoList = new ArrayList<DoctorInfo>();

    ListView lv = (ListView)findViewById(R.id.mylist);
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        AsyncHttpClient client = new AsyncHttpClient();

        client.get(URL_SERVICE, new AsyncHttpResponseHandler() {
            @Override
            public void onStart() {
                super.onStart();
                // show loading bar
                Log.d("onStart", "onStart");
            }

            @Override
            public void onSuccess(String response) {
                super.onSuccess(response);
                Log.d("onSuccess", "onSuccess");
                DoctorsModel doctorModel = new DoctorsModel();
                ArrayList<DoctorInfo> doctorsInfoList = new ArrayList<DoctorInfo>();

                try {
                    // convert response to JSON
                    JSONObject json = new JSONObject(response);

                    // get JSON Items
                    doctorModel.setDoctorCnt(json.getInt("DoctorsCount"));
                    doctorModel.setOp(json.getString("op"));

                    JSONArray doctorsArray = json.getJSONArray("Doctors");
                    Log.d("dr arrat", doctorsArray.toString());
                    for (int i = 0; i < doctorsArray.length(); i++) {
                        JSONObject doctorJSON = doctorsArray.getJSONObject(i);


                        doctorObj .setId(doctorJSON.getString("Id"));
                        Log.d("id", doctorJSON.getString("Id"));
                        doctorObj.setName(doctorJSON.getString("DoctorName"));
                        doctorObj.setGovernorateId(doctorJSON.getString("GovernorateId"));
                        doctorObj.setGovernorateName(doctorJSON.getString("GovernorateName"));
                        doctorObj.setSpecializationName(doctorJSON.getString("SpecializationName"));
                        doctorObj.setHospitalId(doctorJSON.getString("HospitalId"));
                        doctorObj.setHospitalName(doctorJSON.getString("HospitalName"));
                        doctorObj.setImageName(URL_IMAGE_BASE + doctorJSON.getString("ImageName"));
                        doctorObj.setMobile(doctorJSON.getString("Mobile"));
                        doctorObj.setSpecializationId(doctorJSON.getString("SpecializationId"));
                        doctorObj.setWeekendDays(doctorJSON.getString("WeekendDays"));
                        doctorObj.setWorkingDays(doctorJSON.getString("WorkingDays"));
                        doctorObj.setWorkingTime(doctorJSON.getString("WorkingTime"));

                        doctorsInfoList.add(doctorObj);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Throwable e, String message) {
                super.onFailure(e, message);
                // show error message
            }

            @Override
            public void onFinish() {
                super.onFinish();
                // remove loading bar
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                  android.R.layout.activity_list_item);

                // Assign adapter to ListView
                lv.setAdapter(adapter); 
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

}
公共类MainActivity扩展活动{
私有最终字符串URL_服务=”http://92.253.101.239:81/sudandoctors/api.aspx?op=5&GovernorateId=&SpecializationId=&DoctorName=&LastDoctorId=0";
私有最终字符串URL\u IMAGE\u BASE=”http://92.253.101.239:81/sudandoctors/UploadedFiles/"; 
文本视图电视;
DoctorInfo doctorObj=新的DoctorInfo();
ArrayList doctorsInfoList=新建ArrayList();
ListView lv=(ListView)findViewById(R.id.mylist);
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AsyncHttpClient=新的AsyncHttpClient();
get(URL_服务,新的AsyncHttpResponseHandler(){
@凌驾
public void onStart(){
super.onStart();
//显示加载条
Log.d(“启动”、“启动”);
}
@凌驾
成功时公共无效(字符串响应){
超级成功(响应);
Log.d(“onSuccess”、“onSuccess”);
DoctorsModel doctorModel=新DoctorsModel();
ArrayList doctorsInfoList=新建ArrayList();
试一试{
//将响应转换为JSON
JSONObject json=新的JSONObject(响应);
//获取JSON项
setDoctorCnt(json.getInt(“doctorscont”);
setOp(json.getString(“op”));
JSONArray doctorsArray=json.getJSONArray(“医生”);
Log.d(“dr arrat”,doctorsArray.toString());
对于(int i=0;i
我想在列表视图中显示数据,我该怎么做

使用适配器-


使用您的数据创建一个适配器,然后将该适配器设置为ListView的适配器。

先自己动手,例如,在谷歌上搜索教程,如:Reference。并让我知道您了解的内容?