2012-06-16 15 views
0

**我希望一個單獨的列表項目具有fistname,姓氏和返回對象中每個患者的patient_id。 下面是我返回的jsonObject **。如何在android中將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和其他含姓名,姓氏和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); 

    } 


} 

回答

0

我想b埃特使用自定義適配器........讓你自己的適配器

它會補償

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/

首播時間losts這將需要努力去理解,但將來您必須使用自定義適配器。

+1

感謝Dheeresh的迴應,我會嘗試鏈接。 –

+0

有一個很好的帖子,我發現在這個鏈接「http://p-xr.com/android-tutorial-how-to-parse-read-json-data-into-a-android-listview/」 –

相關問題