简体   繁体   English

如何在不创建新适配器等的情况下更新ListView中的ListItem

[英]How to update ListItems in ListView without creating a new Adapter etc

I get my data information (JSON-based) through Volley, then map it with GSON to my DataModel. 我通过Volley获取数据信息(基于JSON),然后将其与GSON映射到我的DataModel。 The JSON eg contains Arrays which contains others Arrays. JSON例如包含包含其他数组的数组。 I will present the result of one Array and if i click on a List Item, the Items should change in reference to the next level Array. 我将展示一个数组的结果,如果我单击一个列表项,则该项应根据下一级数组进行更改。

I am searching for an generic solution to click through various ListViews without create a new Adapter etc. 我正在寻找一种通用解决方案,以单击各种ListView而不创建新的Adapter等。

array1:
   - item
   - item
   - item
   - array2:
      - item
      - item
      - item
      - array3
        - item
        - item
        - item
Just add a new item iten in adapter object and call adapter.notifyDatasetChanged()

You could implement a Filter . 您可以实现Filter Have your adapter implement Filterable, ie a method called getFilter() which returns an instance of your filter. 让适配器实现Filterable,即称为getFilter()的方法,该方法返回过滤器的实例。 The filter itself implements two methods: performFiltering (CharSequence constraint) and publishResults(CharSequence constraint, FilterResults results). 过滤器本身实现两种方法:performFiltering(CharSequence约束)和publishResults(CharSequence约束,FilterResults结果)。

In your onItemClick method you would then call adapter.getFilter.filter(contraint) where constraint is a CharSequence differentiating between your array levels. 然后,在onItemClick方法中,您将调用adapter.getFilter.filter(contraint),其中Constraint是区分数组级别的CharSequence。 In your performFilter method you would put the values of the new array into results.values and the length of the new array into results.count. 在您的performFilter方法中,您可以将新数组的值放入results.values中,并将新数组的长度放入results.count中。

And finally the call to adapter.notifyDatasetChanged() would go into your publishResults method. 最后,对adapter.notifyDatasetChanged()的调用将进入您的publishResults方法。

OnClick of ListItem you need to add items into the same ArrayList you are using for displaying over ListView 您需要将ListItem OnClick添加到用于在ListView上显示的同一ArrayList中。

list.add(model(id, name));

and then 接着

 if(listView.getAdapter()==null){
      adapter=new Adapter(getActivity(),list);
      listView.setAdapter(horizontalListAdapter);
  }
  else{
       adapter.notifyDataSetChanged();

  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM