Android:如何使用php mysql实现可搜索微调器

时间:2018-07-26 09:41:28

标签: android android-studio

我想制作一个用php和mysql实现的可搜索微调器,但是我很困惑如何实现这一目标。

这是我的活动

    package id.sch.smktelkom.www.crud;

import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Toast;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.Volley;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.toptoche.searchablespinnerlibrary.SearchableSpinner;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
public class CashDroppingActivity extends AppCompatActivity   implements  SearchableSpinner.OnItemSelectedListener{
SearchableSpinner  spinner;
private AdView mAdView;
String Url="http://192.168.1.109/SqliteSync/country.php";
public static List<String> lst=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_cash_dropping);
    new GetData().execute();
    spinner= (SearchableSpinner) findViewById(R.id.dpnr_country1);
    spinner.setTitle("Countries");
    mAdView = (AdView) findViewById(R.id.adView);
    AdRequest adRequest = new AdRequest.Builder()
            .build();
    mAdView.loadAd(adRequest);
    spinner.setOnItemSelectedListener(this);
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    String text = spinner.getSelectedItem().toString();
    Toast.makeText(getApplicationContext(),text+" is Selected ",Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
class GetData extends AsyncTask<String,String,String>{
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }
    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
    }
    @Override
    protected String doInBackground(String... params) {
        lst=new ArrayList<String>();
        JsonArrayRequest movieReq = new JsonArrayRequest(Url,
                new Response.Listener<JSONArray>()
                {
                    @Override
                    public void onResponse(JSONArray response) {
                        for(int i=0;i<response.length();i++){
                            try {
                                //Getting json object
                                JSONObject json = response.getJSONObject(i);
                                //Adding the name of the student to array list
                                lst.add(json.getString("CountryName"));
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                        ArrayAdapter adapter=new ArrayAdapter(getBaseContext(), R.layout.support_simple_spinner_dropdown_item, lst);
                        spinner.setAdapter(adapter);
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                    }
                });
        //Creating a request queue
        RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
        //Adding request to the queue
        requestQueue.add(movieReq);
        return null;
    }

        }
    }

这是我的程序的xml文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:ads="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".CashDroppingActivity">

<com.toptoche.searchablespinnerlibrary.SearchableSpinner
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:spinnerMode="dropdown"
    android:id="@+id/dpnr_country1"
    android:background="#e392e6"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="206dp" />
<com.google.android.gms.ads.AdView
    android:id="@+id/adView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    ads:adSize="BANNER"
    ads:adUnitId="ca-app-pub-4229214732740771/5218310041"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginBottom="20dp"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true">
</com.google.android.gms.ads.AdView>
</RelativeLayout>

这是我的php文件

    <?php


require_once('koneksi.php');
   $sql = "SELECT * FROM collect";
$result = mysqli_query($con, $sql);

if ($result->num_rows >0) {
 // output data of each row
 while($row[] = $result->fetch_assoc()) {

 $tem = $row;

 $json = json_encode($tem);

}}
 else {
 echo "0 results";
}
 echo $json;
$con->close();?>

我已经尝试做过,但是最后当我运行它时,数据没有显示    并不会崩溃,只是数据没有显示

非常感谢您的帮助。谢谢:D

1 个答案:

答案 0 :(得分:0)

如果您正在寻找一个像网站一样可搜索的下拉列表。

使用此Library。您可以通过API从服务器检索所有微调器元素,并将其存储在ArrayList中。并在微调适配器中使用此ArrayList。该库会执行所有操作以显示搜索下拉列表(微调框)。希望这会有所帮助。

演示:enter image description here