ListView 显示正确,但仅在 onclick 时获取最后一项。 ListView 上的搜索功能无法正常工作

Posted

技术标签:

【中文标题】ListView 显示正确,但仅在 onclick 时获取最后一项。 ListView 上的搜索功能无法正常工作【英文标题】:ListView displays correctly, but only fetching last item when onclick. Search function on ListView not working as well 【发布时间】:2020-12-03 21:18:15 【问题描述】:

ListView 正确显示所有内容(标题、艺术家、封面、drawables),但是当点击时,只会获取最后一个项目(Pills), 并且搜索功能也没有过滤歌曲,不起作用,希望通过歌曲的标题进行搜索,还有一个用于 getset 方法的类等等,这些都可以正常工作

我是android Studio的初学者,请指导我,不胜感激!

搜索活动:

public class SearchActivity extends AppCompatActivity 

ListView listView;
ArrayList<String> stringArrayList = new ArrayList<>();
MyListAdapter adapter;
//ArrayAdapter<String> adapter;
private SongCollection songCollection = new SongCollection();

String[] mTitle = 
        "Life is Good",
        "Good as Hell",
        "Perfect",
        "Attention",
        "Say You Love Me",
        "Sign of the Times",

        "Lemon",
        "Autopilot",
        "First Time",
        "To U",
        "Fractures",
        "Will He",

        "I'm so Grateful",
        "Stay At Home",
        "The A Team",
        "BIGGER",
        "Find U Again",
        "Pills"
;

String[] mArtist = 
        "Drake",
        "Ariana Grande",
        "Ed Sheeran",
        "Joji",
        "Chris Brown",
        "Harry Styles",

        "Rihanna",
        "Quinn XCII",
        "Ellie Goulding",
        "Diplo",
        "ILLENIUM",
        "Joji",

        "DJ Khaled",
        "Usher",
        "Ed Sheeran",
        "Beyonce",
        "Camila Cabello",
        "Joji"
;

String[] mSongLength = 
        "3.96",
        "2.66",
        "4.39",
        "2.15",
        "2.88",
        "5.68",

        "2.45",
        "3.03",
        "3.23",
        "3.95",
        "5.03",
        "3.37",

        "4.98",
        "3.48",
        "4.31",
        "3.77",
        "2.94",
        "3.12"
;

Integer[] mCoverArt = 
        R.drawable.life_is_good,
        R.drawable.good_as_hell,
        R.drawable.perfect,
        R.drawable.attention,
        R.drawable.say_you_love_me,
        R.drawable.sign_of_the_times,

        R.drawable.lemon,
        R.drawable.auto_pilot,
        R.drawable.first_time,
        R.drawable.to_u,
        R.drawable.fractures,
        R.drawable.will_he,

        R.drawable.im_so_grateful,
        R.drawable.stay_at_home,
        R.drawable.the_a_team,
        R.drawable.bigger,
        R.drawable.find_u_again,
        R.drawable.pills
;

@Override
protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_search);

   // adapter = new ArrayAdapter<>(SearchActivity.this, R.layout.search_item, mTitle, mArtist, mCoverArt, mSongLength);

   adapter = new MyListAdapter(this, mTitle, mArtist, mCoverArt, mSongLength);

    listView = (ListView)findViewById(R.id.list_view);

    listView.setAdapter(adapter);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() 
        @Override
        public void onItemClick(AdapterView<?> parent, View view,int position, long id) 
            Toast.makeText(getApplicationContext(),
                    adapter.getItem(position), Toast.LENGTH_SHORT).show();
            // TODO Auto-generated method stub
            String title = listView.getItemAtPosition(position).toString();
            Song selectedSong = songCollection.searchByTitle(title);
            AppUtil.popMessage(view.getContext(), "Streaming song: " + selectedSong.getTitle());
            sendDatatoActivity(selectedSong);
        
    );

**MyListAdapter:**

public class MyListAdapter extends ArrayAdapter<String> 

private final Activity context;
private final String[] mTitle;
private final String[] mArtist;
private final Integer[] mCoverArt;
private final String[] mSongLength;

public MyListAdapter(Activity context, String[] mTitle,String[] mArtist, Integer[] mCoverArt, String[] mSongLength) 
    super(context, R.layout.search_item, mTitle);
    // TODO Auto-generated constructor stub

    this.context = context;
    this.mTitle = mTitle;
    this.mArtist = mArtist;
    this.mCoverArt = mCoverArt;
    this.mSongLength = mSongLength;


public View getView(int position, View view, ViewGroup parent) 
    LayoutInflater inflater=context.getLayoutInflater();
    View rowView=inflater.inflate(R.layout.search_item, null,true);

    TextView titleTxt = (TextView) rowView.findViewById(R.id.titleTxt);
    ImageView coverArt = (ImageView) rowView.findViewById(R.id.image);
    TextView artistTxt = (TextView) rowView.findViewById(R.id.titleArtist);
    TextView songLength = (TextView) rowView.findViewById(R.id.txtSongLength);

    titleTxt.setText(mTitle[position]);
    coverArt.setImageResource(mCoverArt[position]);
    artistTxt.setText(mArtist[position]);
    songLength.setText(mSongLength[position]);

    return rowView;

;

【问题讨论】:

【参考方案1】:

您写了两次setOnItemClickListener,第二次将覆盖 第一个,所以逻辑不是你想要的。

只保留一个,删除另一个。

【讨论】:

我已将代码编辑为仅 setOnItemClickListener 一次,代码仍然无法获取正确的数据

以上是关于ListView 显示正确,但仅在 onclick 时获取最后一项。 ListView 上的搜索功能无法正常工作的主要内容,如果未能解决你的问题,请参考以下文章

在swipable列表视图中具有onClickListener的视图不能再刷卡,但仅响应onClick事件

如何在android的ListView中为按钮添加OnClick()?

关键字“current_timestamp”附近的语法不正确 - 但仅在一个数据库上

新记录保存在存储中,但仅在刷新页面后显示

基础表单消失但仅在显示对话框时

在 URL 中同时显示 slug 和 ID,但仅在 Django 中按 ID 路由