gpt4 book ai didi

android - 当我点击搜索项目时,搜索 ListView 项目位置发生变化

转载 作者:行者123 更新时间:2023-11-29 17:15:22 25 4
gpt4 key购买 nike

以下是我的适配器类

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();
}

以下是我的主要 Activity 类 fragment

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);
}
});
}}

这是我的模型类

公共(public)类 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 项目位置的确切位置。如果有人知道帮助我。提前致谢....

最佳答案

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。希望对您有所帮助。

关于android - 当我点击搜索项目时,搜索 ListView 项目位置发生变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39270408/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com