2017-08-31 80 views
0

我在回收站視圖中有切換按鈕我在sharedpreference中保存布爾值,以識別是否在應用程序的每次重新啓動時按下切換按鈕現在問題是 當我點擊1切換按鈕和關閉應用程序的每個切換爲脫離按鈕同樣的事情按鈕那裏是切換按鈕和回收視圖之間沒有位置連接,這裏是我的代碼在RecyclerView中切換按鈕位置

@Override 
public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
    View v= LayoutInflater.from(parent.getContext()).inflate(R.layout.model,null); 

    MyHolder holder=new MyHolder(v); 
    SharedPreferences sharedPrefs = c.getSharedPreferences("lol", MODE_PRIVATE); 
    Boolean a = sharedPrefs.getBoolean("abc" , false); 
    if(a) { 
     holder.fav.setBackgroundDrawable(ContextCompat.getDrawable(c, R.drawable.star_light)); 
     holder.fav.setChecked(true); 
    } else { 
     holder.fav.setBackgroundDrawable(ContextCompat.getDrawable(c, R.drawable.star_off)); 
     holder.fav.setChecked(false); 
    } 
    return holder; 
} 

@Override 
public void onBindViewHolder(final MyHolder holder, final int position) { 
    holder.nameTxt.setText(players[position]); 
    holder.posTxt.setText(positions[position]); 
    holder.img.setImageResource(images[position]); 
    holder.fav.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
     @Override 
     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
      if(isChecked) { 
       holder.fav.setBackgroundDrawable(ContextCompat.getDrawable(c,R.drawable.star_light)); 
      SharedPreferences.Editor editor = c.getSharedPreferences("lol", MODE_PRIVATE).edit(); 
      editor.putBoolean("abc", true); 
      editor.commit(); 
      } else { 
       holder.fav.setBackgroundDrawable(ContextCompat.getDrawable(c, R.drawable.star_off)); 
       SharedPreferences.Editor editor = c.getSharedPreferences("lol", MODE_PRIVATE).edit(); 
       editor.putBoolean("abc", false); 
       editor.commit(); 
      } 
     } 
    }); 
+0

好吧,你是正確的。但是你的問題是什麼? –

+0

您使用的邏輯錯誤。您只使用一個SharedPreferences'abc'值來切換所有按鈕。因此,如果一個按鈕有效,它就會反映出來。 –

+0

@ josef.adamcik當我點擊一個切換按鈕並重新啓動應用程序時,所有的按鈕都變成了他們一樣 – Eazyz

回答

0

它的工作謝謝你,這一切都是正確的答案

@Override 
public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
    View v= LayoutInflater.from(parent.getContext()).inflate(R.layout.model,null); 

    MyHolder holder=new MyHolder(v); 

    return holder; 
} 


@Override 
public void onBindViewHolder(final MyHolder holder, final int position) { 


    holder.nameTxt.setText(players[position]); 
    holder.posTxt.setText(positions[position]); 
    holder.img.setImageResource(images[position]); 
    SharedPreferences sharedPrefs = c.getSharedPreferences("lol", MODE_PRIVATE); 
    Boolean a = sharedPrefs.getBoolean("abc" + position, false); 
    if (a){ 
     holder.fav.setBackgroundDrawable(ContextCompat.getDrawable(c, R.drawable.star_light)); 
     holder.fav.setChecked(true); 

    }else { 
     holder.fav.setBackgroundDrawable(ContextCompat.getDrawable(c, R.drawable.star_off)); 
     holder.fav.setChecked(false); 

    } 


    holder.fav.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
     @Override 
     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 

      if (isChecked){ 
       holder.fav.setBackgroundDrawable(ContextCompat.getDrawable(c,R.drawable.star_light)); 
      SharedPreferences.Editor editor = c.getSharedPreferences("lol", MODE_PRIVATE).edit(); 
      editor.putBoolean("abc" + position, true); 
      editor.commit(); 
      } 
      else{ 
       holder.fav.setBackgroundDrawable(ContextCompat.getDrawable(c, R.drawable.star_off)); 
       SharedPreferences.Editor editor = c.getSharedPreferences("lol", MODE_PRIVATE).edit(); 
       editor.putBoolean("abc" + position, false); 
       editor.commit(); 

      } 
     } 
    }); 
0

你的實現是錯誤的。

您應該只使用onCreateViewHolder()來擴大布局。

綁定操作應該在onBindViewHolder()。在onBindViewHolder()使用切換操作

+0

我試圖在onBindeView裏添加相同的代碼,但是結果相同 – Eazyz

0

我做了一個快速編輯。請詳細檢查。我在SharedPreferences中使用了position。所以每個toggle button值都保存在不同的密鑰中。這可能工作。

SharedPreferences.Editor editor = c.getSharedPreferences("lol", MODE_PRIVATE).edit(); 

@Override 
public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
    View v= LayoutInflater.from(parent.getContext()).inflate(R.layout.model,null); 

    MyHolder holder=new MyHolder(v); 

    return holder; 
} 


@Override 
public void onBindViewHolder(final MyHolder holder, final int position) { 


    holder.nameTxt.setText(players[position]); 
    holder.posTxt.setText(positions[position]); 
    holder.img.setImageResource(images[position]); 

    SharedPreferences sharedPrefs = c.getSharedPreferences("lol", MODE_PRIVATE); 
     Boolean a = sharedPrefs.getBoolean("abc"+position , false); 
     if (a){ 
      holder.fav.setBackgroundDrawable(ContextCompat.getDrawable(c, R.drawable.star_light)); 
      holder.fav.setChecked(true); 

     }else { 
      holder.fav.setBackgroundDrawable(ContextCompat.getDrawable(c, R.drawable.star_off)); 
      holder.fav.setChecked(false); 

     } 

    holder.fav.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
     @Override 
     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 

      if (isChecked){ 
       holder.fav.setBackgroundDrawable(ContextCompat.getDrawable(c,R.drawable.star_light)); 

      editor.putBoolean("abc"+position, true); 
      editor.commit(); 
      } 
      else{ 
       holder.fav.setBackgroundDrawable(ContextCompat.getDrawable(c, R.drawable.star_off)); 

       editor.putBoolean("abc"+position, false); 
       editor.commit(); 

      } 
     } 
    }); 
+0

這段代碼給了我NullPointerException – Eazyz

+1

我想你有解決方案! –