0
當我按下回車鍵時沒有錯誤,但沒有改變,過濾器也沒有工作。它似乎沒事,但我不知道出了什麼問題。請回答,我會很感激的是 ArrayAdapter-在自定義arrayadapter中過濾不顯示任何結果;它不是過濾數據
package com.paradise.android.french.adapter;
import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;
import com.paradise.android.french.R;
import com.paradise.android.french.pojo.word;
import java.util.ArrayList;
import java.util.List;
public class wordAdapter extends ArrayAdapter<word> implements Filterable {
private int ColorResourceid;
private List<word> filterWords;
private Filter valueFilter;
List<word> Words;
@Override
public Filter getFilter() {
if (valueFilter == null) {
valueFilter = new ValueFilter();
}
return valueFilter;
}
public wordAdapter(Context context, ArrayList<word> words, int colorResourceId) {
super(context, 0, words);
ColorResourceid = colorResourceId;
Words = words;
filterWords = words;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View listItemView = convertView;
if (listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.list_item, parent, false);
}
word currentWord = getItem(position);
TextView miwokTextView = (TextView) listItemView.findViewById(R.id.french);
miwokTextView.setText(currentWord.getFrenchWord());
TextView defaultTextView = (TextView) listItemView.findViewById(R.id.english);
defaultTextView.setText(currentWord.getEngWord());
View colorit = listItemView.findViewById(R.id.havecolor);
int color = ContextCompat.getColor(getContext(), ColorResourceid);
colorit.setBackgroundColor(color);
return listItemView;
}
private class ValueFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
if (constraint != null && constraint.length() > 0) {
ArrayList<word> filterList = new ArrayList<word>();
for (int i = 0; i < filterWords.size(); i++) {
if ((filterWords.get(i).getEngWord().toUpperCase())
.contains(constraint.toString().toUpperCase())) {
word Word = new word(filterWords.get(i).getEngWord(), filterWords.get(i).getFrenchWord(), filterWords.get(i).getmediaPlayer());
filterList.add(Word);
}
}
results.count = filterList.size();
results.values = filterList;
}else {
results.count = filterWords.size();
results.values = filterWords;
}
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
Words = (ArrayList<word>) results.values;
notifyDataSetChanged();
}
}
@Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
}
@Override
public int getCount() {
return Words.size();
}
}
片段:
package com.paradise.android.french.activity;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.widget.SearchView;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import com.paradise.android.french.R;
import com.paradise.android.french.pojo.word;
import com.paradise.android.french.adapter.wordAdapter;
import java.util.ArrayList;
public class Accommodation extends Fragment {
public static final String ARG_PAGE = "ARG_PAGE";
private MediaPlayer mMediaPlayer;
private AudioManager mAudioManager;
private wordAdapter adapter;
private AudioManager.OnAudioFocusChangeListener mOnAudioFocusChangeListener = new AudioManager.OnAudioFocusChangeListener() {
@Override
public void onAudioFocusChange(int focusChange) {
if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT ||
focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK) {
mMediaPlayer.pause();
mMediaPlayer.seekTo(0);
} else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
mMediaPlayer.start();
} else if (focusChange == AudioManager.AUDIOFOCUS_LOSS) {
releaseMediaPlayer();
}
}
};
private MediaPlayer.OnCompletionListener mCompletionListener = new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
releaseMediaPlayer();
}
};
public static Accommodation newInstance(int page) {
Bundle args = new Bundle();
args.putInt(ARG_PAGE, page);
Accommodation fragment = new Accommodation();
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.list_view, container, false);
mAudioManager = (AudioManager) getActivity().getSystemService(Context.AUDIO_SERVICE);
final ArrayList<word> fte = new ArrayList<word>();
fte.add(new word("I will stay for nights", "Je vais rester des nuits", R.raw.accomodation_iwillstayherefornights));
fte.add(new word("May i see the room first", "Puis-je voir la chambre d'abord", R.raw.accommodation_mayiseetherromfirst));
fte.add(new word("Is breakfast included", "Le petit déjeuner est-il inclus", R.raw.accommodation_isbreakfastincluded));
fte.add(new word("I have a reservation", "J'ai une reservation", R.raw.accommodation_ihaveareservation));
fte.add(new word("Do you have rooms available", "Avez-vous des chambres disponibles", R.raw.accommodation_doyouhaveroomsavailable));
fte.add(new word("I have locked myself out of my room", "Je me suis enfermé dans ma chamber", R.raw.accommodation_ihavelockedmyselfoutoftheroom));
fte.add(new word("What time is checkout", "A quelle heure est la caisse?", R.raw.accommodation_whattimeischeckout));
adapter = new wordAdapter(getActivity(), fte, R.color.accommodation);
ListView listView = (ListView) rootView.findViewById(R.id.list);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
releaseMediaPlayer();
word word = fte.get(position);
int result = mAudioManager.requestAudioFocus(mOnAudioFocusChangeListener,
AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
mMediaPlayer = MediaPlayer.create(getActivity(), word.getmediaPlayer());
mMediaPlayer.start();
mMediaPlayer.setOnCompletionListener(mCompletionListener);
}
}
});
return rootView;
}
@Override
public void onStop() {
super.onStop();
releaseMediaPlayer();
}
private void releaseMediaPlayer() {
if (mMediaPlayer != null) {
mMediaPlayer.release();
mMediaPlayer = null;
mAudioManager.abandonAudioFocus(mOnAudioFocusChangeListener);
}
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.main, menu);
super.onCreateOptionsMenu(menu,inflater);
MenuItem item2 = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(item2);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String query) {
adapter.getFilter().filter(query);
//adapter.notifyDataSetChanged();
return false;
}
});
}
}
菜單:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#5D4037">
<item android:id="@+id/menu_item_share"
app:showAsAction="never"
android:title="Share"
app:actionProviderClass="android.support.v7.widget.ShareActionProvider" />
<item
android:id="@+id/action_about"
android:title="About"
app:showAsAction="never" />
<item
android:id="@+id/action_support"
android:title="Support"
app:showAsAction="never" />
<item
android:id="@+id/action_search"
android:icon="@drawable/ic_search_36pt"
app:showAsAction="ifRoom"
android:orderInCategory="0"
app:actionViewClass="android.support.v7.widget.SearchView"
android:title="Search"/>
</menu>
感謝