gpt4 book ai didi

java - SearchView 过滤器没有产生预期的结果

转载 作者:行者123 更新时间:2023-11-30 01:20:31 26 4
gpt4 key购买 nike

我在 here 中解决了我的问题之后当我在搜索框中输入文本时,搜索过滤器仍然不起作用。

这是我的代码:

package intikom.streammobile.Customer;

import android.app.ProgressDialog;
import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.SearchView;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

import java.util.ArrayList;
import java.util.List;

import intikom.streammobile.Index;
import intikom.streammobile.R;
import intikom.streammobile.Utility.BigProccess;
import intikom.streammobile.Utility.RESTClient;
import intikom.streammobile.Workflow.MyRequest.ListRequestItem;
import intikom.streammobile.Workflow.WorkflowActivity;

public class ListCustomer extends AppCompatActivity implements SearchView.OnQueryTextListener {
private static final String ARG_SECTION_NUMBER = "section_number";
private static String URLService;
private static String rowsPerPage;
private static ListView lv;
private static SharedPreferences pref;
private List<ListCustomerItem> mItems;
private List<ListCustomerItem> mItems2;
private ListCustomerItem item;
private static String customerID;


public ListCustomer() {
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_customer);

URLService = getString(R.string.URLService);
final ListView lv = (ListView)findViewById(R.id.lvCustomerList);
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
item = new ListCustomerItem();
item = mItems.get(position);
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putString("CustomerID", item.CustID);
editor.putString("CustomerName", item.CustName);
editor.commit();


Intent i = new Intent(getApplicationContext(),WorkflowActivity.class);
i.putExtra("CustomerName", item.CustName);
setResult(1000, i);
finish();

}
});

getCustomer();
getLocal();
if (mItems==null)
{
if(mItems2==null) {
Toast.makeText(this, "Internet connection appears to be offline",
Toast.LENGTH_LONG).show();
}
else
{
lv.setAdapter(new ListCustomerAdapter(this, mItems2));

}
}
else
{
lv.setAdapter(new ListCustomerAdapter(this, mItems));

}

getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

getMenuInflater().inflate(R.menu.searchbox, menu);

SearchManager searchManager =
(SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView =
(SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));

searchView.setSubmitButtonEnabled(true);
searchView.setOnQueryTextListener(this);

return true;
}


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

@Override
public boolean onQueryTextChange(String newText) {
if (TextUtils.isEmpty(newText))
{
lv.clearTextFilter();
}
else
{
lv.setFilterText(newText.toString());
}


return true;
}
}

我的适配器

package intikom.streammobile.Customer;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import java.util.List;

import intikom.streammobile.R;

/**
* Created by agilang on 20/01/2016.
*/

public class ListCustomerAdapter extends ArrayAdapter<ListCustomerItem> {
public ListCustomerAdapter(Context context, List<ListCustomerItem> items)
{
super(context, R.layout.style_fragment_list_customer, items);
}

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

if(convertView == null) {
// inflate the GridView item layout
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.style_fragment_list_customer, parent, false);

// initialize the view holder
viewHolder = new ViewHolder();
viewHolder.tvCustName = (TextView) convertView.findViewById(R.id.tvCustName);
viewHolder.tvCustLocation = (TextView) convertView.findViewById(R.id.tvCustLocation);

convertView.setTag(viewHolder);
} else {
// recycle the already inflated view
viewHolder = (ViewHolder) convertView.getTag();
}

// update the item view
ListCustomerItem item = getItem(position);
viewHolder.tvCustName.setText(item.CustName);
viewHolder.tvCustLocation.setText(item.CustLocation);

return convertView;
}

/**
* The view holder design pattern prevents using findViewById()
* repeatedly in the getView() method of the adapter.
*
* @see http://developer.android.com/training/improving-layouts/smooth-scrolling.html#ViewHolder
*/
private static class ViewHolder {
TextView tvCustName;
TextView tvCustLocation;
}
}

和菜单

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

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

这里是在输入文本之前 enter image description here

过滤后

enter image description here

感谢任何帮助。

最佳答案

我认为你的代码://在你的代码中的某些地方(比如 onCreate)像这样定义你的 Adapter:

ListCustomerAdapter adapter = new ListCustomerAdapter(this, mItems2);
...

然后将 onQueryTextChange 方法更改为如下所示:

public boolean onQueryTextChange(String newText) {
if (TextUtils.isEmpty(newText))
{
adapter.getFilter().filter("");
lv.clearTextFilter();
}
else
{
lv.setFilterText(newText);
adapter.getFilter().filter(newText);
}
return true;
}

您可能还想查看 suggestion here about creating a custom Filter to be returned by the getFilter() method of your Adapter .我希望这会有所帮助。

关于java - SearchView 过滤器没有产生预期的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37182979/

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