无法在自定义列表视图上实现搜索功能

Unable to implement search feature on a custom list view

我正在制作自定义列表视图,我在列表视图顶部编辑文本以搜索列表视图中的项目,但是当我在编辑文本中输入任何值时,它会在列表视图中搜索,但当我清除编辑时测试它不显示原始列表视图
我的主要 activity 代码是

package com.example.searchonlist;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.ListView;

public class MainActivity extends Activity implements TextWatcher  {

 private FriendListAdapter friendListAdapter;
 private EditText inputSearch;
 private ListView friendList;
 private ArrayList<String> frndArrList;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  inputSearch = (EditText) findViewById(R.id.inputSearch);
  inputSearch.addTextChangedListener(this);

  frndArrList = new ArrayList<String>();
  frndArrList.add("Ashutosh");
  frndArrList.add("ShreeVridhee");
  frndArrList.add("Tanishi");
  frndArrList.add("Divyanu");
  frndArrList.add("Adarsh");
  frndArrList.add("Akshara");
  frndArrList.add("Pooja");
  frndArrList.add("NoOne");

  friendList = (ListView) findViewById(R.id.listView1);
  friendListAdapter = new FriendListAdapter(this, frndArrList);
  friendList.setAdapter(friendListAdapter);
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  getMenuInflater().inflate(R.menu.main, menu);
  return true;
 }

 @Override
 public boolean onOptionsItemSelected(MenuItem item) {

  int id = item.getItemId();
  if (id == R.id.action_settings) {
   return true;
  }
  return super.onOptionsItemSelected(item);
 }

 @Override
 public void beforeTextChanged(CharSequence s, int start, int count,
   int after) {

 }

 @Override
 public void onTextChanged(CharSequence s, int start, int before, int count) {

  Log.e("EditText", s.toString());

  friendListAdapter.getFilter().filter(s.toString());

 }

 @Override
 public void afterTextChanged(Editable s) {

 }
 
 @Override
 public void onBackPressed() {
  super.onBackPressed();
    }

}

我的列表适配器是

package com.example.searchonlist;

import java.util.ArrayList;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;

public class FriendListAdapter extends BaseAdapter implements Filterable {
 MainActivity context;
 ArrayList<String> frndArrList;
 ArrayList<String> filterArrList;
 private FriendFilter friendFilter;

 public FriendListAdapter(MainActivity context, ArrayList<String> frndArrList) {
  this.context = context;
  this.frndArrList = frndArrList;
  this.filterArrList = frndArrList;
 }

 @Override
 public int getCount() {

  return frndArrList.size();
 }

 @Override
 public Object getItem(int position) {

  return position;
 }

 @Override
 public long getItemId(int position) {

  return position;
 }

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

  LayoutInflater layoutInflater = LayoutInflater.from(context);
  view = layoutInflater.inflate(R.layout.inflate_view, parent, false);
  TextView txtView = (TextView) view.findViewById(R.id.inflate_txtView);
  txtView.setText(frndArrList.get(position));

  return view;
 }

 @Override
 public Filter getFilter() {
  if (friendFilter == null)
   friendFilter = new FriendFilter();

  return friendFilter;

 }

 private class FriendFilter extends Filter {

  @Override
  protected FilterResults performFiltering(CharSequence constraint) {
     FilterResults results = new FilterResults();
     
     if (constraint == null || constraint.length() == 0) {
           // No filter implemented we return all the list
      
           results.values = frndArrList;
           results.count = frndArrList.size();
       }
   
     else {
           // We perform filtering operation
           ArrayList<String> nPlanetList = new ArrayList<String>();
           for (String p : frndArrList) {
            Log.v("String", p);
               if (p.toLowerCase().contains(constraint.toString().toLowerCase()))
                   nPlanetList.add(p);
               Log.e("nPlanetList", nPlanetList.toString());
           }
            
           results.values = nPlanetList;
           results.count = nPlanetList.size();
    
       }
   return results;
   
  }

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

  }

 }

在构造函数中不要将引用分配给两个列表。

    public FriendListAdapter(MainActivity context, ArrayList<String> frndArrList) {
        this.context = context;
        this.frndArrList = frndArrList;
        this.filterArrList.addAll(frndArrList);
    }

您必须为适配器中的搜索操作创建两个不同的数组列表。 一个是原始的,另一个是临时的。 确保不要创建具有相同引用的列表。 您必须使用此临时列表来过滤税并使用原始数组列表进行比较操作。

这是因为您的 publishResults 方法。您将 frndArrList 指定为搜索结果,这意味着在您输入搜索后,您实际上删除了对原始数组列表中数据的所有引用。要解决此问题,您需要更改以下内容:

public int getCount() {
    return filterArrList.size()
}

//...

public View getView(int position, View view, ViewGroup parent) {
    LayoutInflater layoutInflater = LayoutInflater.from(context);
    view = layoutInflater.inflate(R.layout.inflate_view, parent, false);
    TextView txtView = (TextView) view.findViewById(R.id.inflate_txtView);
    //txtView.setText(frndArrList.get(position));
    txtView.setText(filterArrList.get(position));

    return view;
}

//...

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

您可以使用 on textchanged 侦听器来过滤有关 editext 的文本更改调用的新列表。

edit_searchbox.addTextChangedListener(new TextWatcher() {

        public void onTextChanged(CharSequence s, int start,
                int before, int count) {
            // get the text in the EditText
            String searchString = edit_searchbox.getText().toString();
            int textLength = searchString.length();

            // clear the initial data set
            searchResults.clear();

            String playerName="";
            for (int i = 0; i < aa.size(); i++) {
                if(isContent){
                 playerName = aa.get(i).getComment().toString();}
                else{
                     playerName = aa.get(i).getTitle_txt().toString();}

                if (textLength <= playerName.length()) {
                    // compare the String in EditText with Names in the
                    //aa--- ArrayList
                    if (searchString.equalsIgnoreCase(playerName
                            .substring(0, textLength)))
                        searchResults.add(aa.get(i));
                }
            }
            adapter = new NotepadAdapter(MemoActivity.this,
                    R.layout.listitem, searchResults);
            list.setAdapter(adapter);
            adapter.notifyDataSetChanged();
        }

        public void beforeTextChanged(CharSequence s, int start,
                int count, int after) {

        }

        public void afterTextChanged(Editable s) {

        }
    });