我在设计自定义数组适配器时遇到了很大的问题,可以通过将文本写入 EditText 来过滤它。常规方法是使用您的自定义 getFilter() 函数覆盖现有的 getFilter()。
我想,我应该与大家分享我的解决方案(这个在对话框中显示并且可以过滤字符串内部(而不是 getFilter(),它只从每个单词的第一个字符开始过滤字符串)):
MainActivity.java
package com.example.filter_list_custom_adapter;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import org.w3c.dom.Text;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private ArrayAdapter<String> arrayAdapter;
final ArrayList<String> names = new ArrayList<>();
final ArrayList<String> filterednames = new ArrayList<>();
final ArrayList<String> filteredsubstring = new ArrayList<>();
final ArrayList<String> substring = new ArrayList<>();
private ListView list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button but = (Button) findViewById(R.id.button);
but.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
final Dialog dialog = new Dialog(MainActivity.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(false);
dialog.setContentView(R.layout.dialog_list);
list = (ListView) dialog.findViewById(R.id.listview);
EditText theFilter = (EditText) dialog.findViewById(R.id.searchfilter);
names.add("Bastian");
names.add("Laura");
names.add("Benjamin");
names.add("Jonas");
names.add("Deborah");
names.add("Tabea");
substring.add("male");
substring.add("female");
substring.add("male");
substring.add("male");
substring.add("female");
substring.add("female");
arrayAdapter = new ArrayAdapter<String>(
MainActivity.this,
R.layout.list_item,
names ) {
@Override
public View getView(final int position, View convertView, ViewGroup parent){
final ViewHolder viewHolder;
View rowView = convertView;
if (rowView == null) {
LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.list_item, parent,false);
viewHolder = new ViewHolder();
viewHolder.ftxt = (TextView) rowView.findViewById(R.id.filteredtextview);
viewHolder.ntxt = (TextView) rowView.findViewById(R.id.normaltextview);
rowView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) rowView.getTag();
}
viewHolder.ftxt.setText(names.get(position));
viewHolder.ntxt.setText(substring.get(position));
return rowView;
}
};
list.setAdapter(arrayAdapter);
theFilter.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
filter(charSequence.toString());
}
@Override
public void afterTextChanged(Editable editable) {
}
});
Button dialogButton = (Button) dialog.findViewById(R.id.close);
dialogButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
});
}
public void filter(String charText) {
charText = charText.toLowerCase();
filterednames.clear();
filteredsubstring.clear();
if (charText.length() == 0) {
filterednames.addAll(names);
filteredsubstring.addAll(substring);
}
else
{
for (int i = 0; i < names.size(); i++)
{
if (names.get(i).toLowerCase().contains(charText))
{
filterednames.add(names.get(i));
filteredsubstring.add(substring.get(i));
}
}
}
arrayAdapter = new ArrayAdapter<String>(
MainActivity.this,
R.layout.list_item,
filterednames ) {
@Override
public View getView(final int position, View convertView, ViewGroup parent){
final ViewHolder viewHolder;
View rowView = convertView;
if (rowView == null) {
LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
rowView = inflater.inflate(R.layout.list_item, parent,false);
viewHolder = new ViewHolder();
viewHolder.ftxt = (TextView) rowView.findViewById(R.id.filteredtextview);
viewHolder.ntxt = (TextView) rowView.findViewById(R.id.normaltextview);
rowView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) rowView.getTag();
}
viewHolder.ftxt.setText(filterednames.get(position));
viewHolder.ntxt.setText(filteredsubstring.get(position));
return rowView;
}
};
list.setAdapter(arrayAdapter);
}
static class ViewHolder {
public TextView ftxt;
public TextView ntxt;
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/button" />
</RelativeLayout>
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:id="@+id/ftxt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:textSize="14dp"
android:textColor="@android:color/black"/>
<TextView
android:id="@+id/ntxt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="12dp"
android:paddingLeft="12dp"
android:textAppearance="?android:attr/textAppearanceSmall"
/>
</LinearLayout>
对话框列表.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="@+id/searchFilter"/>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/searchFilter"
android:id="@+id/listview"/>
</RelativeLayout>
最好的问候, DarkCat