如何将jsonObject表示为android中的列表项?

时间:2012-06-16 13:03:44

标签: android

**我想要一个单独的列表项在返回的对象中包含每个患者的名字,姓氏和patient_id。 下面是我返回的jsonObject **。

{"tag":"available_patient","success":1,"error":0,"patients":
[{"p_id":"1","fname":"Peter","lname":"Lule"},
{"p_id":"2","fname":"Edgar","lname":"Ainebyoona"},
{"p_id":"5","fname":"Andrew","lname":"mugasa"}]}

我也在使用两个xml文件, 一个包含listView,另一个包含firstname,last name和patient_id,如下所示。

main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="80dp" 
         android:background="@drawable/viewp_bg">

    <Button
        android:id="@+id/bviewPatients"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="View Patients"
        android:layout_marginLeft="200dp"
        android:layout_marginTop="25dp" />

</LinearLayout>    


<ListView android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/mainListView">
</ListView>

</LinearLayout>

patient_list_items

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="8px">

    <TextView
        android:id="@+id/firstName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <TextView
        android:id="@+id/lastName"
        android:layout_marginLeft="6px"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/firstName"/>

    <TextView
        android:id="@+id/id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/firstName"/>

</RelativeLayout>

ViewPatients.java

package mut.doc.pat;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import library.UserFunctions;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

public class ViewPatients2 extends Activity implements OnClickListener {

    ListView mainListView;
    Button viewPatients;

    Bundle gotbasket;
    ArrayList<String> gotjsonResult;

    JSONArray jsonArray = null;

    SimpleAdapter adapter;

    private static String KEY_SUCCESS = "success";
    private static final String TAG_ID = "p_id";
    private static final String TAG_FNAME = "fname";
    private static final String TAG_LNAME = "lname";

    String patient_id, firstname, lastname, doctor_id;
    String[] from;
    int[] to;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mainListView = (ListView) findViewById(R.id.mainListView);
        viewPatients = (Button) findViewById(R.id.bviewPatients);
        viewPatients.setOnClickListener(this);

    }

    public void getPatients(String doctor_id){

        UserFunctions userFunction = new UserFunctions();
        JSONObject json = userFunction.getPatientsByDoctorId(doctor_id);

        try {
            if(json.getString(KEY_SUCCESS) != null){

                String res = json.getString(KEY_SUCCESS);
                if(Integer.parseInt(res) == 1){

                    //patients successfully 
                    //JSONObject json_patients = json.getJSONObject("patients");


                    //getting an array of patients
                    jsonArray = json.getJSONArray("patients");


                    String[] data = new String[jsonArray.length()];

                     // prepare the list of all records
                    ArrayList<HashMap<String, String>> fillMaps = new ArrayList<HashMap<String, String>>();

                    //looping thru all the patients
                    for(int i = 0; i<jsonArray.length(); i++){
                        HashMap<String, String> map = new HashMap<String, String>();

                        JSONObject patient = jsonArray.getJSONObject(i);

                        map.put("elements ", jsonArray.getString(i));

                        fillMaps.add(map);

                    //storing each item into a variable
                        patient_id = patient.getString(TAG_ID);
                        firstname = patient.getString(TAG_FNAME);
                        lastname = patient.getString(TAG_LNAME);

                        from = new String[] {"firstname", "lastname", "patient_id"};
                        to = new int[] { R.id.firstName, R.id.lastName, R.id.id};

                        String[][][] array = new String[jsonArray.length()] [jsonArray.length()][jsonArray.length()];

                        Log.i("Patients", "First Name: " + firstname + " " + " " + lastname + " " + patient_id );

                    }


                    adapter = new SimpleAdapter(ViewPatients2.this, fillMaps, R.layout.patients_list_item, from, to );
                    mainListView.setAdapter(adapter);
                }
            }
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();

            Toast.makeText(getBaseContext(), "Error occured while fetching Patients", Toast.LENGTH_LONG).show();

        }
    }

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub

        gotbasket = getIntent().getExtras();
        doctor_id = gotbasket.getString("doctor_id");

        //call to getPatients()
        getPatients(doctor_id);

    }


}

1 个答案:

答案 0 :(得分:0)

我认为最好使用自定义适配器........让你自己的适配器

它会制作

1- you code less complex.
2- easy to understand.
3- more flexible.
4- more optimized.

失去了这个

的例子

http://www.josecgomez.com/2010/05/03/android-putting-custom-objects-in-listview/

第一次需要努力去理解,但是你将来必须使用自定义适配器。