7

我正在为Android制作一个类似电子书的应用程序,我想过滤书名,但每次你在edittext中输入一个单词或句子时,它都会搜索书籍的内容。

有人可以帮我弄这个吗...

4

4 回答 4

15

试试这个,每当你在编辑文本中输入文本时,列表将显示过滤结果,因为我已经在图像中显示了内容

最初的:

最初的

过滤:

过滤:
这是 main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<EditText 
    android:id="@+id/etSearchbox"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    />
<ListView 
    android:id="@+id/lvFirst"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"

    ></ListView>    

</LinearLayout>

这是 FilterListActivity.java

package com.filterlist;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;

public class FilterListActivity extends Activity{

EditText etSearchbox;
ListView lvFirst;
ArrayAdapter<String> adapter1;
String[] data = {"mehul joisar","amit mishra","amitabh","Aamir khan","jesica","katrina"};

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

etSearchbox=(EditText)findViewById(R.id.etSearchbox);
lvFirst=(ListView)findViewById(R.id.lvFirst);
lvFirst.setTextFilterEnabled(true);

adapter1 = new ArrayAdapter<String>(FilterListActivity.this, android.R.layout.simple_list_item_1, data);   
lvFirst.setAdapter(adapter1);

etSearchbox.addTextChangedListener(new TextWatcher() {

    @Override
    public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
        // TODO Auto-generated method stub
        FilterListActivity.this.adapter1.getFilter().filter(arg0);
    }

    @Override
    public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
            int arg3) {
        // TODO Auto-generated method stub

    }

    @Override
    public void afterTextChanged(Editable arg0) {
        // TODO Auto-generated method stub

    }
});




}
}
于 2012-04-27T20:33:46.927 回答
4

我有一个工作示例,试试这个:

filterEditText = (EditText)findViewById(R.id.filter);
filterEditText.addTextChangedListener(filterTextWatcher);

TextWatcher filterTextWatcher = new TextWatcher() {

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

        }  
        public void onTextChanged(CharSequence s, int start, int before,int count) {  
            adapter.getFilter().filter(s);
        }

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub              
        }  
    };

适配器必须implements Filterable

            @Override
            public Filter getFilter() {
//              Filter filter = null;

                if(filter == null)
                    filter = new CheeseFilter();
                return filter;
            }

和过滤器类:

        public class CheeseFilter extends Filter {

            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                // TODO Auto-generated method stub

                constraint = constraint.toString().toLowerCase();

                FilterResults newFilterResults = new FilterResults();

                if (constraint != null && constraint.length() > 0) {


                    ArrayList<String> auxData = new ArrayList<String>();

                    for (int i = 0; i < data.size(); i++) {
                        if (data.get(i).toLowerCase().contains(constraint))
                            auxData.add(data.get(i));
                    }

                    newFilterResults.count = auxData.size();
                    newFilterResults.values = auxData;
                } else {

                    newFilterResults.count = data.size();
                    newFilterResults.values = data;
                }

                return newFilterResults;
            }

            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {

                ArrayList<String> resultData = new ArrayList<String>();

                resultData = (ArrayList<String>) results.values;

                EfficientAdapter adapter = new EfficientAdapter(context, resultData);
                list.setAdapter(adapter);

//              notifyDataSetChanged();
            }

        }

您可以查看此帖子以获取更多信息:

过滤可扩展列表

过滤列表视图

于 2012-04-27T20:53:43.193 回答
1

当您使用自定义 listView

适配器 :

public class Adapter extends ArrayAdapter {
ArrayList<String> list = new ArrayList<>();
ArrayList<String> filteredData = new ArrayList<>();

public Adapter(@NonNull Context context, int resource) {
    super(context, resource);
}

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

    LayoutInflater inflate = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
    @SuppressLint("ViewHolder") View vi = inflate.inflate(R.layout.ly_items, null);
    try {
        JSONObject js = new JSONObject(list.get(position));
        TextView txtItem = vi.findViewById(R.id.txtItem);
        ImageView imgItem = vi.findViewById(R.id.imgItem);
        txtItem.setText(js.getString("name") + " - " + js.getInt("number"));
        Picasso.get().load(js.getString("logo_url")).into(imgItem);

    } catch (JSONException e) {
        e.printStackTrace();
    }

    return vi;
}

@Override
public void add(@Nullable Object object) {
    super.add(object);
    list.add(object.toString());
    filteredData.add(object.toString());
}

@Override
public int getCount() {
    return list.size();
}

@Nullable
@Override
public Object getItem(int position) {
    return list.get(position);
}


public void filter(String charText) {
    charText = charText.toLowerCase(Locale.getDefault());
    list.clear();
    if (charText.length() == 0) {
        list.addAll(filteredData);
    } else {
        for (String wp : filteredData) {

            try {
                JSONObject json = new JSONObject(wp);
                if (json.getString("name").toLowerCase().contains(charText) || json.getString("number").contains(charText)) {
                    list.add(wp);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }
    notifyDataSetChanged();
    }
}

还有你的课:

    Adapter adapter;
ListView list;
EditText edtSearch;

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

list = findViewById(R.id.list);

edtSearch = findViewById(R.id.edtSearch);

 adapter = new Adapter(this, android.R.layout.simple_list_item_1);


list.setAdapter(adapter);

edtSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                adapter.filter(s.toString());
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });

    }
于 2020-02-24T12:56:24.797 回答
0

您需要为edittext 编写textChangedlistener(或)TextWatcher。在侦听器内部,您需要编写搜索逻辑。这是APIdoc。这是关于textwatcher的示例。

于 2012-04-27T18:38:10.193 回答