如何实现对使用 ListAdapater、ArrayList 和 HashMap 的 ListView 的搜索 [英] How to implement a search for a ListView that uses ListAdapater, ArrayList and HashMap

查看:20
本文介绍了如何实现对使用 ListAdapater、ArrayList 和 HashMap 的 ListView 的搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从 JSON 数组填充的列表视图.首先从 JSON 数组中提取值,然后放入 HashMap.然后将这个 HashMap 添加到一个 ArrayList 中.从这里开始,必要的数据被添加到 ListAdapter.

I have a list view that I populate from a JSON array. First the values are extracted from the JSON array, then put into a HashMap. Then this HashMap is added to an ArrayList. From here, the necessary data is added to a ListAdapter.

我在网上搜索了好几天,想找到一种方法来为此添加搜索功能,但是他们都没有使用 ListAdapater、ArrayList 和 HashMap 的这种组合.我是初学者,请问有人可以帮我做吗?

I searched online for days now, to find a way to add a search function to this, but none of them uses this combination of ListAdapater, ArrayList and HashMap. I am a beginner so can someone help me do this please?

这是我的代码

public class ListNew extends Activity {
ListView list;
TextView eid;
TextView ename; 
TextView edesc;
//Button Btngetdata;
ArrayList<HashMap<String, String>> oslist = new ArrayList<HashMap<String, String>>();

//URL to get JSON Array
private static String url = "somelink";

//JSON Node Names 
private static final String TAG_OS = "events";
private static final String TAG_EID = "eid";
private static final String TAG_ENAME = "ename";
private static final String TAG_ETYPE = "etype";
private static final String TAG_EDESC = "edesc";
private static final String TAG_ESDATE = "esdate";
private static final String TAG_EEDATE = "eedate";
private static final String TAG_ESTIME = "estime";
private static final String TAG_EETIME = "eetime";
private static final String TAG_LOCATION = "location";
private static final String TAG_CREATED_AT = "created_at";
private static final String TAG_EDITED_AT = "edited_at";
private static final String TAG_CREATEDBY = "createdby";
private static final String TAG_IMAGE = "image";
private static final String TAG_APPROVED = "approved";

EditText inputSearch;
ListAdapter adapter;


public static final String TABLE_EVENT = "event";



JSONArray events = null;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //from here
    inputSearch = (EditText) findViewById(R.id.etSearch);

    setContentView(R.layout.listnew);
    oslist = new ArrayList<HashMap<String, String>>();

    new JSONParse().execute();


}

private class JSONParse extends AsyncTask<String, String, JSONObject> {
     private ProgressDialog pDialog;
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
         eid = (TextView)findViewById(R.id.vers);
         ename = (TextView)findViewById(R.id.name);
         edesc = (TextView)findViewById(R.id.api);
        pDialog = new ProgressDialog(ListNew.this);
        pDialog.setMessage("Getting Data ...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();



    }

    @Override
    protected JSONObject doInBackground(String... args) {

        JSONParser jParser = new JSONParser();

        // Getting JSON from URL
        JSONObject json = jParser.getJSONFromUrl2(url);
        return json;
    }
     @Override
     protected void onPostExecute(JSONObject json) {
         pDialog.dismiss();
         try {
                // Getting JSON Array from URL
                events = json.getJSONArray(TAG_OS);
                for(int i = 0; i < events.length(); i++){
                JSONObject c = events.getJSONObject(i);

                // Storing  JSON item in a Variable
                String eid = c.getString(TAG_EID);
                String ename = c.getString(TAG_ENAME);
                String etype = c.getString(TAG_ETYPE);
                String edesc = c.getString(TAG_EDESC);
                String esdate = c.getString(TAG_ESDATE);
                String eedate = c.getString(TAG_EEDATE);
                String estime = c.getString(TAG_ESTIME);
                String eetime = c.getString(TAG_EETIME);
                String location = c.getString(TAG_LOCATION);
                String created_at = c.getString(TAG_CREATED_AT);
                String edited_at = c.getString(TAG_EDITED_AT);
                String createdby = c.getString(TAG_CREATEDBY);
                String image = c.getString(TAG_IMAGE);
                String approved = c.getString(TAG_APPROVED);

                HashMap<String, String> map = new HashMap<String, String>();

                map.put(TAG_EID, eid);
                map.put(TAG_ENAME, ename);
                map.put(TAG_ETYPE, etype);
                map.put(TAG_EDESC, edesc);
                map.put(TAG_ESDATE, esdate);
                map.put(TAG_EEDATE, eedate);
                map.put(TAG_ESTIME, estime);
                map.put(TAG_EETIME, eetime);
                map.put(TAG_LOCATION, location);
                map.put(TAG_CREATED_AT, created_at);
                map.put(TAG_EDITED_AT, edited_at);
                map.put(TAG_CREATEDBY, createdby);
                map.put(TAG_IMAGE, image);
                map.put(TAG_APPROVED, approved);


                oslist.add(map);
                list=(ListView)findViewById(R.id.list);
                TextView emptyText = (TextView)findViewById(R.id.empty);
                list.setEmptyView(emptyText);

                //Log.d("TAG_is d first one", TAG_APPROVED + "," + approved);



                adapter = new SimpleAdapter(ListNew.this, oslist,
                        R.layout.list_v,
                        new String[] { TAG_ENAME,TAG_CREATEDBY, TAG_CREATED_AT }, new int[] {
                                R.id.vers,R.id.name, R.id.api});


                list.setAdapter(adapter);

                list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                    @Override
                    public void onItemClick(AdapterView<?> parent, View view,
                                            int position, long id) {
                        Intent i = new Intent(ListNew.this, NewEvent.class);
                        i.putExtra("eid", oslist.get(+position).get("eid") ); 
                        i.putExtra("ename", oslist.get(+position).get("ename") );
                        i.putExtra("etype", oslist.get(+position).get("etype") );
                        i.putExtra("edesc", oslist.get(+position).get("edesc") );
                        i.putExtra("esdate", oslist.get(+position).get("esdate") );
                        i.putExtra("eedate", oslist.get(+position).get("eedate") );
                        i.putExtra("estime", oslist.get(+position).get("estime") );
                        i.putExtra("eetime", oslist.get(+position).get("eetime") );
                        i.putExtra("location", oslist.get(+position).get("location") );
                        i.putExtra("created_at", oslist.get(+position).get("created_at") );
                        i.putExtra("edited_at", oslist.get(+position).get("edited_at") );
                        i.putExtra("createdby", oslist.get(+position).get("createdby") );
                        i.putExtra("image",  byteArray);
                        i.putExtra("approved", oslist.get(+position).get("approved") );
                        startActivity(i);

                    }
                });


            }//end if
        } catch (JSONException e) {
            e.printStackTrace();
        }




     }


}

}

推荐答案

根据您的描述,我相信您的列表视图有自定义适配器实现.如果自定义适配器需要通过实现来创建自己的过滤器http://developer.android.com/reference/android/widget/Filterable.html

As per you description I believe you have custom adapter implementation for your list view. In case of custom adapters yon need to create your own filters by implementing http://developer.android.com/reference/android/widget/Filterable.html

http://developer.android.com/reference/android/widget/过滤器.html

以下是获取灵感的简单教程

Below is the simple tutorial to take inspiration

http://www.survivingwithandroid.com/2012/10/android-listview-custom-filter-and.html

========== 简单搜索示例 ===

========== Example for simple search ===

您需要在列表视图顶部创建一个编辑文本

You need to create an Edit text on top of list view

<EditText
         android:id="@+id/editTxt"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:layout_marginTop="5dp"
         android:maxLines="1" />

然后将文本更改侦听器应用于此编辑文本

Then apply text change listener to this edit text

editTxt.addTextChangedListener(new TextWatcher() {

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        adapter.getFilter().filter(s.toString());                           
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {

    }

    @Override
    public void afterTextChanged(Editable s) {
    }
});

你就完成了.

这篇关于如何实现对使用 ListAdapater、ArrayList 和 HashMap 的 ListView 的搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆