【问题标题】:Filtering Json Listview with edittext and custom adapter使用edittext和自定义适配器过滤Json Listview
【发布时间】:2018-11-29 10:48:23
【问题描述】:

尝试使用 EditText 过滤我的 Listview,但每次我在 EditText 上输入文本时,我的 listview 都会变为空白,并且不返回任何结果。我尝试了很多方法,这似乎是最容易实现的。任何帮助将不胜感激!

在我的适配器上:

public class AdapterTrilhos extends BaseAdapter {
private final Activity activity;

private LayoutInflater inflater;
private List<Trilhos> triList;
private ArrayList<Trilhos> arraylist=null;


public AdapterTrilhos(List<Trilhos> triList, Activity activity) {
    this.activity = activity;
    this.triList = triList;
    this.arraylist = new ArrayList<Trilhos>();
    this.arraylist.addAll(triList);

}


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

@Override
public Object getItem(int position) {
    return this.triList.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

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


    if (inflater == null)
        inflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if (convertView == null)

            convertView = inflater.inflate(R.layout.dadoslista, null);

            TextView idtxt = (TextView) convertView.findViewById(R.id.txtid);

            TextView tittxt = (TextView) convertView.findViewById(R.id.txttit);

           // TextView usertxt = convertView.findViewById(R.id.txtuser);

            TextView diftxt = (TextView) convertView.findViewById(R.id.txtdif);
   RatingBar ratess= (RatingBar) convertView.findViewById(R.id.ratings);

            Trilhos t = triList.get(position);

            idtxt.setText(String.valueOf(t.getId()));
            tittxt.setText(t.getTitulo());
         //   usertxt.setText(String.valueOf(t.getId_user()));
            diftxt.setText(t.getDificuldade());
            ratess.setRating(t.getMedia());

        return convertView;
    }

public void filter(String charText) {
    charText = charText.toLowerCase(Locale.getDefault());
    triList.clear();
    if (charText.length() == 0) {
        triList.addAll(arraylist);
    } else {
        for (Trilhos cs : arraylist) {
            if (cs.getTitulo().toLowerCase(Locale.getDefault()).contains(charText)) {
                triList.add(cs);
            }
        }
    }
    notifyDataSetChanged();
}

} 在我的活动中

search.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence cs, int start, int before, int count) {
            // TODO Auto-generated method stub
            int textlength = cs.length();

            String text = search.getText().toString().toLowerCase(Locale.getDefault());
            adapter.filter(text);
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                                      int after) {
            // TODO Auto-generated method stub

        }

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

        }
    });

我打算做的是,当我在 editText 上输入文本时,它只会返回带有我输入的单词的结果

【问题讨论】:

  • 附加完整的适配器类。
  • 已更新,我应该发布我的所有其他活动吗?
  • 尝试在过滤方法中记录你的字符串值。
  • 字符串正确到达那里,问题是数据不会更新,只是给出空白列表
  • 你试过这个解决方案Link吗?它为我工作!

标签: android json filter adapter


【解决方案1】:

我认为您应该直接在活动中过滤列表,然后换成新的适配器:

search.addTextChangedListener(new TextWatcher() {

    @Override
    public void onTextChanged(CharSequence cs, int start, int before, int count) {
        int textlength = cs.length();

        String text = search.getText().toString().toLowerCase(Locale.getDefault());

        List<Trilhos> filteredList = yourFilterMethod(oldList, text); 

        mListView.setAdapter(new AdapterTrilhos(filteredList, this));

    }
});

private List<Trilhos> yourFilterMethod(List<object> list, String text)
{
    // some stuff that filter your list using text and returns the filtered one

    return filteredList;
}

【讨论】:

  • 我应该在我的适配器上保留过滤器吗?
  • @Sapo121 哪些错误?不,您不应该这样做,因为您现在要过滤活动中的列表,将其移动到活动中并使其返回一个新列表,而不是直接在适配器上修改它
  • 所以基本上,我需要创建另一个适配器,过滤我的列表并将数据添加到我创建的新适配器中?
  • @Sapo121 我编辑了我的答案以使用您的班级名称。您应该拥有一个负责显示数据的适配器。然后在您的活动中返回过滤列表的过滤器方法,然后您将其用于您设置为 listView 的新 AdapterTrilhos
【解决方案2】:

试试这个?

search.addTextChangedListener(new TextWatcher() {

    @Override
    public void onTextChanged(CharSequence cs, int start, int before, int count) {
        // TODO Auto-generated method stub
        int textlength = cs.length();

        String text = cs.toLowerCase(Locale.getDefault());
        adapter.filter(text);
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count,
                                  int after) {
        // TODO Auto-generated method stub

    }

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

    }
});

【讨论】:

  • 相同的结果,空白列表。
  • 伙计,我正在使用相同的代码,它对我来说非常完美。需要调试一下你就快到了
  • 是的,我知道我快到了,一定是加载或重新加载列表时我犯了一些错误
  • 你的过滤器类有什么?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-12
  • 2020-12-06
  • 1970-01-01
  • 1970-01-01
  • 2014-09-06
相关资源
最近更新 更多