【问题标题】:filter listview by header using custom adapter extend baseadapter [closed]使用自定义适配器扩展 baseadapter 按标题过滤列表视图 [关闭]
【发布时间】:2016-05-24 16:15:02
【问题描述】:

我的列表视图如下所示:

--姓名(String)-- --Money(int)--- ---Years(int)----

我想按名称、金钱或年份过滤此列表视图。 我可以在每个列上添加一个 ImageView,如向上或向下箭头,以调用 onClickListener 来制作列表视图过滤器。

看看下面的图片: enter image description here 根据点击时的图片 Img1 => 按名称过滤,Img2 => 按钱过滤...

这里是适配器

public class Adapter extends BaseAdapter {

private Activity activity;
private LayoutInflater inflater;
private List<Items> items;

public Adapter (Activity activity, List<Items> items){
    this.activity=activity;
    this.items =items;

}

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

@Override
public Object getItem(int position) {
    return items.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.items_row,null);
    }


        TextView name=(TextView)convertView.findViewById(R.id.acname);
        TextView money=(TextView)convertView.findViewById(R.id.ac_money);
        TextView year=(TextView)convertView.findViewById(R.id.ac_year);

    Items item=items.get(position);

        name.setText(item.getName());

        money.setText(item.getMoney());

        year.setText(item.getYear());

    return convertView;
}

}

感谢您的帮助

【问题讨论】:

  • 姓名、金钱和年份如何映射到列表适配器中getView() 中的列表项?什么是底层数据结构(数据库、集合等)?你能发布你的getView() 方法吗?
  • 我编辑帖子 Micha F. 感谢您的帮助
  • 等等,你写的是“过滤器”——我以为你的意思是如何对各个列进行排序,因为这些箭头通常用于:对各个列进行升序或降序排序。我用我的帖子回答了你的问题吗?还是您的意思不同?
  • 你好 Micha F. 请原谅延迟。让我们试着让你回来

标签: android listview filter


【解决方案1】:

在您的 Item 类中,添加以下内部类(比较器):

static class NameComparator implements Comparator<Item> {
    @Override
    public int compare(Item a, Item b) {
        return a.getName().compareTo(b.getName());
    }
}

static class MoneyComparator implements Comparator<Item> {
    @Override
    public int compare(Item a, Item b) {
        return a.getMoney().compareTo(b.getMoney);
    }
}

static class YearComparator implements Comparator<Item> {
    @Override
    public int compare(Item a, Item b) {
        return a.getYear().compareTo(b.getYear);
    }
}

在您的适配器中,您可以添加以下方法:

public void sortByName(boolean descending) {
    Collections.sort(items, new Item.NameComparator());
    if (descending) Collections.reverse(items);
    notifyDataSetChanged();
}

public void sortByMoney(boolean descending) {
    Collections.sort(items, new Item.MoneyComparator());
    if (descending) Collections.reverse(items);
    notifyDataSetChanged();
}

public void sortByYear(boolean descending) {
    Collections.sort(items, new Item.YearComparator());
    if (descending) Collections.reverse(items);
    notifyDataSetChanged();
}

现在您可以在箭头的onClick() 方法中调用这些方法。

【讨论】:

  • 你好 Micha F. 非常感谢。太奇妙了。它的工作很好。非常感谢。
  • 干杯。如果我的回答解决了你的问题,你可以打勾显示,谢谢:)
【解决方案2】:

通过使用 Collections.sort() 方法,然后调用 notifyDataSetChanged()。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多