我是Android的初学者,但我尝试进行自定义列表视图过滤,我以某种方式工作。我唯一的问题是我保留所有值(“原始”ArrayList)的ArrayList在每个过滤中的项目上越来越低。我无法解释这一点,但我认为你可以以某种方式帮助我。
无论如何这里是Custom ArrayAdaptor:
public class PkmnAdapter extends ArrayAdapter<Pkmn> {
private ArrayList<Pkmn> original;
private ArrayList<Pkmn> fitems;
private Filter filter;
public PkmnAdapter(Context context, int textViewResourceId, ArrayList<Pkmn> items) {
super(context, textViewResourceId, items);
this.original = items;//new ArrayList<Pkmn>();
this.fitems = items;//new ArrayList<Pkmn>();
}
@Override
public void add(Pkmn item){
original.add(item);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.row, null);
}
Pkmn pkmn = original.get(position);
if (pkmn != null) {
TextView tt = (TextView) v.findViewById(R.id.RlabPName);
TextView dex = (TextView)v.findViewById(R.id.RlabDex);
ImageView img = (ImageView)v.findViewById(R.id.RimgPkmn);
if (tt != null) { tt.setText(pkmn.getName()); }
if (dex != null){ dex.setText(CalcDex(pkmn.getId())); }
if (img != null){
int resId = getContext().getResources().getIdentifier("dex" + pkmn.getId(), "drawable", "com.compileguy.pokebwteam");
img.setImageResource(resId);
}
}
return v;
}
@Override
public Filter getFilter()
{
if (filter == null)
filter = new PkmnNameFilter();
return filter;
}
private class PkmnNameFilter extends Filter
{
@Override
protected FilterResults performFiltering(CharSequence constraint)
{
FilterResults results = new FilterResults();
String prefix = constraint.toString().toLowerCase();
if (prefix == null || prefix.length() == 0)
{
ArrayList<Pkmn> list = new ArrayList<Pkmn>(original);
results.values = list;
results.count = list.size();
}
else
{
final ArrayList<Pkmn> list = original;
int count = list.size();
final ArrayList<Pkmn> nlist = new ArrayList<Pkmn>(count);
for (int i=0; i<count; i++)
{
final Pkmn pkmn = list.get(i);
final String value = pkmn.getName().toLowerCase();
if (value.startsWith(prefix))
{
nlist.add(pkmn);
}
}
results.values = nlist;
results.count = nlist.size();
}
return results;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
fitems = (ArrayList<Pkmn>)results.values;
clear();
int count = fitems.size();
for (int i=0; i<count; i++)
{
Pkmn pkmn = (Pkmn)fitems.get(i);
add(pkmn);
}
if (fitems.size() > 0)
notifyDataSetChanged();
else
notifyDataSetInvalidated();
}
}
private String CalcDex(int id){
String s = String.valueOf(id);
if (s.length() == 1)
s = "00"+s;
else if (s.length() == 2)
s = "0"+s;
return '#'+s;
}
}
注意:列表视图正确显示了这些项目,但是当我在exaple中删除编辑框中的一个字母(触发过滤)时,就会出现问题。
---编辑---
@Janusz:非常感谢你的回答。这解决了我的问题。
以下是适用于我的源代码,因此如果有人遇到同样的问题,可以试试这个:
private ArrayList<Pkmn> original;
private ArrayList<Pkmn> fitems;
private Filter filter;
public PkmnAdapter(Context context, int textViewResourceId, ArrayList<Pkmn> items) {
super(context, textViewResourceId, items);
this.original = new ArrayList<Pkmn>(items);
this.fitems = new ArrayList<Pkmn>(items);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.row, null);
}
Pkmn pkmn = fitems.get(position);
if (pkmn != null) {
TextView tt = (TextView) v.findViewById(R.id.RlabPName);
TextView dex = (TextView)v.findViewById(R.id.RlabDex);
ImageView img = (ImageView)v.findViewById(R.id.RimgPkmn);
if (tt != null) { tt.setText(pkmn.getName()); }
if (dex != null){ dex.setText(CalcDex(pkmn.getId())); }
if (img != null){
int resId = getContext().getResources().getIdentifier("dex" + pkmn.getId(), "drawable", "com.compileguy.pokebwteam");
img.setImageResource(resId);
}
}
return v;
}
@Override
public Filter getFilter()
{
if (filter == null)
filter = new PkmnNameFilter();
return filter;
}
private class PkmnNameFilter extends Filter
{
@Override
protected FilterResults performFiltering(CharSequence constraint)
{
FilterResults results = new FilterResults();
String prefix = constraint.toString().toLowerCase();
if (prefix == null || prefix.length() == 0)
{
ArrayList<Pkmn> list = new ArrayList<Pkmn>(original);
results.values = list;
results.count = list.size();
}
else
{
final ArrayList<Pkmn> list = new ArrayList<Pkmn>(original);
final ArrayList<Pkmn> nlist = new ArrayList<Pkmn>();
int count = list.size();
for (int i=0; i<count; i++)
{
final Pkmn pkmn = list.get(i);
final String value = pkmn.getName().toLowerCase();
if (value.startsWith(prefix))
{
nlist.add(pkmn);
}
}
results.values = nlist;
results.count = nlist.size();
}
return results;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
fitems = (ArrayList<Pkmn>)results.values;
clear();
int count = fitems.size();
for (int i=0; i<count; i++)
{
Pkmn pkmn = (Pkmn)fitems.get(i);
add(pkmn);
}
}
}
}
答案 0 :(得分:17)
你的问题是这样的:
this.original = items;
this.fitems = items;
项目是您用于ListView的列表,并将其放在两个不同的变量中,不会产生两个不同的列表。您只给列表项两个不同的名称。
您可以使用:
this.fitems = new ArrayList(items);
应该生成一个新列表,并且此列表中的更改只会更改fitems列表。
答案 1 :(得分:2)
只需在Pkmn类上创建一个返回要过滤的值的toString()
方法,即可实现相同的效果。
答案 2 :(得分:0)
我发现过滤ArrayAdapter的最好方法是创建我自己的过滤器类:
private class MyFilter extends Filter
然后在该函数中创建要在过滤器后显示的新对象数组(您可以在class ArrayAdapter
的源代码中找到良好的实现)
@Override
protected FilterResults performFiltering(CharSequence prefix)
现在诀窍在于这个方法
@Override
protected void publishResults(CharSequence constraint, FilterResults results)
当您使用阵列适配器时,无法执行此操作:
myAdapterData = results.values
从那时起你将数据与超级数据断开连接,你必须这样做以保持对超级原始数据数组的引用:
data.clear();
data.addAll((List<YourType>) results.values);
然后覆盖
适配器中的用getFilter()
,例如:
@Override
public Filter getFilter() {
if (filter == null) {
filter = new MyFilter();
}
return filter;
}
答案 3 :(得分:0)
它们在这里包含: https://www.mysamplecode.com/2012/07/android-listview-custom-layout-filter.html
您将需要在arrrat适配器上调用clear()以不击中indexBoundException和每个元素上的add(),因为addAll()仅在蜂窝之后才有效。
@SuppressWarnings(“未选中”) @Override 受保护的void publishResults(CharSequence约束, FilterResults结果){
var arr = ['a','b','c']
/*
function addArr(ar){
ar.push('d')
return ar
}
console.log(addArr(arr)) // ['a', 'b', 'c', 'd']
console.log(arr) // ['a', 'b', 'c', 'd']
*/
//the above output is expected behavior for an Array object
function changeArr(ar){
console.log(ar) //1-// ['a', 'b', 'c']
ar = ['12','11']
console.log(ar) //2-// ['12', '11']
return ar
}
console.log(changeArr(arr)) //3-// ['12', '11']
console.log(arr) //4-// ['a', 'b', 'c']
//now I expect the forth console output to be ['12','11'] here because it is an object
}