2016-07-05 42 views
1

Android Studio中ListView項收藏ImageButton的特點Android Studio中ListView項收藏ImageButton的特點

嗨,大家好,

請幫助我。

我有心ImageButton的listview項目。 (@ drawable/heart)

我想在點擊點擊後將(@ drawable/heart)改成(@ drawable/heartred)。

這意味着它是標記爲最愛。

我還想在我最喜歡的選項卡中顯示那些帶有紅色心臟的項目。

下面是圖片和代碼。

enter image description here

fragment_fragment_one.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.magicstarme.virtualsongbook.FragmentOne"> 

    <!-- TODO: Update blank fragment layout --> 
    <!--<TextView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:text="@string/hello_blank_fragment_one" />--> 

    <TabHost 
     android:id="@+id/tabHost" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true"> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="vertical"> 

      <TabWidget 
       android:id="@android:id/tabs" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content"></TabWidget> 

      <FrameLayout 
       android:id="@android:id/tabcontent" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent"> 

       <LinearLayout 
        android:id="@+id/tab1" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:background="#ffffff" 
        android:orientation="vertical"> 

        <ListView 
         android:id="@+id/slListView" 
         android:layout_width="match_parent" 
         android:layout_height="match_parent" 
         android:listSelector="@android:color/transparent" 
         android:cacheColorHint="@android:color/transparent"/> 

       </LinearLayout> 

       <LinearLayout 
        android:id="@+id/tab2" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:background="#ffffff" 
        android:orientation="vertical"> 

        <TextView 
         android:layout_width="match_parent" 
         android:layout_height="match_parent" 
         android:text="This is tab 2" /> 
       </LinearLayout> 

       <LinearLayout 
        android:id="@+id/tab3" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:background="#ffffff" 
        android:orientation="vertical"> 

        <TextView 
         android:layout_width="match_parent" 
         android:layout_height="match_parent" 
         android:text="This is tab 3" /> 
       </LinearLayout> 
      </FrameLayout> 
     </LinearLayout> 
    </TabHost> 

</FrameLayout> 

fragment_fragment_one_slview.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal" 
    xmlns:android="http://schemas.android.com/apk/res/android"> 

    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:paddingRight="5dip" 
     android:orientation="vertical" 
     android:layout_weight="3 
     "> 
     <FrameLayout 
      android:id="@+id/framelayout" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 

      <TextView 
       android:id="@+id/title" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:textSize="17dp" 
       android:textStyle="bold" /> 

     </FrameLayout> 

     <FrameLayout 
      android:id="@+id/framelayout2" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 

      <TextView 
       android:id="@+id/artist" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:textSize="15dp" 
       android:layout_gravity="left|top"/> 

      <TextView 
       android:id="@+id/volume" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:textSize="15dp" 
       android:layout_gravity="right|top"/> 

     </FrameLayout> 

    </LinearLayout> 

    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_alignParentTop="true" 
     android:layout_weight="1" 
     android:layout_toStartOf="@+id/relativeLayout3"> 

     <FrameLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_alignParentTop="true" 
      android:layout_alignParentStart="true" 
      android:padding="5dp"> 

      <ImageButton 
       android:id="@+id/mic" 
       android:layout_width="32dp" 
       android:layout_height="32dp" 
       android:src="@drawable/mic" 
       android:layout_gravity="left|top" 
       android:background="#ffffff" /> 


     </FrameLayout> 

    </LinearLayout> 

    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_weight="1" 
     android:layout_height="match_parent" 
     android:background="#cccccc" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentEnd="true"> 

     <FrameLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:padding="5dp" 
      android:background="#cccccc"> 

      <TextView 
       android:id="@+id/number" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:textSize="17dp" 
       android:textStyle="bold" 
       android:layout_alignParentTop="true" 
       android:layout_centerHorizontal="true" 
       android:layout_gravity="center_horizontal|top" /> 

     </FrameLayout> 

    </LinearLayout> 
</LinearLayout> 

FragmentOne.class

package com.magicstarme.virtualsongbook; 

import android.content.Context; 
import android.net.Uri; 
import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.support.v7.app.ActionBar; 
import android.support.v7.app.ActionBarActivity; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.AdapterView; 
import android.widget.ListView; 
import android.widget.TabHost; 
import android.widget.ToggleButton; 

import java.util.ArrayList; 


/** 
* A simple {@link Fragment} subclass. 
* Activities that contain this fragment must implement the 
* {@link FragmentOne.OnFragmentInteractionListener} interface 
* to handle interaction events. 
* Use the {@link FragmentOne#newInstance} factory method to 
* create an instance of this fragment. 
*/ 
public class FragmentOne extends Fragment { 
    // TODO: Rename parameter arguments, choose names that match 
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER 
    private static final String ARG_PARAM1 = "param1"; 
    private static final String ARG_PARAM2 = "param2"; 

    // TODO: Rename and change types of parameters 
    private String mParam1; 
    private String mParam2; 

    private OnFragmentInteractionListener mListener; 

    public FragmentOne() { 
     // Required empty public constructor 
    } 

    /** 
    * Use this factory method to create a new instance of 
    * this fragment using the provided parameters. 
    * 
    * @param param1 Parameter 1. 
    * @param param2 Parameter 2. 
    * @return A new instance of fragment FragmentOne. 
    */ 
    // TODO: Rename and change types and number of parameters 
    public static FragmentOne newInstance(String param1, String param2) { 
     FragmentOne fragment = new FragmentOne(); 
     Bundle args = new Bundle(); 
     args.putString(ARG_PARAM1, param1); 
     args.putString(ARG_PARAM2, param2); 
     fragment.setArguments(args); 
     return fragment; 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     if (getArguments() != null) { 
      mParam1 = getArguments().getString(ARG_PARAM1); 
      mParam2 = getArguments().getString(ARG_PARAM2); 
     } 
    } 

    public static boolean heart_selected = false; 

    public static int position = -1; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     View rootView = inflater.inflate(R.layout.fragment_fragment_one, container, false); 

     TabHost host = (TabHost) rootView.findViewById(R.id.tabHost); 
     host.setup(); 

     //Tab 1 
     TabHost.TabSpec spec = host.newTabSpec("SONG LIST"); 
     spec.setContent(R.id.tab1); 
     spec.setIndicator("SONG LIST"); 
     host.addTab(spec); 

      ArrayList<FragmentOne_slResults> slResults = GetSearchResults(); 
     final ListView sllv = (ListView) rootView.findViewById(R.id.slListView); 
     sllv.setAdapter(new FragmentOne_Adapter(getActivity(), slResults)); 

     //Tab 2 
     spec = host.newTabSpec("NEW SONGS"); 
     spec.setContent(R.id.tab2); 
     spec.setIndicator("NEW SONGS"); 
     host.addTab(spec); 

     //Tab 3 
     spec = host.newTabSpec("FAVORITES"); 
     spec.setContent(R.id.tab3); 
     spec.setIndicator("FAVORITES"); 
     host.addTab(spec); 

     return rootView; 
    } 

    //SONG LIST 
    private ArrayList<FragmentOne_slResults> GetSearchResults(){ 
     ArrayList<FragmentOne_slResults> results = new ArrayList<FragmentOne_slResults>(); 

     FragmentOne_slResults slr = new FragmentOne_slResults(); 
     slr.setId("1"); 
     slr.setTitle("Song Title 1 Song Title 1 Title 2"); 
     slr.setArtist("Artist 1"); 
     slr.setVolume("Vol 1"); 
     slr.setNumber("1"); 
     results.add(slr); 

     slr = new FragmentOne_slResults(); 
     slr.setId("2"); 
     slr.setTitle("Song Title 2 Song Title 2 Title 2"); 
     slr.setArtist("Artist 2"); 
     slr.setVolume("Vol 2"); 
     slr.setNumber("2"); 
     results.add(slr); 

     slr = new FragmentOne_slResults(); 
     slr.setId("3"); 
     slr.setTitle("Song Title 3"); 
     slr.setArtist("Artist 3"); 
     slr.setVolume("Vol 3"); 
     slr.setNumber("3"); 
     results.add(slr); 

     slr = new FragmentOne_slResults(); 
     slr.setId("4"); 
     slr.setTitle("Song Title 2"); 
     slr.setArtist("Artist 2"); 
     slr.setVolume("Vol 2"); 
     slr.setNumber("4"); 
     results.add(slr); 

     slr = new FragmentOne_slResults(); 
     slr.setId("5"); 
     slr.setTitle("Song Title 2 Song Title 2 Title 2"); 
     slr.setArtist("Artist 3"); 
     slr.setVolume("Vol 3"); 
     slr.setNumber("5"); 
     results.add(slr); 

     slr = new FragmentOne_slResults(); 
     slr.setId("6"); 
     slr.setTitle("Song Title 2"); 
     slr.setArtist("Artist 2"); 
     slr.setVolume("Vol 2"); 
     slr.setNumber("6"); 
     results.add(slr); 

     slr = new FragmentOne_slResults(); 
     slr.setId("7"); 
     slr.setTitle("Song Title 2 Song Title 2 Title 2 Song Title 2 Song Title 2 Title 23 tle 2 Song Title 2 Song Title 2 Title 2"); 
     slr.setArtist("Artist 3"); 
     slr.setVolume("Vol 3"); 
     slr.setNumber("7"); 
     results.add(slr); 

     slr = new FragmentOne_slResults(); 
     slr.setId("8"); 
     slr.setTitle("Song Title 2"); 
     slr.setArtist("Artist 2"); 
     slr.setVolume("Vol 2"); 
     slr.setNumber("8"); 
     results.add(slr); 

     slr = new FragmentOne_slResults(); 
     slr.setId("9"); 
     slr.setTitle("Song Title 3"); 
     slr.setArtist("Artist 3"); 
     slr.setVolume("Vol 3"); 
     slr.setNumber("9"); 
     results.add(slr); 

     slr = new FragmentOne_slResults(); 
     slr.setId("10"); 
     slr.setTitle("Song Title 2"); 
     slr.setArtist("Artist 2"); 
     slr.setVolume("Vol 2"); 
     slr.setNumber("10"); 
     results.add(slr); 

     slr = new FragmentOne_slResults(); 
     slr.setId("11"); 
     slr.setTitle("Song Title 3"); 
     slr.setArtist("Artist 3"); 
     slr.setVolume("Vol 3"); 
     slr.setNumber("11"); 
     results.add(slr); 

     slr = new FragmentOne_slResults(); 
     slr.setId("12"); 
     slr.setTitle("Song Title 2"); 
     slr.setArtist("Artist 2"); 
     slr.setVolume("Vol 2"); 
     slr.setNumber("12"); 
     results.add(slr); 

     slr = new FragmentOne_slResults(); 
     slr.setId("13"); 
     slr.setTitle("Song Title 3"); 
     slr.setArtist("Artist 3"); 
     slr.setVolume("Vol 3"); 
     slr.setNumber("13"); 
     results.add(slr); 

     slr = new FragmentOne_slResults(); 
     slr.setId("14"); 
     slr.setTitle("Song Title 2"); 
     slr.setArtist("Artist 2"); 
     slr.setVolume("Vol 2"); 
     slr.setNumber("14"); 
     results.add(slr); 

     slr = new FragmentOne_slResults(); 
     slr.setId("15"); 
     slr.setTitle("Song Title 3"); 
     slr.setArtist("Artist 3"); 
     slr.setVolume("Vol 3"); 
     slr.setNumber("15"); 
     results.add(slr); 


     return results; 
    } 

    // TODO: Rename method, update argument and hook method into UI event 
    public void onButtonPressed(Uri uri) { 
     if (mListener != null) { 
      mListener.onFragmentInteraction(uri); 
     } 
    } 

    @Override 
    public void onAttach(Context context) { 
     super.onAttach(context); 
     if (context instanceof OnFragmentInteractionListener) { 
      mListener = (OnFragmentInteractionListener) context; 
     } else { 
      throw new RuntimeException(context.toString() 
        + " must implement OnFragmentInteractionListener"); 
     } 
    } 

    @Override 
    public void onDetach() { 
     super.onDetach(); 
     mListener = null; 
    } 

    /** 
    * This interface must be implemented by activities that contain this 
    * fragment to allow an interaction in this fragment to be communicated 
    * to the activity and potentially other fragments contained in that 
    * activity. 
    * <p/> 
    * See the Android Training lesson <a href= 
    * "http://developer.android.com/training/basics/fragments/communicating.html" 
    * >Communicating with Other Fragments</a> for more information. 
    */ 
    public interface OnFragmentInteractionListener { 
     // TODO: Update argument type and name 
     void onFragmentInteraction(Uri uri); 
    } 
} 

FragmentOne_Adapter.class

package com.magicstarme.virtualsongbook; 

import android.content.Context; 
import android.media.Image; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.ImageButton; 
import android.widget.ImageView; 
import android.widget.TextView; 
import android.widget.ToggleButton; 

import java.util.ArrayList; 

/** 
* Created by Joe on 6/29/2016. 
*/ 
public class FragmentOne_Adapter extends BaseAdapter { 
    private static ArrayList<FragmentOne_slResults> oneslArrayList; 

    private LayoutInflater mInflater; 
    private boolean[] isFavorite; 

    public FragmentOne_Adapter(Context context, ArrayList<FragmentOne_slResults> slresults) { 
     oneslArrayList = slresults; 
     mInflater = LayoutInflater.from(context); 
    } 

    public int getCount() { 
     return oneslArrayList.size(); 
    } 

    public Object getItem(int position) { 
     return oneslArrayList.get(position); 
    } 

    public long getItemId(int position) { 
     return position; 
    } 

    public View getView(final int position, View convertView, ViewGroup parent) { 
     final ViewHolder holder; 
     if (convertView == null) { 
      convertView = mInflater.inflate(R.layout.fragment_fragment_one_slview, null); 
      holder = new ViewHolder(); 

      holder.txtTitle = (TextView) convertView.findViewById(R.id.title); 
      holder.txtArtist = (TextView) convertView.findViewById(R.id.artist); 
      holder.txtVolume = (TextView) convertView.findViewById(R.id.volume); 
      holder.txtNumber = (TextView) convertView.findViewById(R.id.number); 

      holder.buttonHeart = (ImageButton) convertView.findViewById(R.id.heart); 

      convertView.setTag(holder); 
     } else { 
      holder = (ViewHolder) convertView.getTag(); 
     } 

     holder.txtTitle.setText(oneslArrayList.get(position).getTitle()); 
     holder.txtArtist.setText(oneslArrayList.get(position).getArtist()); 
     holder.txtVolume.setText(oneslArrayList.get(position).getVolume()); 
     holder.txtNumber.setText(oneslArrayList.get(position).getNumber()); 

     if (oneslArrayList.get(position).isFavorite) { 
      holder.buttonHeart.setImageResource(R.drawable.heartred); 
     } 
     holder.buttonHeart.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View view) { 
       // Toggle the favorites status of the imageButton and the data. 
       boolean isFavorite = !oneslArrayList.get(position).isFavorite; 
       oneslArrayList.get(position).setIsFavorite(isFavorite); 
       holder.buttonHeart.setImageResource(isFavorite ? R.drawable.heartred : R.drawable.heart); 
      } 
     }); 

     return convertView; 
    } 

    static class ViewHolder { 
     TextView txtTitle; 
     TextView txtArtist; 
     TextView txtVolume; 
     TextView txtNumber; 
     ImageButton buttonHeart; 
     ToggleButton btnFavorite; 
     ImageView btnHeart; 
    } 
} 

FragmentOne_Result.class

包com.magicstarme.virtualsongbook;

/** 
* Created by Joe on 6/29/2016. 
*/ 
public class FragmentOne_slResults{ 
    private String id = ""; 
    private String title = ""; 
    private String artist = ""; 
    private String volume = ""; 
    private String number = ""; 
    boolean isFavorite; 

    public void setId(String id) { 
     this.id = id; 
    } 
    public String getId() { 
     return id; 
    } 

    public void setTitle(String title) { 
     this.title = title; 
    } 
    public String getTitle() {return title;} 

    public void setArtist(String artist) { 
     this.artist = artist; 
    } 
    public String getArtist() { 
     return artist; 
    } 

    public void setVolume(String volume) { 
     this.volume = volume; 
    } 
    public String getVolume() { 
     return volume; 
    } 

    public void setNumber(String number) {this.number = number;} 
    public String getNumber() {return number;} 

    public void setIsFavorite(boolean isFavorite) {this.isFavorite = isFavorite;} 
    //public String getFavorite() {return favorite;} 
} 
+0

了!需要幫助.. – Joehamir

+0

即時通訊試圖開發一個這樣的應用程序https://play.google.com/store/apps/details?id=com.platinumkaraoke.digitalsongbook&hl=en這個應用程序使用數據庫?或者一切都是靜態的? – Joehamir

回答

2
Instead of imagebutton use Toggle for favourite 

<ToggleButton 
    android:id="@+id/tgbFav" 
    android:layout_width="30dp" 
    android:layout_height="30dp" 
    android:focusable="false" 
    android:focusableInTouchMode="false" 
    android:background="@drawable/fav_checked" 
    android:textOff="" 
    android:textOn=""/> 

fav_checked.xml

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<item android:drawable="@drawable/heart" 
    android:state_checked="true" /> 
<item android:drawable="@drawable/heartred" 
    android:state_checked="false" 
    /> 

的最愛列表顯示切換一次點擊更新數據庫或將此項目作爲最喜歡的,所以你可以列出所有喜愛的項目。希望這可以幫助你

在你的適配器內

[ur fav toggle].setChecked(favorites[position]); 
    [ur fav toggle].setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Boolean fav; 
      if (favorites[position] == true) { 
       favorites[position] = false; 
      } else { 
       favorites[position] = true; 
      } 
     } 
    }); 
public View getView(final int position, View convertView, ViewGroup parent) { 
    final ViewHolder holder; 
    if (convertView == null) { 
     convertView = mInflater.inflate(R.layout.fragment_fragment_one_slview, null); 
     holder = new ViewHolder(); 

     holder.txtTitle = (TextView) convertView.findViewById(R.id.title); 
     holder.txtArtist = (TextView) convertView.findViewById(R.id.artist); 
     holder.txtVolume = (TextView) convertView.findViewById(R.id.volume); 
     holder.txtNumber = (TextView) convertView.findViewById(R.id.number); 

     holder.buttonHeart = (ToggleButton) convertView.findViewById(R.id.heart); 

     convertView.setTag(holder); 
    } else { 
     holder = (ViewHolder) convertView.getTag(); 
    } 

    holder.txtTitle.setText(oneslArrayList.get(position).getTitle()); 
    holder.txtArtist.setText(oneslArrayList.get(position).getArtist()); 
    holder.txtVolume.setText(oneslArrayList.get(position).getVolume()); 
    holder.txtNumber.setText(oneslArrayList.get(position).getNumber()); 

     holder.buttonHeart.setChecked(oneslArrayList.get(position).isFavorite); 
    holder.buttonHeart.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View view) { 
      boolean isFavorite = !oneslArrayList.get(position).isFavorite; 
      oneslArrayList.get(position).setIsFavorite(isFavorite); 
     } 
    }); 

    return convertView; 
} 

我已更新適配器的getView 現在通過插入,獲取和更新(用於收藏夾)功能來創建數據庫。然後在FragmentOne_slResults類中初始化boolean isFavorite=false。 所以在這個類DATABSE上最喜歡的,現在這些項目添加

//SONG LIST 
private ArrayList<FragmentOne_slResults> GetSearchResults(){ 
    ArrayList<FragmentOne_slResults> results = new ArrayList<FragmentOne_slResults>(); 

    FragmentOne_slResults slr = new FragmentOne_slResults(); 
    slr.setId("1"); 
    slr.setTitle("Song Title 1 Song Title 1 Title 2"); 
    slr.setArtist("Artist 1"); 
    slr.setVolume("Vol 1"); 
    slr.setNumber("1"); 
    results.add(slr); 

    slr = new FragmentOne_slResults(); 
    slr.setId("2"); 
    slr.setTitle("Song Title 2 Song Title 2 Title 2"); 
    slr.setArtist("Artist 2"); 
    slr.setVolume("Vol 2"); 
    slr.setNumber("2"); 
    results.add(slr); 

    slr = new FragmentOne_slResults(); 
    slr.setId("3"); 
    slr.setTitle("Song Title 3"); 
    slr.setArtist("Artist 3"); 
    slr.setVolume("Vol 3"); 
    slr.setNumber("3"); 
    results.add(slr); 

    slr = new FragmentOne_slResults(); 
    slr.setId("4"); 
    slr.setTitle("Song Title 2"); 
    slr.setArtist("Artist 2"); 
    slr.setVolume("Vol 2"); 
    slr.setNumber("4"); 
    results.add(slr); 

    slr = new FragmentOne_slResults(); 
    slr.setId("5"); 
    slr.setTitle("Song Title 2 Song Title 2 Title 2"); 
    slr.setArtist("Artist 3"); 
    slr.setVolume("Vol 3"); 
    slr.setNumber("5"); 
    results.add(slr); 

    slr = new FragmentOne_slResults(); 
    slr.setId("6"); 
    slr.setTitle("Song Title 2"); 
    slr.setArtist("Artist 2"); 
    slr.setVolume("Vol 2"); 
    slr.setNumber("6"); 
    results.add(slr); 

    slr = new FragmentOne_slResults(); 
    slr.setId("7"); 
    slr.setTitle("Song Title 2 Song Title 2 Title 2 Song Title 2 Song Title 2 Title 23 tle 2 Song Title 2 Song Title 2 Title 2"); 
    slr.setArtist("Artist 3"); 
    slr.setVolume("Vol 3"); 
    slr.setNumber("7"); 
    results.add(slr); 

    slr = new FragmentOne_slResults(); 
    slr.setId("8"); 
    slr.setTitle("Song Title 2"); 
    slr.setArtist("Artist 2"); 
    slr.setVolume("Vol 2"); 
    slr.setNumber("8"); 
    results.add(slr); 

    slr = new FragmentOne_slResults(); 
    slr.setId("9"); 
    slr.setTitle("Song Title 3"); 
    slr.setArtist("Artist 3"); 
    slr.setVolume("Vol 3"); 
    slr.setNumber("9"); 
    results.add(slr); 

    slr = new FragmentOne_slResults(); 
    slr.setId("10"); 
    slr.setTitle("Song Title 2"); 
    slr.setArtist("Artist 2"); 
    slr.setVolume("Vol 2"); 
    slr.setNumber("10"); 
    results.add(slr); 

    slr = new FragmentOne_slResults(); 
    slr.setId("11"); 
    slr.setTitle("Song Title 3"); 
    slr.setArtist("Artist 3"); 
    slr.setVolume("Vol 3"); 
    slr.setNumber("11"); 
    results.add(slr); 

    slr = new FragmentOne_slResults(); 
    slr.setId("12"); 
    slr.setTitle("Song Title 2"); 
    slr.setArtist("Artist 2"); 
    slr.setVolume("Vol 2"); 
    slr.setNumber("12"); 
    results.add(slr); 

    slr = new FragmentOne_slResults(); 
    slr.setId("13"); 
    slr.setTitle("Song Title 3"); 
    slr.setArtist("Artist 3"); 
    slr.setVolume("Vol 3"); 
    slr.setNumber("13"); 
    results.add(slr); 

    slr = new FragmentOne_slResults(); 
    slr.setId("14"); 
    slr.setTitle("Song Title 2"); 
    slr.setArtist("Artist 2"); 
    slr.setVolume("Vol 2"); 
    slr.setNumber("14"); 
    results.add(slr); 

    slr = new FragmentOne_slResults(); 
    slr.setId("15"); 
    slr.setTitle("Song Title 3"); 
    slr.setArtist("Artist 3"); 
    slr.setVolume("Vol 3"); 
    slr.setNumber("15"); 
    results.add(slr); 

    //add results into the database 
    return results; 
} 

所以不是這個 ArrayList<FragmentOne_slResults> slResults = GetSearchResults();替換此與ArrayList<FragmentOne_slResults> slResultsFromDb=[fetch data from database]; ArrayList<FragmentOne_slResults> slResults=new ArrayList<>(); if(slResultsFromDb.size>0){ slResults.addAll(slResultsFromDb); }else slResults.addAll(GetSearchResults());更新數據庫的onclick希望這對你的作品

+0

請參閱更新。我現在使用togglebutton。但是當我點擊一個項目時,一些項目也會變成紅色。然後當我滾動它改變。也當我刷新紅心被刪除。 – Joehamir

+0

我已更新andwer。 –

+0

你能爲我完成代碼嗎?謝謝。 – Joehamir

0

你需要一個布爾添加到您的FragmentOne_slResults這個類會跟蹤對象是否被偏好。不要忘記爲該布爾值添加setter和getter(isFavorite()和setFavorite(boolean))。

此時您必須檢查getView()方法中該布爾值的值,以便您可以根據它是否爲偏好來更改ImageButton的背景圖像。爲了在點擊圖像時改變圖像,還應該在適配器的getView()方法中爲每個心形ImageButton添加一個OnClickListener,該方法將更新該位置數據的偏好狀態並更改圖像按鈕的背景圖像。您getView()方法如下所示:

public View getView(final int position, View convertView, ViewGroup parent) { 
    final ViewHolder holder; 
    if (convertView == null) { 
     convertView = mInflater.inflate(R.layout.fragment_fragment_one_slview, null); 
     holder = new ViewHolder(); 

     holder.txtTitle = (TextView) convertView.findViewById(R.id.title); 
     holder.txtArtist = (TextView) convertView.findViewById(R.id.artist); 
     holder.txtVolume = (TextView) convertView.findViewById(R.id.volume); 
     holder.txtNumber = (TextView) convertView.findViewById(R.id.number); 

     holder.buttonHeart = (ImageButton) convertView.findViewById(R.id.heart); 
     final ImageButton tmp = holder.buttonHeart; 

     holder.btnHeart = (ImageView) convertView.findViewById(R.id.heart); 

     convertView.setTag(holder); 
    } else { 
     holder = (ViewHolder) convertView.getTag(); 
    } 

    holder.txtTitle.setText(oneslArrayList.get(position).getTitle()); 
    holder.txtArtist.setText(oneslArrayList.get(position).getArtist()); 
    holder.txtVolume.setText(oneslArrayList.get(position).getVolume()); 
    holder.txtNumber.setText(oneslArrayList.get(position).getNumber()); 
    // Set the correct heart status based on the favorite status. 
    holder.buttonHeart.setImageResource(oneslArrayList.get(position).isFavorite 
     ? R.drawable.hearted : R.drawable.heart); 
    holder.buttonHeart.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View view) { 
     // Toggle the favorites status of the imageButton and the data. 
     boolean isFavorite = !oneslArrayList.get(position).isFavorite(); 
     oneslArrayList.get(position).setFavorite(isFavorite); 
     holder.buttonHeart.setImageResource(isFavorite ? R.drawable.hearted : R.drawable.heart); 
     } 
    } 

    return convertView; 
    } 

您的類名爲FragmentOne_slResults應該是這樣的:

/** 
* Created by Joe on 6/29/2016. 
*/ 
public class FragmentOne_slResults{ 
    private String id = ""; 
    private String title = ""; 
    private String artist = ""; 
    private String volume = ""; 
    private String number = ""; 
    // this is now private 
    private boolean isFavorite; 

    public void setId(String id) { 
     this.id = id; 
    } 
    public String getId() { 
     return id; 
    } 

    public void setTitle(String title) { 
     this.title = title; 
    } 
    public String getTitle() {return title;} 

    public void setArtist(String artist) { 
     this.artist = artist; 
    } 
    public String getArtist() { 
     return artist; 
    } 

    public void setVolume(String volume) { 
     this.volume = volume; 
    } 
    public String getVolume() { 
     return volume; 
    } 

    public void setNumber(String number) {this.number = number;} 
    public String getNumber() {return number;} 

    public void setIsFavorite(boolean isFavorite) {this.isFavorite = isFavorite;} 
    // The return type of this is boolean, not String 
    public boolean isFavorite() {return isFavorite;} 
} 

此外,清理你的代碼,看看你是否能擺脫所有的呼叫到oneslArrayList.get(position)只需將該項目放入一個變量並重新使用即可。

+0

其中.isFavorite來自哪裏?即時通訊錯誤在這部分。對不起,即時通訊新的android。謝謝。 – Joehamir

+0

您需要將它添加到您的模型對象中以用於'FragmentOne_slResults'我在我的文章的第一句中提到了這一點,但是Ill會回過頭來讓它更加明確。如果沒有這些數據,你的對象不會堅持他們的「偏好狀態」,如果你重新繪製列表,那麼心靈的圖像將會丟失。 –

+0

請參閱編輯。我更新了我的代碼.. – Joehamir

0

1>你是在你的bean或模型類中添加喜歡的歌曲狀態就像下面

FragmentOne_slResults slr = new FragmentOne_slResults(); 
    slr.setId("1"); 
    slr.setTitle("Song Title 1 Song Title 1 Title 2"); 
    slr.setArtist("Artist 1"); 
    slr.setVolume("Vol 1"); 
    slr.setNumber("1"); 
    slr.setIsFavorite(false);//add this variable in your model class 
    results.add(slr); 

2>如果您是喜歡選擇項目,然後更新與真值這個價值,並通知您的適配器 3>下面添加代碼歌曲isFavorite的檢查條件或不

if (oneslArrayList.get(position).isFavorite) { 
    holder.buttonHeart.setImageResource(R.drawable.red_heart); 
    }else{ 
    holder.buttonHeart.setImageResource(R.drawable.white__heart); 
    } 
+0

請參閱編輯,我更新我的代碼。 – Joehamir

+0

@Joehamir謝謝 –

+0

你能完成代碼嗎?請我需要幫助。 – Joehamir