GridVIew不使用SearchView& amp;显示过滤数据。自定义ArrayAdapter

时间:2016-09-03 10:44:17

标签: java android json android-gridview searchview

我无法显示我的GridView数据以便用实际结果进行搜索。

我正在使用SearchView过滤我的数据,它已经过我的输入过滤但未显示实际的过滤数据。好像我在搜索后单击GridView项目然后它在Toast中给我正确的结果。

查看屏幕截图以便更好地理解。

这是我的第一个活动,我将所有json数据(首先是18个字符串)提取到我的自定义GridView中。

enter image description here

现在,当我用' S' char然后它从s开始过滤数据,当我点击项目' five'它给了我吐司的结果,因为十七岁和十七岁。这是正确的结果。但是你可以看到它没有显示实际结果。

结果应该是(在第0个位置 - >'第二个',第一个位置 - >'六个',第三个位置 - >'七个',第四位 - >'十六',第五位 - >'十七')

enter image description here

这是我的Json数据链接 JsonData

menu.xml文件     

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<item
    android:id="@+id/search"
    android:icon="@drawable/search_new"
    android:title="Search"
    app:actionViewClass="android.support.v7.widget.SearchView"
    app:showAsAction="collapseActionView|ifRoom" />

</menu>

MainActivity.java

public class MainActivity extends AppCompatActivity {

public static final String url = "http://cdp.esy.es/Rekhaben/fetch_gallery.php";

GridView gridView;

cust cust_layout;


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

    gridView = (GridView) findViewById(R.id.list);

    sendRequest();


    gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            Toast.makeText(MainActivity.this, "" + gridView.getItemAtPosition(i), Toast.LENGTH_SHORT).show();
        }
    });

}

private void sendRequest() {

    StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            Log.e("res", response);
            showJSON(response);
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

        }
    });

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

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main_menu, menu);


    SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {

            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {

            cust_layout.getFilter().filter(newText);

            return true;
        }
    });

    return true;
}

private void showJSON(String json) {

    Parse_JSON pj = new Parse_JSON(json);
    pj.convertJSON();
    cust_layout = new cust(this, Parse_JSON.txt);
    gridView.setAdapter(cust_layout);
    gridView.setTextFilterEnabled(true);

}
}

更新 cust.java(GridView的自定义适配器)

public class cust extends ArrayAdapter<String> implements Filterable {

private Activity context;
private String[] txt;
private String[] mStringFilterList;

valueFilter aa;

public cust(Activity context, String[] txt) {
    super(context, R.layout.cust, txt);

    this.context = context;
    this.txt = txt;
    mStringFilterList = txt;

    // notifyDataSetChanged();
}

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

    LayoutInflater inflater = context.getLayoutInflater();

    View view = inflater.inflate(R.layout.cust, null, true);

    TextView textView = (TextView) view.findViewById(R.id.txt);

    textView.setText(txt[position]);


    return view;
}

@Override
public Filter getFilter() {
    if (aa == null) {
        aa = new valueFilter();
    }
    return aa;
}

private class valueFilter extends Filter {

    @Override
    protected FilterResults performFiltering(CharSequence constraint) {
        FilterResults results = new FilterResults();
        if (constraint != null && constraint.length() > 0) {

            //String filterList[] = null;
            ArrayList<String> filterList = new ArrayList<String>();
            for (int i = 0; i < mStringFilterList.length; i++) {

                if ((mStringFilterList[i].toUpperCase()).contains(constraint.toString().toUpperCase())) {
                    // filterList[i] = mStringFilterList[i];

                    filterList.add(mStringFilterList[i]);
                }

                /*if ((mStringFilterList.get(i).getName().toUpperCase()).contains(constraint.toString().toUpperCase())) {
                    Country country = new Country(mStringFilterList.get(i).getName());


                    filterList.add(country);
                }*/
            }

            results.count = filterList.size();
            results.values = filterList;
        } else {
            results.count = mStringFilterList.length;
            results.values = mStringFilterList;
        }

        return results;
    }

    @Override
    protected void publishResults(CharSequence charSequence, FilterResults results) {
        Log.e("FILTERED VALUESSSSS", String.valueOf(results.values));
        Log.e("FILTERED COUNT", String.valueOf(results.count));

        txt = (String[]) results.values;
        notifyDataSetChanged();
    }
}
}

ParseJson.java(用于解析json数据的Java类)

public class Parse_JSON {

public static String[] txt;

JSONArray users = null;

public static final String KEY_ARRAY = "result";
public static final String KEY_IMG = "pic_path";
public static final String KEY_TXT = "pic_title";

private String json;

public Parse_JSON(String json) {
    this.json = json;
}

protected void convertJSON() {
    JSONObject jsonObject = null;
    try {
        jsonObject = new JSONObject(json);
        users = jsonObject.getJSONArray(KEY_ARRAY);

        txt = new String[users.length()];

        for (int i = 0; i < users.length(); i++) {
            JSONObject jo = users.getJSONObject(i);
            txt[i] = jo.getString(KEY_TXT);
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
}
}

0 个答案:

没有答案