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

}

我想在列表视图中显示数据,我该怎么做?

4

1 回答 1

1

使用适配器 - http://developer.android.com/reference/android/widget/Adapter.html

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

于 2012-12-16T14:04:58.537 回答