gpt4 book ai didi

Android ActionBar SearchView 自动完成 JSON ListView

转载 作者:行者123 更新时间:2023-11-29 20:26:22 27 4
gpt4 key购买 nike

所以,我正在尝试在我的 actionBar 上创建一个搜索 View ,这样当我键入时,自动完成或在该搜索 View 中以列表格式显示 Json ListView ,这样我就可以单击我刚刚搜索的那些。但我发现它很难实现。其他一切都很好。我可以显示 json 解析元素列表和 geet 可序列化内容。现在我只需要搜索栏即可运行

这是我用于 ListAdapter 和 JSON 解析的 java 类。

public class Collegelist extends ActionBarActivity {

HTTPConnection http;



List<Colleges> college = new ArrayList<Colleges>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.college_list);
http = new HTTPConnection(getApplicationContext());

if (http.isNetworkConnection()) {

//String data = http.HTTPGetData("http://localhost/minorproject/show.php");
//Toast.makeText(getApplicationContext(),data ,Toast.LENGTH_LONG).show();
task.execute();
}
else {


Toast.makeText(getApplicationContext(), "check your connection",
Toast.LENGTH_LONG).show();
}
}


AsyncTask<Void, Void, String> task = new AsyncTask<Void, Void, String>() {

@Override
protected String doInBackground(Void... params) {
String data = http.HTTPGetData("http://my-ip-address/minorproject/show.php");
return data;


}



@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
populateList(result);
displayList();
}

@Override
protected void onPreExecute() {
super.onPreExecute();

}

};
protected void populateList(String result) {
try {
//Toast.makeText(getApplicationContext(),result,Toast.LENGTH_LONG).show();
JSONObject jobj = new JSONObject(result);
String res = jobj.getString("success");
if (!res.equals("true")) {
Toast.makeText(getApplicationContext(), "JSON Error",
Toast.LENGTH_LONG).show();
return;
}
else
{
JSONArray data = jobj.getJSONArray("msg");
// Toast.makeText(getApplicationContext(),"successss",Toast.LENGTH_SHORT).show();
for (int i = 0; i < data.length(); i++) {
JSONObject col = data.getJSONObject(i);
Colleges cg = new Colleges(col.getString("cname"),
col.getString("caddress"),
col.getString("ccontact_a"),
col.getString("ccontact_b"),
col.getString("cwebsites"));

college.add(cg);
}
}

} catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
}


}


protected void displayList() {
ArrayAdapter<Colleges> adapter = new ArrayAdapter<Colleges>(this, R.layout.list_item,college){

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = getLayoutInflater().inflate(R.layout.list_item,null);
//set values
Colleges c = college.get(position);
((TextView)view.findViewById(R.id.name)).setText(c.getName());
/*((TextView)view.findViewById(R.id.address)).setText(c.getAddress());*/



return view;
}

};

final ListView collegelistnew = (ListView) findViewById(R.id.listView);
collegelistnew.setAdapter(adapter);

collegelistnew.setOnItemClickListener(new AdapterView.OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
/*Toast.makeText(
getApplicationContext(),
"You clicked position" + position + "with item name"
+ college.get(position).getName(),
Toast.LENGTH_LONG).show();*/


Intent newIntent =new Intent(getApplicationContext(),CollegeDetails.class);

newIntent.putExtra("college", college.get(position));
startActivity(newIntent);



}

});

}

and my getter and setter class

public class Colleges implements Serializable{
String cname,caddress,ccontact,cwebsites,ccontactb;

public Colleges( String cname, String cwebsites,String caddress, String ccontact, String ccontactb) {
this.cname = cname;
this.caddress = caddress;
this.ccontact = ccontact;
this.ccontact = ccontactb;
this.cwebsites = cwebsites;

}

public String getName() {
return cname;
}

public void setName(String cname) {

this.cname = cname;
}

public String getAddress() {

return caddress;
}

public void setAddress(String caddress) {

this.caddress = caddress;
}

public String getContact() {
return ccontact;
}

public void setContact(String ccontact) {
this.ccontact = ccontact;
}

public String getContactb() {
return ccontactb;
}

public void setContactb(String ccontactb) {
this.ccontactb = ccontactb;
}

public String getWebsites(){
return cwebsites;
}

public void setWebsites(String cwebsites){
this.cwebsites = cwebsites;
}

这是 menu_main xml 文件

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
<item android:id="@+id/action_settings" android:title="@string/action_settings"
android:orderInCategory="100" app:showAsAction="never" />

<item android:id="@+id/action_search"
android:icon="@drawable/search"
android:title="@string/search_title"
app:showAsAction="always|collapseActionView"
android:actionViewClass="android.widget.SearchView"
app:actionViewClass="android.support.v7.widget.SearchView"/>

最佳答案

假设您的列表已准备就绪,并且适配器也已准备好显示。

SearchView 中,您可以获得的是用户输入或输入的当前文本。您现在要做的是将此文本传递到一个方法中,该方法将创建新的过滤列表。

onCreateOptionsMenu

SearchManager searchManager = (SearchManager) SearchActivity.this.getSystemService(Context.SEARCH_SERVICE);
SearchView searchView;

if (searchItem != null) {
searchView = (SearchView) searchItem.getActionView();

if (searchView != null) {

searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

@Override
public boolean onQueryTextSubmit(String s) {
setSearchAdapter(s);
return false;
}

@Override
public boolean onQueryTextChange(String s) {
setSearchAdapter(s);
return false;
}

});
}
}

您将在方法 setSearchAdapter(s) 中执行您的列表任务,基于字符串“s”,您将必须为将使用适配器填充的每个文本更改重新创建一个列表。

你的方法应该是这样的

private void setSearchAdapter(String s) {
// filter list codes
// ....
searchAdapter =new SearchAdapter(SearchActivity.this,
filteredList);
listView.setAdapter(searchAdapter);
}

创建过滤列表的方法有很多种。

  1. 如果您想将数据保存在数据库中,那么您可以创建虚拟表并查询查询,如下所示

    游标 cursor = myDataProvider.query(VirtualTable.CONTENT_URI, null, VirtualTable.NAME + "MATCH ? ", new String[]{s + "*"}, null, null);

注意:为简单起见,您可以使用原始匹配查询。

然后使用合适的适配器类填充它。

  1. 我们可以使用 Filter异步过滤器的类如下:

    私有(private)类 MyFilter 扩展过滤器 {

    @Override
    protected FilterResults performFiltering(CharSequence constraint) {
    FilterResults results = new FilterResults();
    for (int i = 0; i < collegeList.size(); i++) {
    String name = collegeList.get(i);
    if (name.toUpperCase().startsWith(constraint.toString().toUpperCase())) {
    nFilteredList.add(name);
    }
    }
    results.values = nFilteredList;
    results.count = nFilteredList.size();
    return results;
    }

    @Override
    protected void publishResults(CharSequence constraint, FilterResults results) {
    filteredList = (ArrayList<String>) results.values;
    }

您需要在文本更改时调用 searchAdapter.getFilter().filter(s); 并覆盖适配器中的 getFilter() 方法。

我希望这能解决您的问题。

关于Android ActionBar SearchView 自动完成 JSON ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32454694/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com