2016-05-25 7 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コードでクエリのWHERE句はありますか?あなたは 'abcd'を検索したいと思っていて、クエリがSELECT * FROM groupdesign WHERE column_name = "abcd"でなければならないかのように、クエリの特定の名前と一致する条件を設定する必要があります。これは、データベースから "abcd"という値を持つレコードを返します。

+0

ご協力いただきありがとうございます。私はアンドロイドコードを変更して解決しようとします – Elzy

関連する問題