我正在尝试过滤我的歌曲信息的arraylist,我从内部存储中检索数据。我能够在我的适配器中显示过滤器数据,但我如何使用过滤器方法从arraylist中过滤。我尝试了一些发帖,但现在真的很老了,而不是我的情况。I want to pass the filtered data but the arraylist is not getting filtered ,But i am able to show the filtered data in adapter.
这是我试过的。
private CustomAdapter customAdapter;
private ArrayList<Songfileinfo> newlist;
private ArrayList<Songfileinfo> songarr = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
checkReadStoragePermission();
Songdata load = new Songdata();
this.songarr = load.getPlayList(this);
listView = (ListView) findViewById(R.id.list);
editText = (EditText) findViewById(R.id.editText);
customAdapter = new CustomAdapter(this, getPlaylist());
listView.setAdapter(customAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Log.e("size",""+newlist.size());
}
});
editText.addTextChangedListener(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) {
customAdapter.getFilter().filter(charSequence.toString());
customAdapter.notifyDataSetChanged();
}
@Override
public void afterTextChanged(Editable editable) {
}
});
}
private ArrayList<Songfileinfo> getPlaylist() {
newlist = new ArrayList<Songfileinfo>();
Songfileinfo songfileinfo;
for (int i = 0; i < songarr.size(); i++) {
Log.v("SiZE OF IMG ARR", "" + songarr.get(i).getAlbum_art());
songfileinfo = new Songfileinfo(songarr.get(i).getTitle(), songarr.get(i).getAlbum(),
songarr.get(i).getAlbum_art(), songarr.get(i).getArtist(), songarr.get(i).getFile_uri(), songarr.get(i).getDuration());
newlist.add(songfileinfo);
}
return newlist;
}
我的自定义适配器
public class CustomAdapter extends BaseAdapter implements Filterable {
private ArrayList<Songfileinfo> display;
private ArrayList<Songfileinfo> searcharr;
private final Typeface typeface;
CustomFilter customFilter;
Context context;
public CustomAdapter(Context context, ArrayList<Songfileinfo> songarr) {
this.context = context;
this.display = songarr;
this.searcharr = songarr;
typeface = Typeface.createFromAsset(context.getAssets(), "font.ttf");
}
@Override
public int getCount() {
return display.size();
}
@Override
public Object getItem(int pos) {
return display.get(pos);
}
@Override
public long getItemId(int pos) {
return display.indexOf(getItem(pos));
}
@Override
public View getView(int pos, View convertView, ViewGroup parent) {
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.custom_songlist, null);
}
TextView textView = (TextView) convertView.findViewById(R.id.textView);
TextView textView1 = (TextView) convertView.findViewById(R.id.textView2);
TextView textView2 = (TextView) convertView.findViewById(R.id.textView11);
CircleImageView circleImageView = (CircleImageView) convertView.findViewById(R.id.profile_image);
textView.setText(display.get(pos).getTitle());
textView1.setText(display.get(pos).getAlbum());
textView.setTypeface(typeface);
textView1.setTypeface(typeface);
long timer = 0;
timer = Long.parseLong(display.get(pos).getDuration());
textView2.setText("" + milliSecondsToTimer(timer));
textView2.setTypeface(typeface);
Glide.with(context).load(Uri.parse(display.get(pos).getAlbum_art()))
.thumbnail(0.5f)
.fitCenter()
.error(R.drawable.music)
.diskCacheStrategy(DiskCacheStrategy.SOURCE)
.into(circleImageView);
return convertView;
}
public String milliSecondsToTimer(long milliseconds) {
String finalTimerString = "";
String secondsString = "";
// Convert total duration into time
int hours = (int) (milliseconds / (1000 * 60 * 60));
int minutes = (int) (milliseconds % (1000 * 60 * 60)) / (1000 * 60);
int seconds = (int) ((milliseconds % (1000 * 60 * 60)) % (1000 * 60) / 1000);
// Add hours if there
if (hours > 0) {
finalTimerString = hours + ":";
}
// Prepending 0 to seconds if it is one digit
if (seconds < 10) {
secondsString = "0" + seconds;
} else {
secondsString = "" + seconds;
}
finalTimerString = finalTimerString + minutes + ":" + secondsString;
// return timer string
return finalTimerString;
}
@Override
public Filter getFilter() {
if (customFilter == null) {
customFilter = new CustomFilter();
}
return customFilter;
}
class CustomFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence charSequence) {
FilterResults filterResults = new FilterResults();
if (charSequence != null && charSequence.length() > 0) {
charSequence = charSequence.toString().toUpperCase();
ArrayList<Songfileinfo> filter = new ArrayList<>();
for (int i = 0; i < searcharr.size(); i++) {
if (searcharr.get(i).getTitle().toUpperCase().contains(charSequence)) {
Songfileinfo songfileinfo = new Songfileinfo(searcharr.get(i).getTitle(), searcharr.get(i).getAlbum(), searcharr.get(i).getAlbum_art(), searcharr.get(i).getArtist(), searcharr.get(i).getFile_uri(), searcharr.get(i).getDuration());
filter.add(songfileinfo);
}
}
filterResults.count = filter.size();
filterResults.values = filter;
} else {
filterResults.count = searcharr.size();
filterResults.values = searcharr;
}
return filterResults;
}
@Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
display = (ArrayList<Songfileinfo>) filterResults.values;
notifyDataSetChanged();
}
}
}
我试图获得大小和大小保持不变,我知道我必须创建一个新的arraylist但是我如何使用filter或textchangelistener来创建一个新的arraylist.Any提示将有所帮助。