Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在Android Studio中仅显示数据库中选定的数据_Java_Php_Android_Mysql_Database - Fatal编程技术网

Java 如何在Android Studio中仅显示数据库中选定的数据

Java 如何在Android Studio中仅显示数据库中选定的数据,java,php,android,mysql,database,Java,Php,Android,Mysql,Database,我的程序设计用于在数据库中搜索输入的特定名称,然后程序将只显示特定名称的行。但是,我的代码无法显示特定数据。我试过很多种方法,但都不能解决我的问题。我的php代码如下: <?php $con = mysql_connect('mysql17.000webhost.com', 'a2634311_minzhe', 'MZRules0118'); if (!$con) { die('Could not connect: ' . mysql_er

我的程序设计用于在数据库中搜索输入的特定名称,然后程序将只显示特定名称的行。但是,我的代码无法显示特定数据。我试过很多种方法,但都不能解决我的问题。我的php代码如下:

    <?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;
        }

    }

我的活动代码如下:

    <?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;
        }

    }
导入android.app.Activity;
导入android.content.Context;
导入android.content.Intent;
导入android.content.SharedReferences;
导入android.os.AsyncTask;
导入android.os.StrictMode;
导入android.support.v7.app.ActionBarActivity;
导入android.os.Bundle;
导入android.util.Log;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.BaseAdapter;
导入android.widget.Button;
导入android.widget.ListView;
导入android.widget.TextView;
导入android.widget.Toast;
导入org.json.JSONArray;
导入org.json.JSONException;
导入org.json.JSONObject;
导入java.io.InputStream;
公共类PatientLocation扩展了ActionBarActivity{
私有列表视图PatientLocationListView;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
StrictMode.ThreadPolicy policy=新建StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(策略);
this.PatientLocationListView=(ListView)this.findViewById(R.id.listView4);
新位置()。执行(新API_连接器());
}
public void setListAdapter(JSONArray JSONArray){
SharedReferences设置=GetSharedReferences(“MyPreferences”,Context.MODE\u PRIVATE);
String name=settings.getString(“name”,”);
this.PatientLocationListView.setAdapter(新的PatientAdapter(jsonArray,name,this));
}
私有类位置扩展异步任务{
@凌驾
受保护的JSONArray doInBackground(API_连接器…参数){
//它在后台线程上执行
返回参数[0]。GetPatientLocation();
}
@凌驾
受保护的void onPostExecute(JSONArray JSONArray){
//检查空值
if(jsonArray!=null){
//仅当数组不为null时才将其传递给适配器
setListAdapter(jsonArray);
}否则{
Toast.makeText(PatientLocation.this,“返回空数组”,Toast.LENGTH_SHORT.show();
}
}
}
}
类PatientAdapter扩展了BaseAdapter{
私有JSONArray数据阵列;
私人活动;
字符串pname;
专用静态充气机=空;
公共PatientAdapter(JSONArray JSONArray,字符串名称,活动a){
this.dataArray=jsonArray;
本活动=a;
this.pname=名称;
充气器=(LayoutFlater)this.activity.getSystemService(Context.LAYOUT\u充气器\u服务);
}
@凌驾
public int getCount(){
返回此.dataArray.length();
}
@凌驾
公共对象getItem(int位置){
返回位置;
}
@凌驾
公共长getItemId(int位置){
返回位置;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
字符串patientname,patientlocation;
//设置转换视图(如果为空)
ListCell=新ListCell();
if(convertView==null){
convertView=充气机充气(右布局活动位置,空);
cell.patient_name=(TextView)convertView.findViewById(R.id.textView30);
cell.patient_location=(TextView)convertView.findViewById(R.id.textView31);
convertView.setTag(单元格);
}
否则{
cell=(ListCell)convertView.getTag();
}
//更改单元格的数据
试一试{

对于(int i=0;iphp代码中查询中的Where is Where子句?您需要设置一些条件以匹配查询中的特定名称,例如,如果要搜索'abcd',则您的查询应该是
从groupdesign中选择*,其中column\u name=“abcd”
。这将返回包含值“abcd”的记录使用
AsyncTask
时,无需使用
StrictMode.ThreadPolicy
方法,您是否从服务器获取数据?好的,谢谢您提供的信息。是的,我正在从服务器获取数据。在视图类中,当我删除if case时,它似乎将显示来自服务器的所有数据。但是,使用if case、 它无法显示任何内容。您的问题不清楚。请详细说明获取当前代码的问题。我的代码仅在数据库中的名称相等时显示数据。感谢您的帮助。我将尝试通过更改我的android代码来解决此问题