我的java文件是:这个文件包含Listview中的搜索和排序,但是在使用搜索栏后排序不起作用,我发现的问题是代码:adapter.getFilter()。filter(charSequence)当我删除它时,该应用程序工作正常,但未完成过滤。所以问题在于那条线。有没有办法在listview中进行搜索和排序?
public class MainActivity extends AppCompatActivity {
public List<String> galaxies=new ArrayList<>();
boolean ascending = true;
/*
- When activity is created.
*/
private Button sortBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
final ListView lv= (ListView) findViewById(R.id.mListView);
//REFERENCE MATERIALSEARCHBAR AND LISTVIEW
MaterialSearchBar searchBar = (MaterialSearchBar)
findViewById(R.id.searchBar);
searchBar.setHint("Search..");
galaxies.add("Cartwheel");
galaxies.add("Whirlpool");
galaxies.add("Andromeda I");
galaxies.add("Andromeda II");
galaxies.add("Sombrero");
galaxies.add("Messier 81");
galaxies.add("Messier 87");
galaxies.add("Canis Majos Over-density");
galaxies.add("Messier 92");
galaxies.add("Black Eye");
galaxies.add("Centaurus A");
galaxies.add("Centaurus B");
galaxies.add("Milky Way");
galaxies.add("IC 1011");
galaxies.add("Star Bust");
galaxies.add("Hoag's Object");
galaxies.add("Pinwheel");
galaxies.add("Triangulum");
galaxies.add("Cosmos Redshift");
galaxies.add("Ursa Minor");
galaxies.add("Virgo Stellar-Stream");
//ADAPTER
final ArrayAdapter adapter=new
ArrayAdapter(this,android.R.layout.simple_list_item_1,galaxies);
lv.setAdapter(adapter);
//SORT
sortBtn = (Button) findViewById(R.id.sortBtn);
adapter.notifyDataSetChanged();
lv.setTextFilterEnabled(true);
sortBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (ascending)
{
Collections.sort(galaxies);
Toast.makeText(getApplicationContext(),galaxies.get(0),
Toast.LENGTH_SHORT).show();
}
else
{
Collections.reverse(galaxies);
Toast.makeText(getApplicationContext(),galaxies.get(0),
Toast.LENGTH_SHORT).show();
}
lv.setAdapter(adapter);
ascending = !ascending;
}
});
//SEARCHBAR TEXT CHANGE LISTENER
searchBar.addTextChangeListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
//SEARCH FILTER
adapter.getFilter().filter(charSequence);
}
@Override
public void afterTextChanged(Editable editable) {
}
});
//LISTVIEW ITEM CLICKED
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(MainActivity.this, adapter.getItem(i).toString(), Toast.LENGTH_SHORT).show();
}
});
}
}
我的内容xml是:
<?xml version="1.0" encoding="utf-8"?>
<!--
- ActivityMain.xml.
- This is a template layout for our MainActivity.
- Root layout tag is CoordinatorLayout from design support library.
- CordinatorLayout is viewgroup that is a superpowered on framelayout.
- CoordinatorLayout is intended for two primary use cases:
As a top-level application decor or chrome layout
As a container for a specific interaction with one or more child views
- Inside our CordinatorLayout we add : AppBarLayout,FloatingActionButton and include content_main.xml.
- AppBarLayout is vertical LinearLayout that implements scrolling features of material design concept.
- It should be a direct child of CordinatorLayout, otherwise alot of features won't work.
- Inside the AppBarLayout we add our toolbar,which we give a blue color.
- Next we add our Material SearchBar which will give us the searchbar.
- We can specify attributes like style and hint.
- We will add our widgets in our content_main.xml, not here as this is a template layout.
- Finally we have a FloatingActionButton, a class that derives from android.support.design.widget.VisibilityAwareImageButton.
Its the round button you see in our user interface.
-->
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.tutorials.hp.searchbarlistview.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
<com.mancj.materialsearchbar.MaterialSearchBar
app:mt_hint="Custom hint"
app:theme="@style/AppTheme.PopupOverlay"
app:mt_maxSuggestionsCount="5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/searchBar" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_main" />
<Button
android:id="@+id/sortBtn"
android:layout_width="100dp"
android:layout_height="40dp"
android:layout_centerInParent="true"
android:layout_gravity="bottom|end|center_horizontal"
android:background="@color/colorAccent"
android:text="Sort"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="16dp" />
</android.support.design.widget.CoordinatorLayout>
问题是,当我使用排序按钮时,它工作正常,但只在开始时。当我使用搜索栏并使用排序选项时,它不起作用
答案 0 :(得分:0)
一旦你点击过滤器,阵列适配器会维护你的值的单独副本,你对自己的星系列表副本所做的任何更改都不会影响你的列表,请尝试使用此代码对值进行排序
adapter.sort(list, new Comparator<String>() {
@Override
public int compare(String s1, String s2) {
return s1.compareTo(s2);
}
});
这将对您的适配器维护的副本进行排序
答案 1 :(得分:0)
您可以使自定义适配器扩展BaseAdapter并将列表传递到自定义适配器的构造函数中。 您可以在最后维护两个不同的列表副本。 第一个将是原始列表,第二个将是您传递给适配器构造函数的那个。
以下链接会给您一个想法: Custom Adapter for List View