Android json内容的搜索功能
我开发了一个android应用程序,其中所有内容都是从json获取的,json使用asynctask完成,并在ListView中填充。我在应用程序中有近10个自定义列表视图。现在我想在主页中实现一个搜索。我是android新手。请建议我,如何实施它。只需要搜索json对象中的titles节点。我知道如何设置搜索小部件、listview xml、添加清单文件元数据以及将搜索活动添加到主活动。我想知道,如何实现对所有自定义列表视图的单一搜索 我的json文件如下所示。自定义列表视图包含图像、标题和说明Android json内容的搜索功能,android,json,search,Android,Json,Search,我开发了一个android应用程序,其中所有内容都是从json获取的,json使用asynctask完成,并在ListView中填充。我在应用程序中有近10个自定义列表视图。现在我想在主页中实现一个搜索。我是android新手。请建议我,如何实施它。只需要搜索json对象中的titles节点。我知道如何设置搜索小部件、listview xml、添加清单文件元数据以及将搜索活动添加到主活动。我想知道,如何实现对所有自定义列表视图的单一搜索 我的json文件如下所示。自定义列表视图包含图像、标题和说
[
{
"name":"Steve",
"title":"How things work",
"image":url,
},
{
"name":"Jack",
"date":"helium extraction",
"image":url,
}
]
您可以做一件事来快速搜索,将所需数据保存在arrayList中,只需使用“.contains”运行for循环 使用后,您将在JSONOBJECV中获得json响应:
title = jsonObjRecv.getString("title");
现在
if(title.contains(//your search string)){//conditions and code }
1。首先,创建一个对象
User
,例如:
public class User{
String name;
String title;
String UrlImage;
// setter
// getter
}
2.然后创建一个ListAdapter扩展BaseAdapter
来填充ListView中的数据
(从中查看此教程)
另请参见(法语)中的,以了解如何在custiom适配器中填充数据
3.对于搜索功能,请创建包含EditText
searchEditText的布局布局\u搜索
4.在每个包含listView和您的数据(来自Json)的布局中包含此布局
5.在异步任务中,将每个JsonObject
添加到ArrayList
中,在解析结束时,您将拥有一个ArrayList,其中包含所有数据[User(name,title,url)]
,并填充到listView中
6.b回到搜索功能,在每个活动中向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
}
});
EditText fillSearch=(EditText)findViewById(R.id.searchEditText);
fillSearch.addTextChangedListener(新的TextWatcher(){
@凌驾
public void onTextChanged(字符序列arg0、int arg1、int arg2、int arg3){
//TODO自动生成的方法存根
}
@凌驾
更改前的公共void(字符序列arg0、int arg1、int arg2、,
int arg3){
//TODO自动生成的方法存根
}
@凌驾
public void PostTextChanged(可编辑arg0){
//TODO自动生成的方法存根
//TODO自动生成的方法存根
String text=fillSearch.getText().toString().toLowerCase(Locale.getDefault());
ArrayList=搜索(文本);//我们的函数
}
});
7.添加搜索方法,返回包含我们搜索的所有数据的列表(从用户在EditText中输入的文本开始):
publicArrayListSearch(字符串s)
{
ArrayList=新建ArrayList();
for(User u:listUsers){//listUsers=包含在异步任务中解析的所有数据
if(User.getTitle().toLowerCase().startsWith)
{
增加(u);
}
}
退货清单;
}
对于不同的自定义视图,活动是否有不同的片段?主页中的十个类别作为按钮,我使用了意图来打开指定的活动。片段仅用于带有导航抽屉的主页。感谢您的明确解释。如果我将来更改服务器端的内容,它会起作用吗。我是说json…我不想在所有页面中显示搜索编辑字段。但在第4点中,您提到了类似“4.在每个包含listView和数据(来自Json)的布局中包含此布局”
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
}
});
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;
}