【问题标题】:searchable spinner (toptoche) loading data from mysql可搜索微调器(toptoche)从 mysql 加载数据
【发布时间】:2019-02-26 15:53:15
【问题描述】:

所以我有这个带有搜索栏的 android 微调器,并且想从数据库中加载数据,这就是我目前所拥有的

JSON

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

ma​​inactivity.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/

【问题讨论】:

  • ArrayAdapter adapter = ArrayAdapter.createFromResource(this,your json response array, android.R.layout.simple_spinner_item);

标签: android android-volley spinner


【解决方案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);

【讨论】:

  • 我正在使用具有可搜索文本视图的微调器,所以我正在使用这个 mytrendin.com/implement-search-functionality-android-spinner 但我遇到的问题是我似乎无法从 json 添加数据
  • 将您的 json 解析为 ArrayList 并将其用于微调适配器 ArrayAdapter adapter = new ArrayAdapter (this, android.R.layout.simple_spinner_item,list1);跨度>
【解决方案2】:

所以我设法解决了这个问题 我把我的答案放在这里是因为我找不到任何带有 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);

    }





}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-27
    • 2016-04-09
    • 2021-09-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多