2016-05-25 34 views
-1

我的程序旨在搜索數據庫中輸入的特定名稱,然後程序將只顯示特定名稱的行。但是,我的代碼無法顯示特定的數據。我嘗試過很多種方法,但無法解決我的問題。我的PHP代碼如下:如何在Android Studio中僅顯示數據庫中選定的數據

<?php 
    $con = mysql_connect('mysql17.000webhost.com', 'a2634311_minzhe', 'MZRules0118'); 

    if (!$con) 
     { 
     die('Could not connect: ' . mysql_error()); 
     } 

    mysql_select_db("a2634311_gdp", $con); 
    $result = mysql_query("SELECT * FROM groupdesign"); 
    while($row = mysql_fetch_assoc($result)) 
     { 
     $output[]=$row; 
     } 
    print(json_encode($output)); 

    mysql_close($con); 
    ?> 

我的活動代碼如下:

import android.app.Activity; 
    import android.content.Context; 
    import android.content.Intent; 
    import android.content.SharedPreferences; 
    import android.os.AsyncTask; 
    import android.os.StrictMode; 
    import android.support.v7.app.ActionBarActivity; 
    import android.os.Bundle; 
    import android.util.Log; 
    import android.view.LayoutInflater; 
    import android.view.View; 
    import android.view.ViewGroup; 
    import android.widget.BaseAdapter; 
    import android.widget.Button; 
    import android.widget.ListView; 
    import android.widget.TextView; 
    import android.widget.Toast; 

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

    import java.io.InputStream; 


    public class PatientLocation extends ActionBarActivity { 

     private ListView PatientLocationListView; 

     @Override 
     protected void onCreate(Bundle savedInstanceState) { 


      StrictMode.ThreadPolicy policy = new   StrictMode.ThreadPolicy.Builder().permitAll().build(); 
      StrictMode.setThreadPolicy(policy); 

      this.PatientLocationListView = (ListView)   this.findViewById(R.id.listView4); 

      new Location().execute(new API_Connector()); 
     } 

     public void setListAdapter(JSONArray jsonArray) { 

      SharedPreferences settings = getSharedPreferences("myprefrences", Context.MODE_PRIVATE); 
      String name = settings.getString("name", ""); 
      this.PatientLocationListView.setAdapter(new   PatientAdapter(jsonArray,name, this)); 
     } 

     private class Location extends AsyncTask<API_Connector, Long, JSONArray> { 
      @Override 
      protected JSONArray doInBackground(API_Connector... params) { 

       //it is executed on Background thread 

       return params[0].GetPatientLocation(); 
      } 

      @Override 
      protected void onPostExecute(JSONArray jsonArray) { 
       // check for null 
       if (jsonArray != null) { 
        // only pass the array to adapter if it is not null 
        setListAdapter(jsonArray); 
       } else { 
        Toast.makeText(PatientLocation.this, "Null Array returned", Toast.LENGTH_SHORT).show(); 
       } 


      } 
     } 
    } 

    class PatientAdapter extends BaseAdapter { 

     private JSONArray dataArray; 
     private Activity activity; 
     String pname; 

     private static LayoutInflater inflater = null; 

     public PatientAdapter(JSONArray jsonArray, String name, Activity a) { 
      this.dataArray = jsonArray; 
      this.activity = a; 
      this.pname = name; 

      inflater = (LayoutInflater)   this.activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     } 

     @Override 
     public int getCount() { 
      return this.dataArray.length(); 
     } 

     @Override 
     public Object getItem(int position) { 
      return position; 
     } 

     @Override 
     public long getItemId(int position) { 
      return position; 
     } 

     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 

      String patientname,patientlocation; 
      //set up convert view if it is null 
      ListCell cell = new ListCell(); 
      if (convertView == null) { 
       convertView = inflater.inflate(R.layout.activity_position, null); 

       cell.patient_name = (TextView)   convertView.findViewById(R.id.textView30); 
       cell.patient_location = (TextView) convertView.findViewById(R.id.textView31); 
       convertView.setTag(cell); 
      } 
      else { 
       cell = (ListCell) convertView.getTag(); 
      } 
      //change the data of the cell 

      try { 
       for(int i=0;i<dataArray.length();i++) { 
        JSONObject jsonObject = this.dataArray.getJSONObject(i); 
        if(pname == jsonObject.getString("Name")) { 
         patientname = jsonObject.getString("Name"); 
         patientlocation = jsonObject.getString("Location"); 

         cell.patient_name.setText(patientname); 
         cell.patient_location.setText(patientlocation); 
        } 

       } 
      } 
      catch (JSONException e) { 
       e.printStackTrace(); 
      } 

      return convertView; 
     } 

     private class ListCell { 
      private TextView patient_name; 
      private TextView patient_location; 
     } 

    } 
+0

當使用'AsyncTask'時,不需要使用'StrictMode.ThreadPolicy'方法,並且您從服務器獲取數據? –

+0

好的,謝謝你的信息。是的,我從服務器獲取數據 – Elzy

+0

看來,在View類中,當我刪除if情況時,它將顯示來自服務器的所有數據。但是,如果是這種情況,則無法顯示任何內容。 – Elzy

回答

0

其中,是在PHP代碼查詢條款?你需要設置一些條件來匹配查詢中的特定名稱,就像你想搜索'abcd'那麼你的查詢應該是SELECT * FROM groupdesign WHERE column_name = "abcd"。這將返回從您的數據庫中附帶值「abcd」的記錄

+0

感謝您的幫助。我會嘗試通過更改我的android代碼來解決它 – Elzy

相關問題