我应该如何向我的音乐流媒体应用添加搜索功能?

时间:2016-12-31 07:19:37

标签: java android xml adapter baseadapter

我在这里使用BaseAdapter,我甚至尝试实施Filterable但它没有用。大多数教程使用ArrayAdapter<>进行搜索功能。如果任何人都可以建议一些更简单的approch来实现搜索功能,那将会很有帮助。

这是 res/xml/searchable.xml文件

<?xml version="1.0" encoding="utf-8"?>

<searchable xmlns:android="http://schemas.android.com/apk/res/android"
            android:label="@string/app_name"
            android:hint="@string/search_hint" />

我的Adpater课程 SCTrackAdapter.java

package com.judah.songsofzion.ui;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.judah.songsofzion.R;
import com.squareup.picasso.Picasso;

import java.util.List;

public class SCTrackAdapter extends BaseAdapter {
    private Context mContext;
    private List<Track> mTracks;

    public SCTrackAdapter(Context context, List<Track> tracks) {
        mContext = context;
        mTracks = tracks;
    }

    @Override
    public int getCount() {
        return mTracks.size();
    }

    @Override
    public Object getItem(int i) {
        return mTracks.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        Track track = (Track) getItem(i);
        ViewHolder viewHolder;
        if (view == null) {
            view = LayoutInflater.from(mContext).inflate(R.layout.track_list_row, viewGroup, false);
            viewHolder = new ViewHolder();
            viewHolder.trackImageView = (ImageView) view.findViewById(R.id.track_image);
            viewHolder.titleTextView = (TextView) view.findViewById(R.id.track_title);
            viewHolder.durationTextView = (TextView) view.findViewById(R.id.track_duration);
            viewHolder.descriptionTextView = (TextView) view.findViewById(R.id.track_description);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }
        viewHolder.titleTextView.setText(track.getTitle());
        viewHolder.durationTextView.setText(track.getDuration());
        viewHolder.descriptionTextView.setText(track.getDescription());
        Picasso.with(mContext).load(track.getArtworkURL()).into(viewHolder.trackImageView);
        return view;
    }


    static class ViewHolder {
        ImageView trackImageView;
        TextView titleTextView;
        TextView durationTextView;
        TextView descriptionTextView;
    }

}

这是 main_menu.xml文件

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/item_search"
        android:actionViewClass="android.support.v7.widget.SearchView"
        android:icon="@drawable/magnify"
        android:title="Search"
        app:actionViewClass="android.support.v7.widget.SearchView"
        app:showAsAction="always|collapseActionView"/>
</menu>

请检查下面提供的链接,我在这个案例中引用此链接:https://github.com/aliasZadok/SongsofZion

0 个答案:

没有答案