可搜索的微调器(toptoche)从mysql加载数据

时间:2019-02-25 10:02:42

标签: android android-volley spinner

所以我有一个带搜索栏的android旋转器,想从数据库中加载数据,这是我到目前为止的

JSON

[
{"name":"one"},{"name":"two"},{"name":"three"},{"name":"four"},{"name":"five"},
{"name":"six"}
]

mainactivity.java

 /*getting the json data part*/
    Spinner spinner = (Spinner) findViewById(R.id.spinner);
            StringRequest stringRequest = new StringRequest(Request.Method.GET, AppConfig.URL_NAME,
                    new Response.Listener<String>() {
                        @Override
                        public void onResponse(String response) {
                            try {
                                JSONArray namesARRAY= new JSONArray(response);

                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            Toast.makeText(MainActivity.this , error.getMessage(),Toast.LENGTH_SHORT).show();
                        }
                    });
    /*spinner part*/
          ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,ARRAY_WILL_GO_HERE, android.R.layout.simple_spinner_item);
                adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                spinner.setAdapter(adapter);

这就是我要打的行

ArrayAdapter.createFromResource(this,ARRAY_WILL_GO_HERE,android.R.layout.simple_spinner_item);

将包含json的数组,如果有人可以告诉我该怎么做,我不知道如何将其放置在那里。 请注意,我正在按照本教程 https://www.mytrendin.com/implement-search-functionality-android-spinner/

2 个答案:

答案 0 :(得分:1)

您可以使用用于旋转器的自定义适配器来完成

public class CustomSpinnerAdapter extends ArrayAdapter<SpinnerBean> {
int groupid;
Activity context;
ArrayList<SpinnerBean> list;
LayoutInflater inflater;

 public CustomSpinnerAdapter(Context context, int groupid, int id, ArrayList<SpinnerBean> list){
    super(context,id,list);
    this.list=list;
    inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    this.groupid=groupid;
}

public View getView(int position, View convertView, ViewGroup parent ){
    View itemView=inflater.inflate(R.layout.prediction_list,parent,false);
    TextView textView=(TextView) itemView.findViewById(R.id.text2);
    textView.setText(list.get(position).getId());

    return itemView;
}

public View getDropDownView(int position, View convertView, ViewGroup
        parent){
    return getView(position,convertView,parent);

}}

xml项

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
    android:id="@+id/text2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="@dimen/margin_10dp"
    android:textAppearance="?android:attr/textAppearanceListItemSmall"
    android:gravity="center_vertical"
    android:background="#f5f5f5"
    android:textSize="10sp"
    android:text="@string/app_name"
    android:minHeight="?android:attr/listPreferredItemHeightSmall" />
 </LinearLayout>

实施

  CustomSpinnerAdapter dataAdapter = new CustomSpinnerAdapter(context, 
  R.layout.prediction_list,R.id.text2,list);
  spinner.setAdapter(dataAdapter);

答案 1 :(得分:0)

所以我设法解决了这个问题 我将答案放在这里,因为我无法通过mysql和Volley教程找到任何可搜索的微调器 首先添加依赖项

implementation 'com.toptoche.searchablespinner:searchablespinnerlibrary:1.3.1'
  implementation 'com.android.volley:volley:1.1.1'

布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:background="#4DB567">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:layout_margin="20dp">

        <com.toptoche.searchablespinnerlibrary.SearchableSpinner
            android:id="@+id/spinner"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="5dp"
            />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Choose From Options"
            android:padding="5dp"
            android:textStyle="bold"/>
    </LinearLayout>


</RelativeLayout>

.java文件

public class MainActivity extends AppCompatActivity {


    List<String> list1 = new ArrayList<String>();
    Spinner spinner ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         spinner = (Spinner) findViewById(R.id.spinner);

        StringRequest stringRequest = new StringRequest(Request.Method.GET, AppConfig.YOUR_URL_FOR_JSON,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        try {
                            JSONArray name = new JSONArray(response);
                            for(int i = 0; i<name.length(); i++)
                            {
                                JSONObject nameObject = name.getJSONObject(i);
                                list1.add(nameObject.getString("names"));                            
                            }

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                },
                new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(SchoolActivity.this , error.getMessage(),Toast.LENGTH_SHORT).show();
            }
        });
        Volley.newRequestQueue(this).add(stringRequest);

        ArrayAdapter<String> adapter = new ArrayAdapter<String> (this, android.R.layout.simple_spinner_item,list1);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);

    }





}