我有一个可过滤的自定义列表视图。它目前有过滤器。但是在过滤后,原始位置会丢失,当我设置onClickListener
时,它会显示错误的结果。有没有办法获得原来的位置?
搜索适配器
public class SearchAdapter extends ArrayAdapter implements Filterable{
private List<word> allModelItemsArray;
private List<word> filteredModelItemsArray;
private Activity context;
private ModelFilter filter;
private LayoutInflater inflator;
MediaPlayer mediaPlayer;
private List<word> list;
public SearchAdapter(Activity context, List<word> list) {
super(context, R.layout.search_row, list);
this.context = context;
this.allModelItemsArray = new ArrayList<word>();
this.list = list;
allModelItemsArray.addAll(list);
this.filteredModelItemsArray = new ArrayList<word>();
filteredModelItemsArray.addAll(allModelItemsArray);
inflator = context.getLayoutInflater();
getFilter();
}
@Override
public Filter getFilter() {
if (filter == null){
filter = new ModelFilter();
}
return filter;
}
static class ViewHolder {
protected TextView text;
protected TextView translatedText;
protected TextView pronounce;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
Typeface font = Typeface.createFromAsset(context.getAssets(), "fonts/NotoSans-Regular.ttf");
word m = filteredModelItemsArray.get(position);
ViewHolder viewHolder = null;
if (convertView == null) {
view = inflator.inflate(R.layout.search_row, null);
viewHolder = new ViewHolder();
viewHolder.text = (TextView) view.findViewById(R.id.list_view_search_words);
viewHolder.text.setTypeface(font);
viewHolder.translatedText = (TextView) view.findViewById(R.id.translated_text);
viewHolder.translatedText.setTypeface(font);
viewHolder.pronounce = (TextView) view.findViewById(R.id.pronounce);
viewHolder.pronounce.setTypeface(font);
view.setTag(viewHolder);
} else {
view = convertView;
viewHolder = ((ViewHolder) view.getTag());
}
viewHolder.text.setText(m.getMainText());
viewHolder.translatedText.setText(m.getTranslationText());
viewHolder.pronounce.setText(m.getPronunciation_in_english());
/*get full sound file name*/
Integer fileNameLength = m.getAudio().toString().length();
String fileName = m.getAudio().toString();
final String soundFile = fileName.substring(0,fileNameLength - 4);
final ImageView imgPlay = (ImageView) view.findViewById(R.id.phraselist_play_audio);
imgPlay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mediaPlayer == null) {
View parentRow = (View) v.getParent();
ListView listView = (ListView) parentRow.getParent();
final int position = listView.getPositionForView(parentRow);
try {
Uri mp3 = Uri.parse("android.resource://" + context.getPackageName() + "/raw/" + soundFile);
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(context, mp3);
mediaPlayer.prepare(); // might take long! (for buffering, etc)
mediaPlayer.start();
imgPlay.setImageResource(R.drawable.playactive);
// mediaPlayer.setOnCompletionListener(onCompletionListener);
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
mediaPlayer.release();
mediaPlayer = null;
imgPlay.setImageResource(R.drawable.playinactive);
}
});
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
return view;
}
private class ModelFilter extends Filter
{
@Override
protected FilterResults performFiltering(CharSequence constraint) {
constraint = constraint.toString().toLowerCase();
FilterResults result = new FilterResults();
if(constraint != null && constraint.toString().length() > 0)
{
ArrayList<word> filteredItems = new ArrayList<word>();
for(int i = 0, l = allModelItemsArray.size(); i < l; i++)
{
word m = allModelItemsArray.get(i);
if(m.getMainText().toLowerCase().contains(constraint))
filteredItems.add(m);
}
result.count = filteredItems.size();
result.values = filteredItems;
}
else
{
synchronized(this)
{
result.values = allModelItemsArray;
result.count = allModelItemsArray.size();
}
}
return result;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
filteredModelItemsArray = (ArrayList<word>)results.values;
notifyDataSetChanged();
clear();
for(int i = 0, l = filteredModelItemsArray.size(); i < l; i++)
add(filteredModelItemsArray.get(i));
notifyDataSetInvalidated();
}
}
}
搜索列表活动(onResume功能)
protected void onResume(){
super.onResume();
GlobalState state = ((GlobalState) getApplicationContext());
state.doAction();
wordDb = new wordDB(SearchListActivity.this);
paymentDb = new paymentDB(SearchListActivity.this);
//getAllWords();
final ArrayList<word> words = new ArrayList<word>();
Cursor row;
Integer checkPayment = paymentDb.CheckTable();
/*If free not purchased*/
if(checkPayment == 0)
{
row = wordDb.selectWordList("0");
}
else
{
row = wordDb.selectAllWord();
}
row.moveToFirst();
while (!row.isAfterLast()) {
words.add( new word(row.getString(0),row.getString(1),row.getString(2),row.getString(3),row.getString(4),row.getString(5),row.getString(6),row.getString(7),row.getString(8),row.getString(9)));
row.moveToNext();
}
row.close();
final SearchAdapter adapter = new SearchAdapter(SearchListActivity.this, words);
final ListView listView = (ListView) findViewById(R.id.search_list_view_word);
listView.setAdapter(adapter);
final EditText etSearch = (EditText) findViewById(R.id.etSearch);
// Add Text Change Listener to EditText
etSearch.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// Call back the Adapter with current character to Filter
adapter.getFilter().filter(s.toString());
adapter.notifyDataSetChanged();
listView.setAdapter(adapter);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,int after) {
}
@Override
public void afterTextChanged(Editable s) {
if(s.length()==0){
}
}
});
ListView searchListView;
searchListView = (ListView)findViewById(R.id.search_list_view_word);
searchListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Object o = parent.getItemAtPosition(position);
word_obj = (word) o;
if (Integer.valueOf(word_obj.getCategoryId()) >= 0) {
Intent myIntent = new Intent(SearchListActivity.this, WordDetailsActivity.class);
myIntent.putExtra("word_obj", word_obj);
myIntent.putExtra("position", position);
//Log.d("id",String.valueOf(id));
//Log.d("getitem", String.valueOf(adapter.getItemId(position)));
//words.get(words.indexOf(searchAdapter.getItemId(position)));
myIntent.putExtra("currentClickedId", word_obj.getCsvWordId().toString());
myIntent.putExtra("favouriteFlag", 0);
myIntent.putExtra("searchFlag", 1);
myIntent.putExtra("searchString", etSearch.getText().toString());
SearchListActivity.this.startActivity(myIntent);
}
}
});
}
答案 0 :(得分:0)
在列表视图的项目点击内,您将对象置于所需位置,对吧?
比较该对象的路径(考虑它是一首歌)或歌曲或原始数组列表中的独特内容。
运行for
循环,并在包含所有歌曲的原始歌曲列表的整个大小上进行迭代。获得匹配结果后,将for
循环的循环控制变量存储到position
中,然后在意图中使用它。
答案 1 :(得分:0)
song_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
SongModel songModel = (SongModel)arrayAdapter.getItem(position);
/* Toast.makeText(getActivity().getApplicationContext(),songModel.getSongTitle(),Toast.LENGTH_SHORT).show();*/
//用于循环,即使在搜索之后也能识别歌曲的正确位置
String path = songModel.path();
for(int i=0;i<songs.size();i++){
if(songs.get(i).path().equals(path)){
pos=i;
}
}
Intent intent
= new Intent(getActivity(), PlayMedia.class);
intent.putExtra("position", pos);
intent.putExtra("songlist", songs);
intent.putExtra("clicked", "yes");
startActivity(intent);
}
});
return view;
}
这里是我的点击监听器,我正在编写用于识别被点击的歌曲的正确路径的代码。