Android 带过滤器的自定义列表视图不起作用

Android Custom Listview with Filter not working

我有一个自定义列表视图,想用它来过滤记录。请帮忙。我想按姓名过滤学生记录。

我尝试了多种互联网可用选项,但 none 有效。

我的主要activity(这里是学生activity)代码:

String[] arr_uid = new String[50];
String[] arr_name = new String[50];
String[] arr_mobile = new String[50];
// these arrays are populated form database then

 Student_custom_Adapter adapter = new Student_custom_Adapter(StudentActivity.this, arr_uid, arr_name, arr_mobile);

                list = (ListView) findViewById(R.id.lv_student);
                list.setAdapter(adapter);

我的自定义适配器 class :

public class Customer_custom_Adapter extends ArrayAdapter<String>{
    private final Activity context;
    private final String[] uid1;
    private final String[] mobile;
    private final String[] name;
    

    public Student_custom_Adapter(Activity context, String[] uid, String[] name , String[] mobile, ) {
        super(context, R.layout.custom_students, uid);
        // TODO Auto-generated constructor stub

        this.context=context;
        this.uid1= uid;
        this.mobile = mobile;
        this.name= name;
       

    }


    public View getView(int position, View view, ViewGroup parent) {

        String finaldate;
        LayoutInflater inflater=context.getLayoutInflater();
        View rowView=inflater.inflate(R.layout.custom_student, null,true);


        
        TextView tv_uid = (TextView) rowView.findViewById(R.id.tv_cust_uid);
        TextView tv_name = (TextView) rowView.findViewById(R.id.tv_cust_name);
         TextView tv_mobile = (TextView) rowView.findViewById(R.id.tv_cust_mobile) ;
        
       
           tv_uid.setText("ID: "+ uid[position]);
            tv_name.setText("Name: "+name[position]);
            tv_mobile.setText("Mobile : "+mobile[position]);
           
        }

        return rowView;



    };
}

您必须自己对其进行过滤,然后使用过滤后的数组更新适配器中的数组。类似于以下内容:

StudentActivity.java:

...

String[] arr_uid = new String[50];
String[] arr_name = new String[50];
String[] arr_mobile = new String[50];

...

private void filterList(String name) {
    String[] filter_uid = new String[50];
    String[] filter_name = new String[50];
    String[] filter_mobile = new String[50];
    int j = 0;
    for (String s : arr_name) {
        if (s != null && s.contains(name)) {
            filter_uid[j] = arr_uid[j];
            filter_name[j] = s;
            filter_name[j] = arr_mobile[j];
            ++j;
        }
    }
    adapter.updateList(filter_uid, filter_name, filter_mobile);
}

Customer_custom_Adapter.java

/* Do not declare as final */
private String[] uid1;
private String[] mobile;
private String[] name;

...

/* Add this method; call it to update your list */
public void updateList(String[] uid, String[] name, String[] mobile) {
    this.uid1= uid;
    this.mobile = mobile;
    this.name= name;
    notifyDataSetChanged();
}
int j = 0 ;

int temp =0;


for (String s : arr_name) {

    if (s != null && s.contains(name)) {

        filter_uid[j] = arr_uid[temp];

        filter_name[j] = arr_name[temp];

        filter_name[j] = arr_mobile[temp];

        ++j;
    }

温度++;

}