在TextView

时间:2017-05-23 21:19:37

标签: java android android-spinner

我有一个searchablespinner使用库https://github.com/michaelprimez/searchablespinner和Items: 中国,印度,美国,印度尼西亚,巴西,........!

OnItemSelectedlistener 直接在某个项目上联合状态时,在Textview上它会正确显示联合状态项;但是当我在微调器中进行搜索并且我点击联合状态时,TextView上显示的项目是字符串中的第一项,它是中国

我正在寻找一种方法来在搜索时显示所选项目,而无需通过该方法:

adapter.getItem(position).

MainActivity.java

public class MainActivity extends AppCompatActivity {

    private SearchableSpinner spinner;
    private ArrayList<String> country;
    private ListAdapter adapter;
    private JSONArray result;
    private TextView txtCountry;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        country = new ArrayList<>();
        txtCountry = (TextView) findViewById(R.id.textCountry);

        spinner = (SearchableSpinner) findViewById(R.id.spinner);
        spinner.setOnItemSelectedListener(mOnItemSelectedListener);

        getData();
    }

    private void getData(){

        StringRequest stringRequest = new StringRequest(Config.DATA_URL, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                JSONObject j = null;
                try {
                    j = new JSONObject(response);
                    result = j.getJSONArray(Config.JSON_ARRAY);
                    getCountry(result);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                    }});

        RequestQueue requestQueue = Volley.newRequestQueue(this);
        requestQueue.add(stringRequest);
    }

    private void getCountry(JSONArray j){
        for(int i=0;i<j.length();i++){
            try {
                JSONObject json = j.getJSONObject(i);
                country.add(json.getString(Config.TAG_COUNTRY));
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        adapter = new ListAdapter(MainActivity.this, R.layout.spinner_item, country);
        spinner.setAdapter(adapter);
    }

    private String getCountry(int position) {
        String name="";
        try {
            JSONObject json = result.getJSONObject(position);
            name = json.getString(Config.TAG_COUNTRY);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return name;
    }


    private OnItemSelectedListener mOnItemSelectedListener = new OnItemSelectedListener() {
        @Override
        public void onItemSelected(View view, int position, long id) {
            txtCountry.setText(getCountry(position));

        }

        @Override
        public void onNothingSelected() {
            txtCountry.setText("");
        }
    };

}

ListAdapter.java

public class ListAdapter extends BaseAdapter implements Filterable {

    private List<String>mStrings = null;
    private List<String>filteredData = null;
    private LayoutInflater mInflater;
    private ItemFilter mFilter = new ItemFilter();

    public ListAdapter(Context context, int spinner_item, List<String> data) {
        this.filteredData = data ;
        this.mStrings = data ;
        mInflater = LayoutInflater.from(context);
    }

    public int getCount() {
        return filteredData.size();
    }

    public Object getItem(int position) {
        return filteredData.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

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

        ViewHolder holder;

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.spinner_item, null );

            holder = new ViewHolder();
            holder.text = (TextView) convertView.findViewById(R.id.DisplayCountry);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.text.setText(filteredData.get(position));
        return convertView;
    }

    static class ViewHolder {
        TextView text;

    }

    public Filter getFilter() {
        return mFilter;
    }

    private class ItemFilter extends Filter {
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {

            String filterString = constraint.toString().toLowerCase();

            FilterResults results = new FilterResults();

            final List<String> list = mStrings;

            int count = list.size();
            final ArrayList<String> nlist = new ArrayList<String>(count);

            String filterableString ;

            for (int i = 0; i < count; i++) {
                filterableString = list.get(i);
                if (filterableString.toLowerCase().contains(filterString)) {
                    nlist.add(filterableString);
                }
            }

            results.values = nlist;
            results.count = nlist.size();

            return results;
        }

        @SuppressWarnings("unchecked")
        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            filteredData = (ArrayList<String>) results.values;
            notifyDataSetChanged();
        }

    }
}

1 个答案:

答案 0 :(得分:0)

在您的活动中尝试此代码。使用您的图片名称而不是我的名字。名称是微调项目中的名称。

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    LinearLayout layout = new LinearLayout(this);
    Spinner sp_iv = new Spinner(this);
    String[] map_list = {
        "Gujarat Map",
        "India Map",
        "World Map"
    };
    ArrayAdapter < String > spinnerAdapter = new ArrayAdapter < String > (this, android.R.layout.simple_spinner_item, map_list);
    spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    sp_iv.setAdapter(spinnerAdapter);
    sp_iv.setOnItemSelectedListener(this);
    layout.addView(sp_iv);
    layout.addView(globalImageWidget);
    setContentView(layout);
}

&lt; ----- AndroidManifest.xml中需要的权限-----&gt;无需许可。