2017-04-09 14 views
0

私は行に2つの画像ボタンを持つlistviewを持っています。タップ画像ボタンは背景色を変更します。最初のリスト項目をタップすると画像ボタンの背景が変わり、ビューが保存されますがリストビューの下をスクロールすると、別のリスト項目の画像ボタンの背景色も変わります。以下はカスタムアダプタのgetViewです。どうすればこの問題を回避できますか?getviewとの混乱android imagebuttons

あなたは以下を参照してください、あなたのコードに応じて、他を追加する必要が
public View getView(final int i, View view, ViewGroup viewGroup) { 
    ViewHolder holder = new ViewHolder(); 
    holder = null; 

    //view=null; 
    if (inflater == null) { 
     inflater = (LayoutInflater) activity 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

    } 
    if (view == null) { 
     view = inflater.inflate(R.layout.student_list, null); 

     holder = new ViewHolder(); 

     holder.presentButton = (ImageButton) view.findViewById(R.id.imageView); 
     holder.absentButton = (ImageButton) view.findViewById(R.id.imageView2); 
     holder.presentButton.setBackgroundColor(0); 
     holder.absentButton.setBackgroundColor(0); 


     view.setTag(holder); 

    } 
    else { 
     holder = (ViewHolder) view.getTag(); 

    } 

    final SQLiteStudents db1 = new SQLiteStudents(activity.getApplicationContext()); 
    final TextView tvName = (TextView) view.findViewById(R.id.tv_name); 
    final TextView tvRoll = (TextView) view.findViewById(R.id.tv_roll); 
    final studentInfo s = students.get(i); 
    tvRoll.setText(s.getRoll() + "."); 
    tvName.setText(s.getName()); 
    final Integer roll = Integer.parseInt(s.getRoll()); 
    //ivpresent.setBackgroundColor(0); 
    final ViewHolder finalHolder1 = holder; 
    //final ViewHolder finalHolder = holder; 
    holder.presentButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      db1.updateUser(roll,"present"); 


      finalHolder1.presentButton.setBackgroundColor(GREEN); 
      finalHolder1.absentButton.setBackgroundColor(0); 
      //v1.setTag(v.getTag()); 
      //Log.d("present","Roll No: "+String.valueOf(roll)); 
      finalHolder1.presentButton.setTag(Integer.toString(i)); 
      notifyDataSetChanged(); 

     } 
    }); 
    holder.absentButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      db1.updateUser(roll,"absent"); 
      finalHolder1.presentButton.setBackgroundColor(0); 
      finalHolder1.absentButton.setBackgroundColor(RED); 
      //view=null; 
      //Log.d("absent","Roll No: "+String.valueOf(roll)); 
      finalHolder1.presentButton.setTag(Integer.toString(i)); 
      notifyDataSetChanged(); 
     } 
    }); 


    return view; 

} 
public static class ViewHolder { 
    public ImageButton presentButton; 
    public ImageButton absentButton; 
} 

答えて

0
public class CustomListAdapter extends BaseAdapter { 
    private Activity activity; 
    private LayoutInflater inflater; 
    private List<studentInfo> students; 
    private boolean presentButton = false; 
    private boolean absentButton = false; 

public View getView(final int i, View view, ViewGroup viewGroup) { 
    ViewHolder holder = new ViewHolder(); 
    holder = null; 

    //view=null; 
    if (inflater == null) { 
     inflater = (LayoutInflater) activity 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

    } 
    if (view == null) { 
     view = inflater.inflate(R.layout.student_list, null); 

     holder = new ViewHolder(); 

     holder.presentButton = (ImageButton) view.findViewById(R.id.imageView); 
     holder.absentButton = (ImageButton) view.findViewById(R.id.imageView2); 
     holder.presentButton.setBackgroundColor(0); 
     holder.absentButton.setBackgroundColor(0); 


     view.setTag(holder); 

    } 
    else { 
     holder = (ViewHolder) view.getTag(); 

    } 

    final SQLiteStudents db1 = new SQLiteStudents(activity.getApplicationContext()); 
    final TextView tvName = (TextView) view.findViewById(R.id.tv_name); 
    final TextView tvRoll = (TextView) view.findViewById(R.id.tv_roll); 
    final studentInfo s = students.get(i); 
    tvRoll.setText(s.getRoll() + "."); 
    tvName.setText(s.getName()); 
    final Integer roll = Integer.parseInt(s.getRoll()); 
    //ivpresent.setBackgroundColor(0); 
    final ViewHolder finalHolder1 = holder; 
    //final ViewHolder finalHolder = holder; 
    holder.presentButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      db1.updateUser(roll,"present"); 
      presentButton = true; 
     } 
    }); 
    holder.absentButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      db1.updateUser(roll,"absent"); 
      absentButton = true; 

     } 
    }); 

    if(presentButton){ 
    finalHolder1.presentButton.setBackgroundColor(GREEN); 
      finalHolder1.absentButton.setBackgroundColor(0); 
      //v1.setTag(v.getTag()); 
      //Log.d("present","Roll No: "+String.valueOf(roll)); 
      finalHolder1.presentButton.setTag(Integer.toString(i)); 
      notifyDataSetChanged(); 
    } else { 
    //set button to some default button like black 
    } 

    if(absentButton){ 
    finalHolder1.presentButton.setBackgroundColor(0); 
      finalHolder1.absentButton.setBackgroundColor(RED); 
      //view=null; 
      //Log.d("absent","Roll No: "+String.valueOf(roll)); 
      finalHolder1.presentButton.setTag(Integer.toString(i)); 
      notifyDataSetChanged(); 
    } else { 
    //set button to some default button like black 
    } 


    return view; 

} 
+0

他のセクションにコードを追加するには、自分のアプリケーションがクラッシュします。デフォルトの色は、両方の画像ボタンではnullです。私は、押されたボタンだけを強調したいのですが、listviewの他のボタンでは強調しません。私のリストビューの行は、textviewと2つのimagebuttonsで構成されています.. customadapterのフルコード:[リンク](https://pastebin.com/SbTKWKLB) –

+0

私はあなたのコードからアイデアを得るが、まだ解決していない問題..上記のコードを返します最終的なboolean [] presentButton =新しいブール値[1]; ' 最終的な配列にimagebutton boolを宣言して実行すると、ボタンをクリックしても強調表示されない.. :( –

+0

私をサポートしてくれてありがとう、私はまだあなたが提供したガイドに問題があります。すべてのリスト項目のimageButtonの1つがonScrollで選択されています。[code](https://pastebin.com/Uupxwt0q)のvew行を変更しました。前。 –