2017-04-09 36 views
0

我有一個列表視圖中有兩個圖像按鈕在行中。點擊圖像按鈕更改背景顏色。當我點擊第一個列表項時,圖像按鈕背景變化和視圖被保存,但滾動列表視圖的底部,另一列表項的圖像按鈕的背景顏色也會改變。下面是自定義適配器的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

中的其他部分添加代碼崩潰我的應用程序。對於兩個圖像按鈕,默認顏色都是空的。我想僅突出顯示按下的按鈕而不是其他按鈕。我的列表視圖行由textview和2 imagebuttons組成..完整的customadapter代碼位於:[link](https://pastebin.com/SbTKWKLB) –

+0

我從你的代碼中得到了這個想法,但仍然沒有解決問題..上面的代碼返回錯誤和期望:'最終布爾[] presentButton =新布爾[1];' 當我運行聲明imagebutton布爾到最終數組,沒有按鈕突出顯示後點擊它.. :( –

+0

謝謝你支持我,但是,我仍然遇到你提供的指南問題,所有列表項目中的一個imageButton被選中onScroll。我在[code](https://pastebin.com/Uupxwt0q)上更改了vew行,但它給了我相同的錯誤以前。 –