2015-05-22 64 views
0

我的listview項每次頁面加載時都會重複。我認爲我的adapter課程中有錯誤。請幫忙。先謝謝你。Android重複列表視圖項

public class MyAdapter extends ArrayAdapter<Model> { 

    private final List<Model> list; 
    private final Activity context; 
    boolean checkAll_flag = false; 
    boolean checkItem_flag = false; 

    public MyAdapter(Activity context, List<Model> list) { 
     super(context, R.layout.rows, list); 
     this.context = context; 
     this.list = list; 
    } 

    static class ViewHolder { 
     protected TextView text; 
     protected CheckBox checkbox; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 

     ViewHolder viewHolder = null; 
     if (convertView == null) { 
      LayoutInflater inflator = context.getLayoutInflater(); 
      convertView = inflator.inflate(R.layout.rows, null); 
      viewHolder = new ViewHolder(); 
      viewHolder.text = (TextView) convertView.findViewById(R.id.label); 
      viewHolder.checkbox = (CheckBox) convertView.findViewById(R.id.check); 
      viewHolder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 

       @Override 
       public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
        int getPosition = (Integer) buttonView.getTag(); // Here we get the position that we have set for the checkbox using setTag. 
        list.get(getPosition).setSelected(buttonView.isChecked()); // Set the value of checkbox to maintain its state. 
       } 
      }); 
      convertView.setTag(viewHolder); 
      //convertView.setTag(R.id.label, viewHolder.text); 
      //convertView.setTag(R.id.check, viewHolder.checkbox); 
      } else { 
      viewHolder = (ViewHolder) convertView.getTag(); 
     } 
     viewHolder.checkbox.setTag(position); // This line is important. 

     viewHolder.text.setText(list.get(position).getName()); 
     viewHolder.checkbox.setChecked(list.get(position).isSelected()); 

     return convertView; 
    } 
} 

我填這個功能,我在片段的onCreate方法

私人列表getModel(){

list.clear(); 
    for(int i=0;i<list1.size();i++){ 
     String a=list1.get(i); 
     list.add(new Model(a)); 
    } 
    return list; 
} 

我打電話名單這是我的片段類

public class VaccAdmin extends FragmentActivity implements OnItemClickListener{ 
private Database dbHelper;  
private double x,y; 
ListView listView; 
ArrayAdapter<Model> adapter; 
List<Model> list = new ArrayList<Model>(); 
List<String> list1=new ArrayList<String>(); 

public void onCreate(Bundle icicle) { 
    super.onCreate(icicle); 
    setContentView(R.layout.list); 
    dbHelper=new Database(this); 
    dbHelper.addVaccine(); 
    listView = (ListView) findViewById(R.id.my_list); 

    String age = getIntent().getStringExtra("age"); 
    System.out.println(age+"Ageinmo"); 
    double ageinmo=Double.parseDouble(age); 
    if(ageinmo<=1.5){ 
     x=0.0; 
     y=1.5; 
    } else if(ageinmo<=2.25){ 
     x=1.75; 
     y=2.25; 
    } else if(ageinmo<=4.25){ 
     x=3.5; 
     y=4.25; 
    }else if(ageinmo<=6.0){ 
     x=4.5; 
     y=6.0; 
    }else if(ageinmo<=9.0){ 
     x=6.25; 
     y=9.0; 
    }else if(ageinmo<=12.0){ 
     x=9.25; 
     y=12.0; 
    }else if(ageinmo<=15.0){ 
     x=12.25; 
     y=15.0; 
    }else if(ageinmo<=18.0){ 
     x=15.25; 
     y=18.0; 
    }else if(ageinmo<=23.0){ 
     x=18.25; 
     y=23.0; 
    }else if(ageinmo<=72.0){ 
     x=36.25; 
     y=72.0; 
    }else if(ageinmo<=120.0){ 
     x=72.25; 
     y=120.0; 
    }else if(ageinmo<=144.0){ 
     x=120.25; 
     y=144.0; 
    }else if(ageinmo<=216.0){ 
     x=144.25; 
     y=216.0; 
    } 
    list1=dbHelper.getVacNames(x, y); 
    int size=list1.size(); 
    System.out.println("Size"+size); 
    adapter = new MyAdapter(this,getModel()); 
    listView.setAdapter(null); 
    listView.setAdapter(adapter); 
    listView.setOnItemClickListener(this); 
} 

@Override 
public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3) { 
      TextView label = (TextView) v.getTag(R.id.label); 
CheckBox checkbox = (CheckBox) v.getTag(R.id.check); 
Toast.makeText(v.getContext(), label.getText().toString()+" "+isCheckedOrNot(checkbox), Toast.LENGTH_LONG).show(); 
} 

private String isCheckedOrNot(CheckBox checkbox) { 
    if(checkbox.isChecked()) 
    return "is checked"; 
    else 
    return "is not checked"; 
} 


private List<Model> getModel() { 

    list.clear(); 
    for(int i=0;i<list1.size();i++){ 
     String a=list1.get(i); 
     list.add(new Model(a)); 
    } 
    return list; 
} 

}

+0

都必須有你的清單後全碼 –

+0

嘗試寫convertView =無效的問題;在ViewHolder聲明之上viewHolder = null;並檢查出來。 –

+0

在您的活動課程中發佈您如何呼叫您的適配器,以及如何填充列表中的數據。 – Soham

回答

0

我相信這應該outide convertview == NULL被寫入

viewHolder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 

       @Override 
       public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
        int getPosition = (Integer) buttonView.getTag(); // Here we get the position that we have set for the checkbox using setTag. 
        list.get(getPosition).setSelected(buttonView.isChecked()); // Set the value of checkbox to maintain its state. 
       } 
      }); 
+0

它給出了偵聽器上的空指針異常,如果我在外寫入它 –

+0

viewHolder.checkbox.setTag(position); //在convertview內部執行== null –

+0

nope不工作仍然重複值 –