以下是我的适配器类
public class SongAdapter extends BaseAdapter{
Context context;
//private ArrayList<SongModel> songsList = null;
SongAdapter songAdapter;
private ArrayList<SongModel> songsListDat = null;
private ArrayList<SongModel> songsList;
private static LayoutInflater inflater;
public SongAdapter(FragmentActivity activity, ArrayList<SongModel> songsListDat) {
this.context = activity;
this.songsListDat = songsListDat;
this.songsList = new ArrayList<SongModel>();
this.songsList.addAll(songsListDat);
}
@Override
public int getViewTypeCount() {
return getCount();
}
@Override
public int getCount() {
return songsListDat.size();
}
@Override
public Object getItem(int position) {
return songsListDat.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getItemViewType(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View vi = convertView;
if (convertView == null)
vi = inflater.inflate(R.layout.playlist_item, null);
TextView title = (TextView) vi.findViewById(R.id.songTitle); // title
SongModel song = new SongModel();
song = songsListDat.get(position);
// Setting all values in listview
title.setText(song.getSongTitle());
return vi;
}
// Filter Class
public void filter(String charText) {
charText = charText.toLowerCase(Locale.getDefault());
songsListDat.clear();
if (charText.length() == 0) {
songsListDat.addAll(songsList);
}
else {
for (SongModel song : songsList) {
if (song.getSongPath().toLowerCase(Locale.getDefault()).contains(charText)) {
songsListDat.add(song);
}
}
}
notifyDataSetChanged();
}
以下是我的主要活动类片段
EditText edtSearch;
ListAdapter adapter;
SongAdapter songAdapter;
ArrayList<SongModel> songList = new ArrayList<SongModel>();
SongsManager songsManager = new SongsManager();
// Songs list
public ArrayList<SongModel> songsList = new ArrayList<>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View windows = inflater.inflate(R.layout.playlist, container, false);
return windows;
}
@Override
public void onViewCreated(View v, Bundle savedInstanceState) {
super.onViewCreated(v, savedInstanceState);
edtSearch = (EditText)getView().findViewById(R.id.search);
final ArrayList<SongModel> songsListData = songsManager.songList;
Log.i("songsListData...",""+songsListData.size());
SongsManager plm = new SongsManager();
// get all songs from sdcard
this.songsList = plm.getPlayList();
// looping through playlist
for (int i = 0; i < songsListData.size(); i++) {
// creating new HashMap
SongModel song = songsListData.get(i);
// adding HashList to ArrayList
songsListData.add(song);
}
// Adding menuItems to ListView
/* adapter = new SimpleAdapter(getActivity(), songsListData, R.layout.playlist_item, new String[] { "songTitle" }, new int[] {
R.id.songTitle });*/
songAdapter = new SongAdapter(getActivity(),songsList);
setListAdapter(songAdapter);
// selecting single ListView item
final ListView lv = getListView();
// listening to single listitem click
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View arg0,
int position, long id) {
// getting listitem index
Log.i("Index", "..." + position);
//int songIndex = position;
// Starting new intent
Intent i = new Intent(getActivity().getApplicationContext(),Main.class);
songAdapter.getItem(position);
// Sending songIndex to PlayerActivity
i.putExtra("songIndex",position);
getActivity().setResult(100, i);
startActivityForResult(i, 100);
}
});
edtSearch.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence cs, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
String text = edtSearch.getText().toString().toLowerCase(Locale.getDefault());
songAdapter.filter(text);
}
});
}}
这是我的模特课
公共类SongModel {
private String songPath;
private String songTitle;
public SongModel(String songPath,String songTitle)
{
this.songPath = songPath;
this.songTitle = songTitle;
}
public void setSongPath(String songPath){
this.songPath = songPath;
}
public String getSongPath() {
return songPath;
}
public void setSongTitle(String songTitle){
this.songTitle = songTitle;
}
public String getSongTitle() {
return songTitle;
}}
我如何获得搜索的listview项目位置的确切位置。 如果有人知道帮助我。 提前谢谢....
答案 0 :(得分:1)
调用filter()
和notifyDatasetChanged()
时,项目的原始索引会丢失。要检索原始位置,请在onItemClick()
:
SongModel sm = songAdapter.getItem(position);
int originalIndex = songAdapter.songList.indexOf(sm);
//Do whatever you want with originalIndex
答案 1 :(得分:1)
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View arg0,
int position, long id) {
// getting listitem index
Log.i("Index", "..." + position);
//int songIndex = position;
// Starting new intent
Intent i = new Intent(getActivity().getApplicationContext(),Main.class);
SongModel song = (SongModel)songAdapter.getItem(position);
// Sending songIndex to PlayerActivity
i.putExtra("songIndex",song.getId() );
getActivity().setResult(100, i);
startActivityForResult(i, 100);
}
});
看到你的歌曲模型中必须有一个唯一的歌曲ID,而不是传递该ID。 希望它有所帮助。
答案 2 :(得分:1)
我得到了简单的答案。在您执行view
的{{1}}中,执行以下操作:
onItemClick()
这将是原始所选歌曲列表中的实际位置。
希望这会对你有所帮助。
答案 3 :(得分:0)
尝试下面给出的代码相应地更改
public class SongAdapter extends ArrayAdapter<ToursModel> {
private List<SongModel> originalList;
private List<SongModel> tourList;
private SongFilter filter;
private Context context;
public SongAdapter(Context context, int resource, List<SongModel> songList) {
super(context, resource, songList);
this.context = context;
this.songList = new ArrayList<SongModel>();
this.songList.addAll(tourList);
this.originalList = new ArrayList<>();
this.originalList.addAll(songList);
// backup = songList;
getFilter();
}
@Override
public Filter getFilter() {
if (filter == null) {
filter = new TourFilter();
}
return filter;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
// Log.v("ConvertView", String.valueOf(position));
if (convertView == null) {
LayoutInflater vi = (LayoutInflater) context.getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.tour_list_item, null);
holder = new ViewHolder();
} else {
holder = (ViewHolder) convertView.getTag();
}
return convertView;
}
private class ViewHolder {
TextView songName;
}
private class TourFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
constraint = constraint.toString().toLowerCase();
FilterResults result = new FilterResults();
if (constraint != null && constraint.toString().length() > 0) {
ArrayList<SongModel> filteredItems = new ArrayList<SongModel>();
for (int i = 0, l = originalList.size(); i < l; i++) {
SongModel country = originalList.get(i);
if (country.getTourDes().toString().toLowerCase().contains(constraint))
filteredItems.add(country);
}
result.count = filteredItems.size();
result.values = filteredItems;
} else {
synchronized (this) {
result.values = originalList;
result.count = originalList.size();
// Log.e("originalist", "" + originalList.size());
}
// // Log.e("originalist", "original");
}
return result;
}
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint,
FilterResults results) {
tourList = (ArrayList<SongModel>) results.values;
notifyDataSetChanged();
clear();
for (int i = 0, l = songList.size(); i < l; i++)
add(tourList.get(i));
// notifyDataSetInvalidated();
}
}
}