SQLite的使用及我的最爱歌曲的实现
早上好~~~~各位,今天接着昨天的android上数据库SQLite的使用来说说,怎么在昨天的基础上,实现歌曲添加入我的最爱.先上理想的效果图,进入播放界面之后就可以点击爱心添加为最爱歌曲,并放入数据库,在我的最爱栏目也可以看的到,再次点击红心就可以取消我的最爱,丛数据库再拿出来(讲解的话,主要在代码的注释里面):
先来实现点击主界面上的我的最爱后切换至我的最爱fragment,一个MyFavoriteFragment.java和一个fragment_favorite_layout.xml,先给出布局文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/music_list_layout"
android:layout_gravity="center_horizontal"
android:orientation="vertical"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:background="@drawable/bg_photo_01">
<RelativeLayout
android:id="@+id/top_layout"
android:background="#00000000"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/favorite_fragment_to_main_fragment" //从我的最爱fragment回退到mainFragment的按钮,也就是那个箭头
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/img_actionitem_back"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="15pt"
android:textColor="@color/tomato"
android:text="我 的 最 爱"
android:completionThreshold="1"
android:layout_toEndOf="@id/favorite_fragment_to_main_fragment"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/top_layout">
<ListView
android:id="@+id/favorite_music_list" //用来显示歌曲列表的litView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#ffff99c9"
android:dividerHeight="2dp"
android:drawSelectorOnTop="false"
android:listSelector="@color/cornsilk">
</ListView>
</RelativeLayout>
</RelativeLayout>
package com.example.dada.myapplication;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.net.Uri;
import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
public class MyFavoriteFragment extends Fragment {
private OnFragmentInteractionListener mListener;
public static MyFavoriteFragment newInstance() {
MyFavor