Java ListView搜索后排序。安卓工作室
我制作了一个带有搜索和排序的自定义列表视图。“排序”按钮在我搜索之前工作正常,但如果我搜索然后尝试排序,它将不再工作。即使我删除了搜索字段中的所有字符,排序也不再起作用。如何在搜索时进行排序,并且列表因搜索而更改 我的活动:Java ListView搜索后排序。安卓工作室,java,android,sorting,listview,search,Java,Android,Sorting,Listview,Search,我制作了一个带有搜索和排序的自定义列表视图。“排序”按钮在我搜索之前工作正常,但如果我搜索然后尝试排序,它将不再工作。即使我删除了搜索字段中的所有字符,排序也不再起作用。如何在搜索时进行排序,并且列表因搜索而更改 我的活动: package com.example.lihtuaniancardgame; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.text.
package com.example.lihtuaniancardgame;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class HighscoreActivity extends AppCompatActivity {
ListView listViewHighscore;
Button buttonSort;
EditText EditTextSearch;
ArrayList<IconUserHighscore> usersList = new ArrayList<>();
userListAdapter adapter;
boolean sorted = true;
//userListAdapter ada = new userListAdapter();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_highscore);
EditTextSearch = findViewById(R.id.editTextHighscoreSearch);
buttonSort = findViewById(R.id.buttonSort);
listViewHighscore = findViewById(R.id.ListViewHighscore);
//TEST DATA
usersList = FormUsersTestData();
adapter = new userListAdapter(this, R.layout.adapter_view_layout, usersList);
listViewHighscore.setTextFilterEnabled(true);
listViewHighscore.setAdapter(adapter);
EditTextSearch.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
HighscoreActivity.this.adapter.getFilter().filter(s);
adapter.notifyDataSetChanged();
}
@Override
public void afterTextChanged(Editable s) {
}
});
buttonSort.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (sorted) {
sortArrayListDESC();
} else {
sortArrayListASC();
}
sorted = !sorted;
adapter.notifyDataSetChanged();
}
});
}
private void sortArrayListDESC() {
adapter.sort(new Comparator<IconUserHighscore>() {
@Override
public int compare(IconUserHighscore o1, IconUserHighscore o2) {
return o1.getNickname().compareTo(o2.getNickname());
}
});
}
private void sortArrayListASC() {
adapter.sort(new Comparator<IconUserHighscore>() {
@Override
public int compare(IconUserHighscore o1, IconUserHighscore o2) {
return o2.getNickname().compareTo(o1.getNickname());
}
});
}
public ArrayList<IconUserHighscore> FormUsersTestData() {
IconUserHighscore bart = new IconUserHighscore(R.drawable.bart, "Bart", 10);
IconUserHighscore flanders = new IconUserHighscore(R.drawable.flanders, "Flanders", 0);
IconUserHighscore homer = new IconUserHighscore(R.drawable.homer, "Homer", 8);
IconUserHighscore krusty = new IconUserHighscore(R.drawable.krusty, "Krusty", 5);
IconUserHighscore liza = new IconUserHighscore(R.drawable.liza, "Liza", 9);
IconUserHighscore maggie = new IconUserHighscore(R.drawable.maggie, "Maggie", 7);
IconUserHighscore marge = new IconUserHighscore(R.drawable.marge, "Marge", 6);
IconUserHighscore millhouse = new IconUserHighscore(R.drawable.millhouse, "Millhouse", 1);
IconUserHighscore otto = new IconUserHighscore(R.drawable.otto, "Otto", 3);
IconUserHighscore santasLittleHelper = new IconUserHighscore(R.drawable.santaslittlehelper, "Santa's Little Helper", 2);
IconUserHighscore snowball = new IconUserHighscore(R.drawable.snowball, "Snowball", 4);
ArrayList<IconUserHighscore> usersList1 = new ArrayList<>();
usersList1.add(bart);
usersList1.add(flanders);
usersList1.add(homer);
usersList1.add(krusty);
usersList1.add(liza);
usersList1.add(maggie);
usersList1.add(marge);
usersList1.add(millhouse);
usersList1.add(otto);
usersList1.add(santasLittleHelper);
usersList1.add(snowball);
usersList1.add(bart);
usersList1.add(flanders);
usersList1.add(homer);
usersList1.add(krusty);
usersList1.add(liza);
usersList1.add(maggie);
usersList1.add(marge);
usersList1.add(millhouse);
usersList1.add(otto);
usersList1.add(santasLittleHelper);
usersList1.add(snowball);
return usersList1;
}
}
列表适配器:
package com.example.lihtuaniancardgame;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.util.ArrayList;
import java.util.Locale;
public class userListAdapter extends ArrayAdapter<IconUserHighscore> {
private static final String TAG = "PersonListAdapter";
private Context mContext;
int mResource;
public userListAdapter(@NonNull Context context, int resource, @NonNull ArrayList<IconUserHighscore> objects) {
super(context, resource, objects);
mContext = context;
mResource = resource;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
int img = getItem(position).getImg();
String nickname = getItem(position).getNickname();
int score = getItem(position).getScore();
IconUserHighscore userHighscore = new IconUserHighscore(img, nickname, score);
LayoutInflater inflater = LayoutInflater.from(mContext);
convertView = inflater.inflate(mResource, parent, false);
ImageView imgIcon = (ImageView) convertView.findViewById(R.id.imageViewIcon);
TextView tvNickname = (TextView) convertView.findViewById(R.id.textViewUsername);
TextView tvScore = (TextView) convertView.findViewById(R.id.textViewScore);
imgIcon.setImageResource(img);
tvNickname.setText(nickname);
tvScore.setText(String.valueOf(score));
return convertView;
}
public void filter(String charText){
charText = charText.toLowerCase(Locale.getDefault());
}
}
package com.example.lihtuaniancardgame;
导入android.content.Context;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.ArrayAdapter;
导入android.widget.ImageView;
导入android.widget.TextView;
导入androidx.annotation.NonNull;
导入androidx.annotation.Nullable;
导入java.util.ArrayList;
导入java.util.Locale;
公共类userListAdapter扩展了ArrayAdapter{
私有静态最终字符串标记=“PersonListAdapter”;
私有上下文;
国际资源;
公共userListAdapter(@NonNull上下文上下文,int资源,@NonNull ArrayList对象){
超级(上下文、资源、对象);
mContext=上下文;
mResource=资源;
}
@非空
@凌驾
公共视图getView(int位置,@Nullable视图convertView,@NonNull视图组父级){
int img=getItem(position).getImg();
字符串昵称=getItem(位置).Get昵称();
int score=getItem(position).getScore();
IconUserHighscore=新IconUserHighscore(img、昵称、分数);
LayoutFlater充气机=LayoutFlater.from(mContext);
convertView=充气机。充气(mResource,父项,false);
ImageView imgIcon=(ImageView)convertView.findViewById(R.id.imageViewIcon);
TextView TV昵称=(TextView)convertView.findViewById(R.id.textViewUsername);
TextView tvScore=(TextView)convertView.findViewById(R.id.textViewScore);
imgIcon.setImageResource(img);
tv昵称.setText(昵称);
tvScore.setText(String.valueOf(score));
返回视图;
}
公共空过滤器(字符串图表文本){
charText=charText.toLowerCase(Locale.getDefault());
}
}
活动xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".HighscoreActivity"
android:orientation="vertical"
android:gravity="center"
android:padding="16dp">
<TextView
android:id="@+id/textViewHighscore"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignWithParentIfMissing="true"
android:gravity="center"
android:text="@string/highscore"
android:textColor="@android:color/holo_green_dark"
android:textSize="32sp" />
<EditText
android:id="@+id/editTextHighscoreSearch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:hint="Search"
android:gravity="center"
android:layout_below="@id/textViewHighscore"
/>
<ListView
android:id="@+id/ListViewHighscore"
android:layout_width="match_parent"
android:layout_above="@id/buttonSort"
android:layout_below="@id/editTextHighscoreSearch"
android:layout_height="match_parent"/>
<Button
android:id="@+id/buttonSort"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="@string/sort_by_nickname" />
</RelativeLayout>
列表项xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".HighscoreActivity"
android:orientation="vertical"
android:gravity="center"
android:padding="16dp">
<TextView
android:id="@+id/textViewHighscore"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignWithParentIfMissing="true"
android:gravity="center"
android:text="@string/highscore"
android:textColor="@android:color/holo_green_dark"
android:textSize="32sp" />
<EditText
android:id="@+id/editTextHighscoreSearch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:hint="Search"
android:gravity="center"
android:layout_below="@id/textViewHighscore"
/>
<ListView
android:id="@+id/ListViewHighscore"
android:layout_width="match_parent"
android:layout_above="@id/buttonSort"
android:layout_below="@id/editTextHighscoreSearch"
android:layout_height="match_parent"/>
<Button
android:id="@+id/buttonSort"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="@string/sort_by_nickname" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".HighscoreActivity"
android:orientation="vertical"
android:gravity="center"
android:padding="16dp">
<TextView
android:id="@+id/textViewHighscore"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignWithParentIfMissing="true"
android:gravity="center"
android:text="@string/highscore"
android:textColor="@android:color/holo_green_dark"
android:textSize="32sp" />
<EditText
android:id="@+id/editTextHighscoreSearch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:hint="Search"
android:gravity="center"
android:layout_below="@id/textViewHighscore"
/>
<ListView
android:id="@+id/ListViewHighscore"
android:layout_width="match_parent"
android:layout_above="@id/buttonSort"
android:layout_below="@id/editTextHighscoreSearch"
android:layout_height="match_parent"/>
<Button
android:id="@+id/buttonSort"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="@string/sort_by_nickname" />
</RelativeLayout>