我是一个新的开发人员,一个月前才开始编程。我创建了一个具有回收者视图和搜索(过滤器)功能的歌曲本应用。当我单击项目列表时,位置正确。
当我在过滤器后单击该项目时,它的位置错误。歌号7单击时打开第一首歌曲。过滤器后保留在位置1的任何歌曲号都将在单击时打开1号歌曲。这是该应用程序的屏幕截图。Here I filtered song no.7 but when I click it opens song no.1
这是我的回收站适配器Java。
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ExampleViewHolder> implements Filterable {
private List<Item> songList;
private List<Item> songListFull;
private OnItemClickListener mListener;
public interface OnItemClickListener{
void OnItemClick(int position);
}
public void setOnItemClickListener(OnItemClickListener listener){
mListener=listener;
}
public static class ExampleViewHolder extends RecyclerView.ViewHolder {
TextView textView1;
TextView textView2;
ExampleViewHolder(View itemView, final OnItemClickListener listener) {
super(itemView);
textView1 = itemView.findViewById(R.id.text_view1);
textView2 = itemView.findViewById(R.id.text_view2);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (listener!=null){
int position = getAdapterPosition();
if (position!=RecyclerView.NO_POSITION){
listener.OnItemClick(position);
}
}
}
});
}
}
RecyclerAdapter(List<Item> songlist) {
this.songList = songlist;
songListFull = new ArrayList<>(songlist);
}
@NonNull
@Override
public ExampleViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item,
parent, false);
return new ExampleViewHolder(v, mListener);
}
@Override
public void onBindViewHolder(@NonNull ExampleViewHolder holder, int position) {
Item currentItem = songList.get(position);
holder.textView1.setText(currentItem.getText1());
holder.textView2.setText(currentItem.getText2());
}
@Override
public int getItemCount() {
return songList.size();
}
@Override
public Filter getFilter() {
return exampleFilter;
}
private Filter exampleFilter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
List<Item> filteredList = new ArrayList<>();
if (constraint == null || constraint.length() == 0) {
filteredList.addAll(songListFull);
} else {
String filterPattern = constraint.toString().toLowerCase().trim();
for (Item item : songListFull) {
if (item.getText1().toLowerCase().contains(filterPattern)) {
filteredList.add(item);
}
}
}
FilterResults results = new FilterResults();
results.values = filteredList;
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
songList.clear();
songList.addAll((List) results.values);
notifyDataSetChanged();
}
};
}
这是我的MainActivity Java。
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
private RecyclerAdapter adapter;
private List<Item> songList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fillSongList();
setUpRecyclerView();
} private void setUpRecyclerView() {
final RecyclerView recyclerView = findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
adapter = new RecyclerAdapter(songList);
final String[] tSong = getResources().getStringArray(R.array.song_title);
final String[] dSong = getResources().getStringArray(R.array.song_detail);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);
adapter.setOnItemClickListener(new RecyclerAdapter.OnItemClickListener(){
@Override
public void OnItemClick(int position) {
String t = tSong[position];
String d = dSong[position];
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
intent.putExtra("song",d);
intent.putExtra("title",t);
startActivity(intent);
}
});
}
private void fillSongList() {
songList = new ArrayList<>();
songList.add(new Item("Be Mulugaonw- 1", "1"));
songList.add(new Item( "Gwdan Jerusalem nwgwrao -2" , "2"));
songList.add(new Item("Jebla Jisu Kristoa fwipingwn -3","3"));
songList.add(new Item("Ang solo kwnadwngmwn - 4", "4"));
songList.add(new Item( "Gitao tao mwnse som - 5", "5"));
songList.add(new Item( "Fwrbu jwngjwng nujafwidw - 6","6"));
songList.add(new Item( "Onjlaigra bida fongbaifwr - 7","7"));
songList.add(new Item("Jwngw onnaiao mwnselo - 8","8"));
songList.add(new Item("Naihor hadorao mwnse - 9","9"));
songList.add(new Item("Buhumkou salidwng - 10","10"));
songList.add(new Item("Nwng boha dong - 11","11"));
songList.add(new Item("Wi songsarni bida fongbaifwr - 12", "12"));
songList.add(new Item("Wi bahaifwr gwsw swlai - 13","13"));
songList.add(new Item("Nwi hani lwgwfwr nwi - 14","14"));
songList.add(new Item("Bwtwra katiswi - 15","15"));
songList.add(new Item("Be songsarao hani derakoulai - 16", "16"));
songList.add(new Item("Bwkangjanaikou nagirwbla - 17","17"));
songList.add(new Item("Gwsw swlaidw boibw - 18","18"));
songList.add(new Item("Dagi daogalang switanjwng - 19", "19"));
songList.add(new Item("Tu tabai laswi laswi - 20","20"));
请帮助我,请
除了发送位置以外,在该位置发送整个项目
public interface OnItemClickListener{
void OnItemClick(Item item);
}
通过该位置的项目
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (listener!=null){
int position = getAdapterPosition();
if (position!=RecyclerView.NO_POSITION){
listener.OnItemClick(yuurSonglist[position]);
}
}
}
});