我需要此过滤器的帮助,或使用recyclerView在片段上使用editText进行搜索,当afterTextChanged运行过滤器和吐司显示正确的数据,但在recyclerView中仅显示空白列表时,我尝试创建新的添加列表,但仍然不显示任何内容。 >
这是我的片段
public class HomeFragment extends Fragment {
private SQLiteHandler db;
private TextView welcome;
private TextView jmlInspection;
private RecyclerView mRecyclerView;
private ListAdapter mListadapter;
EditText searchHome;
private ArrayList<DataNote> arraylist = new ArrayList<DataNote>();
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home, container, false);
// SqLite database handler
db = new SQLiteHandler(getActivity().getApplicationContext());
// Fetching user details from sqlite
HashMap<String, String> user = db.getUserDetails();
String name = user.get("name");
welcome = (TextView) view.findViewById(R.id.txtWelcome);
jmlInspection = (TextView) view.findViewById(R.id.txtJmlInspection);
mRecyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
searchHome = (EditText) view.findViewById(R.id.searchHome);
welcome.setText("Welcome, "+name +"!");
jmlInspection.setText("Saat ini anda memiliki 7 Data.");
final LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
mRecyclerView.setLayoutManager(layoutManager);
//ArrayList data = new ArrayList<DataNote>();
for (int i = 0; i < DataNoteImformation.id.length; i++)
{
DataNote wp = new DataNote(
DataNoteImformation.id[i],
DataNoteImformation.branchArray[i],
DataNoteImformation.assetcodeArray[i],
DataNoteImformation.customerArray[i],
DataNoteImformation.licenseplateArray[i]
);
arraylist.add(wp);
}
mListadapter = new ListAdapter(arraylist);
mRecyclerView.setAdapter(mListadapter);
searchHome.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence query, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
String text = searchHome.getText().toString().toLowerCase(Locale.getDefault());
mListadapter.filter(text);
}
});
return view;
}
public class ListAdapter extends RecyclerView.Adapter<ListAdapter.ViewHolder>
{
private ArrayList<DataNote> dataList;
private List<DataNote> filterlist = null;
public ListAdapter(ArrayList<DataNote> data)
{
this.dataList = data;
this.filterlist = data;
}
public class ViewHolder extends RecyclerView.ViewHolder
{
TextView textViewBranch;
TextView textViewAsset;
TextView textViewCustomer;
TextView textViewLicense;
Button btnReceive;
public ViewHolder(View itemView)
{
super(itemView);
this.textViewBranch = (TextView) itemView.findViewById(R.id.branch);
this.textViewAsset = (TextView) itemView.findViewById(R.id.assetcode);
this.textViewCustomer = (TextView) itemView.findViewById(R.id.customername);
this.textViewLicense = (TextView) itemView.findViewById(R.id.licenseplate);
this.btnReceive = (Button) itemView.findViewById(R.id.receiveBtn);
}
}
@Override
public ListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_item, parent, false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(ListAdapter.ViewHolder holder, final int position)
{
holder.textViewBranch.setText(dataList.get(position).getBranch());
holder.textViewAsset.setText(dataList.get(position).getAssetcode());
holder.textViewCustomer.setText(dataList.get(position).getCustomer());
holder.textViewLicense.setText(dataList.get(position).getLisenceplate());
holder.btnReceive.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
//Toast.makeText(getActivity(), "Item " + position + " is clicked.", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getActivity(),
AssetReceiveActivity.class);
intent.putExtra("name",dataList.get(position).getCustomer());
intent.putExtra("code",dataList.get(position).getAssetcode());
intent.putExtra("plat",dataList.get(position).getLisenceplate());
startActivity(intent);
}
});
}
@Override
public int getItemCount()
{
return dataList.size();
}
public void filter(String charText) {
charText = charText.toLowerCase(Locale.getDefault());
filterlist.clear();
if (charText.length() == 0) {
filterlist.addAll(dataList);
}
else
{
for (DataNote wp : dataList)
{
if (wp.getCustomer().toLowerCase().contains(charText.toLowerCase()))
{
//Toast.makeText(getActivity(), "data " + wp.getCustomer() , Toast.LENGTH_SHORT).show();
filterlist.add(wp);
}
}
}
notifyDataSetChanged();
}
}
}
Logcat上没有错误。我为此需要帮助,对不起,英语不好。
答案 0 :(得分:1)
看这个例子
适配器
public class ListAdapter extends RecyclerView.Adapter<ItemHolder> implements Filterable {
private Context iContext;
private List<DataNote > dataList = new ArrayList<>();
private List<DataNote > filterlist = new ArrayList<>();
public ListAdapter (List<DataNote > dataList, Context context) {
this.dataList = dataList;
this.filterlist = dataList;
iContext = context;
}
@Override
public Filter getFilter() {
Filter filter = new Filter() {
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
dataList= (List<DataNote >) results.values;
notifyDataSetChanged();
}
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
List<DataNote> filteredList = new ArrayList<>();
String searchText = constraint.toString().toLowerCase();
String[] split = searchText.split(",");
ArrayList<String> searchGenres = new ArrayList<>(split.length);
for (String aSplit : split) {
// remove spaces
String trim = aSplit.trim();
// skip empty entries
if (trim.length() > 0)
searchGenres.add(trim);
}
for (DataNote dataNames : filterlist) {
// filter by title
if (dataNames.getCustomer().toLowerCase().trim().contains(searchText)) {
filteredList.add(dataNames);
}
}
results.count = filteredList.size();
results.values = filteredList;
return results;
}
};
return filter;
}
}
在Fragment
中:
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
mListadapter.getFilter().filter(s.toString());
}
答案 1 :(得分:0)
1)在绑定中,您正在从dataList对象访问对象,并且getItemCount()还返回dataList的大小。
2)在过滤期间,您正在执行过滤并将已过滤的数据存储在filterlist中,并调用notifyDataSetChanged()。
因此,此处始终显示具有所有数据的dataList,而不显示具有已过滤数据的filterlist。
如果要显示过滤的数据,则应将过滤器列表与recyclerview一起使用,
public ListAdapter(ArrayList<DataNote> data)
{
this.dataList = data;
this.filterlist = new ArrayList(dataList);
}
@Override
public int getItemCount()
{
return filterlist.size();
}
@Override
public void onBindViewHolder(ListAdapter.ViewHolder holder, final int position)
{
holder.textViewBranch.setText(filterlist.get(position).getBranch());
holder.textViewAsset.setText(filterlist.get(position).getAssetcode());
holder.textViewCustomer.setText(filterlist.get(position).getCustomer());
holder.textViewLicense.setText(filterlist.get(position).getLisenceplate());
holder.btnReceive.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
//Toast.makeText(getActivity(), "Item " + position + " is clicked.", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getActivity(),
AssetReceiveActivity.class);
intent.putExtra("name",filterlist.get(position).getCustomer());
intent.putExtra("code",filterlist.get(position).getAssetcode());
intent.putExtra("plat",filterlist.get(position).getLisenceplate());
startActivity(intent);
}
});
}