2016-07-01 3 views
0

私を助けてください。Androidスタジオのリストビューアイテムがあります。クリックしてお気に入りに追加します

私の心ImageViewの .IとのListViewは、私は心のimage.thenをクリックすると、赤と心臓のイメージの色を変更したいきた私はブックマークのリストビューのようfavorites.itをクリックし、すべての項目を表示したいですアイテム

ここに出力します。ここで

enter image description here

私のコードです。ここ

FragmentOne

@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.setTitle("Song Title 1 Song Title 1 Title 2"); 
     slr.setArtist("Artist 1"); 
     slr.setVolume("Vol 1"); 
     slr.setNumber("123456"); 
     results.add(slr); 

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

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

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

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

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

     slr = new FragmentOne_slResults(); 
     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("123456"); 
     results.add(slr); 

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

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

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

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

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

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

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

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

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

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

     return results; 
    } 

FragmentOne_Adapter

package com.magicstarme.virtualsongbook; 

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

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; 

    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(int position, View convertView, ViewGroup parent) { 
     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); 

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

     return convertView; 
    } 

    static class ViewHolder { 
     TextView txtTitle; 
     TextView txtArtist; 
     TextView txtVolume; 
     TextView txtNumber; 
    } 
} 

は私を助けてください、XML

<?xml version="1.0" encoding="utf-8"?> 
<!--<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal" 
    android:paddingLeft="10dip" > 
    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal"> 
     <RelativeLayout 
      android:layout_width="200dp" 
      android:layout_height="match_parent" 
      android:id="@+id/relativeLayout1" 
      android:paddingRight="5dip"> 

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

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

         <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="match_parent"> 

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

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

        </FrameLayout> 

      </LinearLayout> 
     </RelativeLayout> 

     <RelativeLayout 
      android:layout_width="65dp" 
      android:layout_height="match_parent" 
      android:layout_alignParentTop="true" 
      android:id="@+id/relativeLayout2" 
      android:layout_toStartOf="@+id/relativeLayout3"> 

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

       <ImageView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:src="@drawable/mic" 
        android:layout_gravity="left|top" /> 

       <ImageView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:src="@drawable/heart" 
        android:layout_gravity="right|top" /> 

      </FrameLayout> 

     </RelativeLayout> 

     <RelativeLayout 
      android:layout_width="80dp" 
      android:layout_height="match_parent" 
      android:background="#cccccc" 
      android:layout_alignParentTop="true" 
      android:layout_alignParentEnd="true" 
      android:id="@+id/relativeLayout3"> 

      <FrameLayout 
       android:layout_width="80dp" 
       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> 

     </RelativeLayout> 
    </LinearLayout> 
</RelativeLayout>--> 

<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"> 

      <ImageView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:src="@drawable/mic" 
       android:layout_gravity="left|top" /> 

      <ImageView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:src="@drawable/heart" 
       android:layout_gravity="right|top" /> 

     </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> 

です。

私はのデジタルソングブックのようなソングブックアプリケーションをプレイストアから開発しています。

おかげで、

ジョー

+0

フラグがtrueの場合はすべてのアイテムに対して1つのフラグを設定し、それ以外の場合はそれを赤にします – PriyankaChauhan

+0

リストアイテムのレイアウトを指定できますか? – babadaba

+0

をご覧ください。誰も私にサンプルコードを与えることができます。私はアンドロイドにちょっと新しいです。あなたが理解して欲しいです。 – Joehamir

答えて

1

は心の色を変更するための私のコードです。あなたのレイアウトのxmlファイルでこれを使用してください

<ToggleButton 
    android:id="@+id/toggleButton" 
    style="@style/toggleButton" 
    android:layout_width="40dp" 
    android:layout_height="40dp" 
    android:layout_gravity="right" 
    android:background="@drawable/favourite_toggle_bg"/> 

スタイルファイルでこれを追加します。

<style name="toggleButton" parent="@android:Theme.Black"> 
    <item name="android:buttonStyleToggle">@style/Widget.Button.Toggle</item> 
    <item name="android:textOn"></item> 
    <item name="android:textOff"></item> 
</style> 

drawableフォルダのfavourite_toggle_bg.xmlに新しいファイルを作成してこのコードを追加してください。

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
     <item android:state_checked="false" 
       android:drawable="@mipmap/favourite_no" /> 
     <item android:state_checked="true" 
       android:drawable="@mipmap/favourite_yes" /> 
    </selector> 

あなたのホルダーにトグルボタンbtnFavouriteを追加して、あなたは、あなたのgetViewメソッドアダプタ方法でこれを追加お気に入りのトグルボタン、チェックしたい場合は、Javaコードでトグルボタン

holder.btnFavourite = (ToggleButton) convertView.findViewById(R.id.toggleButton); 

でこれをinitilize

holder.btnFavourite.setChecked(true); 
+0

このコードに感謝します。これはより良いですが、私は心臓をクリックすると、2つの心臓が赤くなり、私がスクロールすると、他の心臓に移動します。別のものは、私がリフレッシュし、他の断片に行くとき、赤はグレーの心臓に再びなります。私を助けてください。 – Joehamir

+0

こんにちは@asim私を助けてください。どのように設定することができますholder.btnFavourite.setChecked(true);リストビューからデータをリストするとき。 – Joehamir

+0

FragmentOne_slResultsクラスにIsFavouriteブール変数を追加します。それをfalseで初期化し、ユーザーがお気に入りのボタンをクリックするとtrue oneslArrayList.get(position).IsFavourite = trueに設定されます。あなたのgetViewメソッドでは、このIsFavourite値に応じてお気に入りのボタンを次のように変更します。if(IsFavourite?holder.btnFavourite.setChecked(true):holder.btnFavourite.setChecked(false)) – asim

2

あなたはViewholderでのImageButtonとして、それを追加しようとしたし、それのためのgetViewメソッドでSetOnclickListenerを追加することがありますか?

holder.button = (ImageButton)convertView.findviewById(...); 
final ImageButton tmp = holder.button; 
tmp.setOnClickListenet(...) ; 

はEDIT:あなたのXMLで の代わり:

<ImageView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/heart" 
      android:layout_gravity="right|top" /> 

でそれを置き換えます。ここでは

<ImageButton 
android:id < add your ID here !!! > 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/heart" 
      android:layout_gravity="right|top" /> 
+0

ありがとう、私にもっとコードを与えることができますか?申し訳ありませんが、私はアンドロイドに新しいです。あなたが理解して欲しいです。 – Joehamir

+0

心臓のアイコンにXMLのIDを付けます。 ViewHolderのボタンとして宣言します。 getviewはViewholderを使用可能なビューに変換します。そこにアクションを初期化できます。独自の内部ループでオブジェクトを変更することはできないため、最終的なtmpが必要です。 ViewHolderは、getviewの適切なViewに「膨らんだ」テンプレートを取得するだけのテンプレートです。 ViewHolderパターンの使い方を理解するために、ViewHolderパターンについてお読みいただくことをお勧めします。 編集:ちょうどあなたのXMLを見た。ハートアイコンは、ImageViewの代わりにボタンでなければなりません。 – VikingPingvin

+0

ありがとうございます。どのようなコードを 'tmp.setOnClickListenet(...);'の中に追加してイメージビューのsrcをハートからハートに変更する必要がありますか?助けてください。ありがとう。 – Joehamir

関連する問題