2014-10-07 60 views
0

我开发了一个android应用程序,其中所有的内容都是从json中获取的,这些内容是使用asynctask完成的,并填充在列表视图中。我在应用程序中有近10个自定义列表视图。现在我想在主页中实现一个搜索。我是android新手。请建议我,如何实施它。只有json对象中的标题节点需要搜索。我知道如何设置搜索小部件,列表视图xml,添加清单文件元数据以及搜索活动到主要活动。我想知道,如何实现所有自定义列表视图的单一搜索。json content的搜索功能

我的json文件如下所示。自定义列表视图包含图片,标题和说明。

[ 
    { 
     "name":"Steve", 
     "title":"How things work", 
     "image":url, 
    }, 
    { 
     "name":"Jack", 
     "date":"helium extraction", 
     "image":url, 
    } 
    ] 
+0

活动是否具有不同的自定义视图不同的片段? – user1728071 2014-10-07 10:59:14

+0

http://www.survivingwithandroid.com/2012/10/android-listview-custom-filter-and.html – 2014-10-07 11:05:09

+0

主页中的十个类别作为按钮,我用意图打开指定的活动。片段仅用于带导航抽屉的主页。 – 2014-10-07 11:09:48

回答

0

首先,创建例如对象User

public class User{ 
    String name; 
    String title; 
    String UrlImage; 

    // setter 
    // getter 

} 

2.然后创建一个ListAdapter extends BaseAdapter ListView中

填充数据0

(看从AndroidHive这个tutoruel)

另见this tutoriel(法语)从TutoZone.info来看看如何在custiom适配器填充数据

对于搜索fonctionality,创建layout layout_search包含EditTextsearchEditText

4.包括每个布局此布局包含一个ListView与您的数据(从JSON)

5.在你asynckTask,添加每个JsonObject至A ArrayList<User>,在解析你将有一个ArrayList的结束包含所有数据[User(name, title,url)]的,人口在你的ListView

6. BVack到搜索fonctionality,每个活动添加一个侦听到EDITTEXT:addTextChangedListener:

EditText fillSearch=(EditText)findViewById(R.id.searchEditText); 

fillSearch.addTextChangedListener(new TextWatcher() { 

      @Override 
      public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { 
       // TODO Auto-generated method stub 

      } 

      @Override 
      public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, 
        int arg3) { 
       // TODO Auto-generated method stub 

      } 

      @Override 
      public void afterTextChanged(Editable arg0) { 
       // TODO Auto-generated method stub 
       // TODO Auto-generated method stub 
       String text = fillSearch.getText().toString().toLowerCase(Locale.getDefault()); 

       ArrayList<User> list = search(text); // our function 
      } 
     }); 
返回列表

7.添加搜索方法中包含的所有数据ythat我们搜索(开始与用户的EditText输入的文本):

public ArrayList<User>search(String s) 
{ 
    ArrayList<User> list= new ArrayList<User>(); 

     for (User u: listeUsers) { // listUsers= contains all data parsed in asynckTask 
      if(User.getTitle().toLowerCase().startsWith(s)) 
      { 
       list.add(u); 
      } 
      } 
     return list; 
} 
+0

感谢您的明确解释。如果我将来更改服务器端的内容,它会起作用吗?我的意思是json ... – 2014-10-07 11:43:04

+0

我不想在所有页面中显示搜索编辑字段。但在第4点中,您提到了类似“4.将此布局包含在每个包含带有您的数据的listView(来自Json)的布局中” – 2014-10-07 11:58:37

0

你可以做一件事的快速搜索,在的ArrayList保存所需的数据THN只是做一个for循环运行用“载”

+0

您能否请您展示您所知道的任何教程或示例代码。 – 2014-10-07 11:10:48

+0

我可能会改变服务器端(JSON)的数据,所以如果我使用“.contains”,它将如何工作? – 2014-10-07 12:00:31

+0

JSONObject jsonObjRecv = HttpClientJSON.SendHttpPost(Appconstants.BASE_URL + Appconstants。DATA,jobjsend);您将在jsonObjRecv中获得json响应,然后使用:title = jsonObjRecv.getString(“title”);现在用if(title.contains(//您的搜索字符串)){//条件和代码} – kiturk3 2014-10-08 10:46:48