ListView displays correctly, but only fetching last item when onclick. Search function on ListView not working as well

huangapple go评论130阅读模式
英文:

ListView displays correctly, but only fetching last item when onclick. Search function on ListView not working as well

问题

ListView正确显示所有内容(标题、艺术家、封面、绘图),但当点击时,只会获取最后一项(Pills),搜索功能也无法过滤歌曲,不起作用,希望能够按歌曲标题进行搜索,还有一个用于getset方法的类也正常工作。

我是Android Studio的初学者,请指导我,非常感谢!

搜索活动:

public class SearchActivity extends AppCompatActivity {

    ListView listView;
    ArrayList<String> stringArrayList = new ArrayList<>();
    MyListAdapter 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 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();

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

        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;
    }
}
英文:

ListView is displaying all content correctly (title, artist, cover, drawables), however when clicked on, only the last item (Pills) will be fetched,
and the search function is not filtering the songs as well, not working, wish to search by the title of the song, have a class for the getset methods etc as well which is working fine

Am beginner at Android Studio, please guide me, greatly appreciated!

Search Activity:

public class SearchActivity extends AppCompatActivity {
ListView listView;
ArrayList&lt;String&gt; stringArrayList = new ArrayList&lt;&gt;();
MyListAdapter adapter;
//ArrayAdapter&lt;String&gt; adapter;
private SongCollection songCollection = new SongCollection();
String[] mTitle = {
&quot;Life is Good&quot;,
&quot;Good as Hell&quot;,
&quot;Perfect&quot;,
&quot;Attention&quot;,
&quot;Say You Love Me&quot;,
&quot;Sign of the Times&quot;,
&quot;Lemon&quot;,
&quot;Autopilot&quot;,
&quot;First Time&quot;,
&quot;To U&quot;,
&quot;Fractures&quot;,
&quot;Will He&quot;,
&quot;I&#39;m so Grateful&quot;,
&quot;Stay At Home&quot;,
&quot;The A Team&quot;,
&quot;BIGGER&quot;,
&quot;Find U Again&quot;,
&quot;Pills&quot;
};
String[] mArtist = {
&quot;Drake&quot;,
&quot;Ariana Grande&quot;,
&quot;Ed Sheeran&quot;,
&quot;Joji&quot;,
&quot;Chris Brown&quot;,
&quot;Harry Styles&quot;,
&quot;Rihanna&quot;,
&quot;Quinn XCII&quot;,
&quot;Ellie Goulding&quot;,
&quot;Diplo&quot;,
&quot;ILLENIUM&quot;,
&quot;Joji&quot;,
&quot;DJ Khaled&quot;,
&quot;Usher&quot;,
&quot;Ed Sheeran&quot;,
&quot;Beyonce&quot;,
&quot;Camila Cabello&quot;,
&quot;Joji&quot;
};
String[] mSongLength = {
&quot;3.96&quot;,
&quot;2.66&quot;,
&quot;4.39&quot;,
&quot;2.15&quot;,
&quot;2.88&quot;,
&quot;5.68&quot;,
&quot;2.45&quot;,
&quot;3.03&quot;,
&quot;3.23&quot;,
&quot;3.95&quot;,
&quot;5.03&quot;,
&quot;3.37&quot;,
&quot;4.98&quot;,
&quot;3.48&quot;,
&quot;4.31&quot;,
&quot;3.77&quot;,
&quot;2.94&quot;,
&quot;3.12&quot;
};
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&lt;&gt;(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&lt;?&gt; 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(), &quot;Streaming song: &quot; + selectedSong.getTitle());
sendDatatoActivity(selectedSong);
}
});

**MyListAdapter: **

public class MyListAdapter extends ArrayAdapter&lt;String&gt; {
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

得分: 1

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

英文:

> You wrote setOnItemClickListener twice, the second one will override
> the first one, so the logical is not your wanted.

Only keep one and delete another one.

huangapple
  • 本文由 发表于 2020年8月14日 08:53:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/63405076.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定