2014-02-14 50 views
1

我正在做一個android應用程序,它顯示listview中的患者列表。當我點擊列表中的一個項目時,另一個列表將顯示列表處方。我的問題是我將如何檢索該特定患者的所有處方列表。如何檢索ListView中的一個項目的數據,並打開另一個ListView

首先,點擊這個病人中的一個列表視圖

enter image description here

病人表結構

enter image description here

然後進入下一個列表視圖處方

這是我的處方表結構

enter image description here

,我想在列表視圖

enter image description here

這是關係視圖顯示此爲FKEY

enter image description here

這裏是我的處方清單代碼。

public class DocListPrescription extends ListActivity { 

    // Progress Dialog 
    private ProgressDialog pDialog; 
    TextView docusername; 
    Button btndelete; 

    String uid; 
    // Creating JSON Parser object 
    JSONParser2 jParser = new JSONParser2(); 

    ArrayList<HashMap<String, String>> patientsList; 

    // url to get all products list 
    // private static String url_all_patients = "http://192.168.43.6/android_connect/get_all_patients.php"; 
    private static String url_all_patients = "http://10.0.2.2/android_connect/get_all_prescriptions.php"; 

    // JSON Node names 
    private static final String TAG_SUCCESS = "success"; 
    private static final String TAG_PRESCRIPTION = "prescription"; 
    private static final String TAG_PRES_ID = "pres_id"; 
    private static final String TAG_UID = "uid"; 
    private static final String TAG_NAME = "pres_name"; 
    private static final String TAG_DATE = "date"; 
    private static final String TAG_DOCUSERNAME = "docusername"; 

    private ListView lv1; 
    // products JSONArray 
    JSONArray patients = null; 


    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.listpatient); 
     //docusername = (TextView) findViewById(R.id.docusername); 

      Intent i = getIntent(); 

     // getting product id (pid) from intent 
     uid = i.getStringExtra(TAG_UID); 


     // Hashmap for ListView 
     patientsList = new ArrayList<HashMap<String, String>>(); 

     // Loading products in Background Thread 
     new LoadAllProducts().execute(); 

     // Get listview 
     ListView lv = getListView(); 


     // on seleting single product 
     // launching Edit Product Screen 
     lv.setOnItemClickListener(new OnItemClickListener() { 

      @Override 
      public void onItemClick(AdapterView<?> parent, View view, 
        int position, long id) { 
       // getting values from selected ListItem 
       String pres_id = ((TextView) view.findViewById(R.id.pres_id)).getText() 
         .toString(); 


       // Starting new intent 
       Intent in = new Intent(getApplicationContext(), 
         AddPrescription.class); 

       // sending id to next activity 
       //in.putExtra(TAG_USERNAME,docusername.getText().toString()); 

       in.putExtra(TAG_PRES_ID, pres_id); 

       // starting new activity and expecting some response back 
       startActivityForResult(in, 100); 
       //startActivity(i); 

      } 
     }); 
     lv1 = (ListView) findViewById(android.R.id.list); 
     registerForContextMenu(lv1); 



    } 
    /* 
    @Override 
    public void onCreateContextMenu(ContextMenu menu, View v, 
     ContextMenuInfo menuInfo) { 
     if (v.getId()==R.id.list) { 
     AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo; 
     menu.setHeaderTitle(Countries[info.position]); 
     String[] menuItems = getResources().getStringArray(R.menu.menu); 
     for (int i = 0; i<menuItems.length; i++) { 
      menu.add(Menu.NONE, i, i, menuItems[i]); 
     } 
     } 
    } 
*/ 

    @Override 
    public void onBackPressed() { 
     super.onBackPressed(); 

     //userFunctions.logoutUser(getApplicationContext()); 
     Intent intent = new Intent(this, DocMenuActivity.class); 
     intent.putExtra(TAG_DOCUSERNAME,docusername.getText().toString()); 
     // Close all views before launching Dashboard 
     intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
     //startActivity(dashboard); 
     startActivity(intent); 
    } 
    // Response from Edit Product Activity 
    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 
     // if result code 100 
     if (resultCode == 100) { 
      // if result code 100 is received 
      // means user edited/deleted product 
      // reload this screen again 
      Intent intent = getIntent(); 
      finish(); 
      startActivity(intent); 
     } 

    } 

    /** 
    * Background Async Task to Load all product by making HTTP Request 
    * */ 
    class LoadAllProducts extends AsyncTask<String, String, String> { 

     /** 
     * Before starting background thread Show Progress Dialog 
     * */ 
     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      pDialog = new ProgressDialog(DocListPrescription.this); 
      pDialog.setMessage("Loading precriptions. Please wait..."); 
      pDialog.setIndeterminate(false); 
      pDialog.setCancelable(false); 
      pDialog.show(); 
     } 

     /** 
     * getting All products from url 
     * */ 
     protected String doInBackground(String... args) { 
      // Building Parameters 
      List<NameValuePair> params = new ArrayList<NameValuePair>(); 
      params.add(new BasicNameValuePair("uid", uid)); 

      // getting JSON string from URL 
      JSONObject json = jParser.makeHttpRequest(url_all_patients, "GET", params); 

      // Check your log cat for JSON reponse 
      Log.d("All Prescriptions: ", json.toString()); 

      try { 
       // Checking for SUCCESS TAG 
       int success = json.getInt(TAG_SUCCESS); 

       if (success == 1) { 
        // products found 
        // Getting Array of Products 
        patients = json.getJSONArray(TAG_PRESCRIPTION); 

        // looping through All Products 
        for (int i = 0; i < patients.length(); i++) { 
         JSONObject c = patients.getJSONObject(i); 

         // Storing each json item in variable 
         String id = c.getString(TAG_PRES_ID); 
         String details = c.getString(TAG_NAME); 

         // creating new HashMap 
         HashMap<String, String> map = new HashMap<String, String>(); 

         // adding each child node to HashMap key => value 
         map.put(TAG_PRES_ID, id); 
         map.put(TAG_NAME, details); 

         // adding HashList to ArrayList 
         patientsList.add(map); 
        } 
       } else { 
        // no products found 
        // Launch Add New product Activity 
        Intent i = new Intent(getApplicationContext(), 
          AddPrescription.class); 
        // Closing all previous activities 
        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
        startActivity(i); 
       } 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 

      return null; 
     } 

     /** 
     * After completing background task Dismiss the progress dialog 
     * **/ 
     protected void onPostExecute(String file_url) { 
      // dismiss the dialog after getting all products 
      pDialog.dismiss(); 
      // updating UI from Background Thread 
      runOnUiThread(new Runnable() { 
       public void run() { 
        /** 
        * Updating parsed JSON data into ListView 
        * */ 
        ListAdapter adapter = new SimpleAdapter(
          DocListPrescription.this, patientsList, 
          R.layout.rowprescrip, new String[] { TAG_PRES_ID, 
            TAG_NAME}, 
          new int[] { R.id.pres_id, R.id.pres_name }); 

     /* 


     */   
        // updating listview 
        setListAdapter(adapter); 
       } 
      }); 



     } 

    } 

} 

從這段代碼。我將ListofPatients的病人的uid傳遞給了這個活動,這樣我就可以得到這個特定項目(患者)的uid。

這是我的PHP檢索所有處方爲患者

<?php 

/* 
* Following code will list all the products 
*/ 

// array for JSON response 
$response = array(); 


// include db connect class 
require_once __DIR__ . '/db_connect.php'; 

// connecting to db 
$db = new DB_CONNECT(); 

// get all products from products table 
$result = mysql_query("SELECT *FROM prescription") or die(mysql_error()); 

// check for empty result 
if (mysql_num_rows($result) > 0) { 
    // looping through all results 
    // products node 
    $response["prescription"] = array(); 

    while ($row = mysql_fetch_array($result)) { 
     // temp user array 
     $prescription = array(); 
     $prescription["pres_id"] = $row["pres_id"]; 
     $prescription["pres_name"] = $row["pres_name"]; 
     // $prescription["created_at"] = $row["created_at"]; 
     $prescription["date"] = $row["date"]; 



     // push single product into final response array 
     array_push($response["prescription"], $prescription); 
    } 
    // success 
    $response["success"] = 1; 

    // echoing JSON response 
    echo json_encode($response); 
} else { 
    // no products found 
    $response["success"] = 0; 
    $response["message"] = "No products found"; 

    // echo no users JSON 
    echo json_encode($response); 
} 
?> 

從這個代碼。我不知道如何使用患者uid的foreign_key檢索該處方。請幫忙。我怎樣才能做到這一點?提前致謝。我現在真的需要幫助。

+0

如果你顯示錶結構以及映射表和期望的結果,那麼我們可以嘗試看看效果會更好 –

+0

@AkhilSidharth我已經更新了我的問題。請檢查並請盡你所能幫助我。我真的需要這個應用程序。 – user3226149

+0

您是否可以將患者的餐桌結構與您期望的結果一起使用?告訴,你希望結果如何? –

回答

0

因爲您的要求只是從處方表中獲取特定患者的數據,請使用下面的代碼。

select * from prescription where patient_user_fkey= mysql_real_escape_string($patient_id); 

如果您想要患者的詳細信息,可以使用JOIN獲取數據。我仍然不確定這是您的要求嗎?如果不是,至少讓我們知道應該是你想要的輸出是

相關問題