2015-12-04 62 views
0

所以我有一個自定義列表適配器,並且我已經瞭解到,將偵聽器附加到該列表中的特定項目是令人難以置信的混淆。我明白ListView可以回收視圖,並且您必須使用持有者和該項目的參考來引用每個項目。當我滾動時,我的EditTexts工作,但我的CheckBoxes採取再循環視圖的價值。這是我的代碼。一切都顯示正確,所以它不是一個XML問題。EditText在ListView中正常工作,但複選框沒有

public void changeMate(View view) { 
    View parent = (View)view.getParent(); 
    CheckBox box = (CheckBox)view; 
    CheckBox mate; 
    if (view.getId()==R.id.trueBox) mate = (CheckBox) parent.findViewById(R.id.falseBox); 
    else mate = (CheckBox) parent.findViewById(R.id.trueBox); 
    mate.setChecked(!box.isChecked()); 
    TextView label = (TextView)parent.findViewById(R.id.atomName); 
    // MainActivity.atoms[(int)label.getText().charAt(0)-'A'].immutableTruth=box.getId()==R.id.trueBox; 
    MainActivity.atoms[(int)label.getText().charAt(0)-'A'].immutableTruth=((CheckBox)findViewById(R.id.trueBox)).isChecked(); 
    MainActivity.atoms[(int)label.getText().charAt(0)-'A'].valueSet=true; 
    c.put((int)label.getText().charAt(0)-'A',MainActivity.atoms[(int)label.getText().charAt(0)-'A'].immutableTruth); 


} 

public void kill(View view) { 
    try { 
     finish(); 
    } catch (Throwable throwable) { 
     throwable.printStackTrace(); 
    } 
} 

public void handleChanges(View view) { 
} 


static class AtomViewer extends ArrayAdapter<Atom> { 
private ArrayList<Atom> objects; 

public AtomViewer(Context context, int resource, ArrayList objects) { 
    super(context, resource, objects); 
    this.objects = objects; 
} 
    @Override 
     public View getView(int position, View convertView, final ViewGroup parent) { 

// assign the view we are converting to a local variable 
View v = convertView; 
final ViewHolder holder; 

// first check to see if the view is null. if so, we have to inflate it. 
// to inflate it basically means to render, or show, the view. 
if (v == null) { 
    holder=new ViewHolder(); 

    LayoutInflater inflater = (LayoutInflater) `enter code here` getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    convertView = inflater.inflate(R.layout.atom_view, null); 
holder.editText1= (EditText) convertView.findViewById(R.id.definition); 
    convertView.setTag(holder); 
} else holder= (ViewHolder) convertView.getTag(); 
v=convertView; 
holder.ref=position; 
CheckBox tb = (CheckBox) v.findViewById(R.id.trueBox); 
CheckBox fb = (CheckBox) v.findViewById(R.id.falseBox); 
holder.tb=tb; 
holder.fb=fb; 

TextView name = (TextView) v.findViewById(R.id.atomName); 
Atom i = objects.get(position); 
holder.editText1.setText(e.valueAt(position)); 
holder.editText1.addTextChangedListener(new TextWatcher() { 
    @Override 
    public void beforeTextChanged(CharSequence s, int start, int count, int after) { 

    } 

    @Override 
    public void onTextChanged(CharSequence s, int start, int before, int count) { 

    } 

    @Override 
    public void afterTextChanged(Editable s) { 
     e.setValueAt(holder.ref, s); 

    } 
}); 
{ 
    if (!i.valueSet) 
    { 
     holder.fb.setChecked(!i.truth); 
     holder.tb.setChecked(i.truth); 
    } 
    boolean truth = c.get(position); 
    if (truth&&i.valueSet) { 
     holder.fb.setChecked(false); 
     holder.tb.setChecked(true); 

    } 
    else if(!truth) 
    { 
     holder.fb.setChecked(true); 
     holder.tb.setChecked(false); 
    } 
    CharSequence t = String.valueOf(i.getname()); 
    name.setText(t); 
    if (i.dictionaryValue != null) ``holder.editText1.setText(i.dictionaryValue); 
    } 
`return v; 

} 




} 

public static Atom[] atoms; 
public ArrayList<Atom> mParts = new ArrayList<Atom>(); 
public AtomViewer mAdapter; 
public ListView listView; 
public static SparseArray<Editable> e = new SparseArray<>(26); 
public static SparseBooleanArray c = new SparseBooleanArray(26); 


@Override 
protected void onCreate(Bundle stuff) { 
    super.onCreate(null); 


    setContentView(R.layout.activity_info); 
    Intent i = getIntent(); 
    getWindow().getDecorView().setSaveEnabled(false); 
    Bundle b=i.getExtras(); 
    atoms=MainActivity.atoms; 
    String arg = b.getString("Argument"); 
    String conc = b.getString("Conclusion"); 
    TextView tv = (TextView) findViewById(R.id.argText); 
    tv.setText(arg); 
    tv = (TextView) findViewById(R.id.concText); 
    tv.setText(conc); 
    tv=(TextView)findViewById(R.id.valid); 
    tv.setText(b.getString("Validity")); 
    tv=(TextView)findViewById(R.id.sound); 
    tv.setText(b.getString("Soundness")); 
    //int index = 0; 

    for (Atom a:atoms 
     ) { 
    if(a!=null) { 
     if(a.valueSet) c.put(a.ID,a.immutableTruth); else c.put(a.ID,a.truth); 
     mParts.add(a); 


    } 


    } 
    ArrayAdapter<Atom> arrayAdapter = new            `enter code here`AtomViewer(this,R.layout.atom_view,mParts); 
    listView= (ListView) findViewById(R.id.list); 
    listView.setAdapter(arrayAdapter); 
    listView.getCheckedItemPositions(); 


    } 
private static class ViewHolder { 
    EditText editText1; 
    CheckBox tb,fb; 
    int ref; 
} 
} 

回答

0

編輯你的代碼,移動

holder.ref=position; 
CheckBox tb = (CheckBox) v.findViewById(R.id.trueBox); 
CheckBox fb = (CheckBox) v.findViewById(R.id.falseBox); 
holder.tb=tb; 
holder.fb=fb; 
TextView name = (TextView) v.findViewById(R.id.atomName); 

內,如果(V == NULL)

+0

沒有幫助,雖然我明白爲什麼它是正確的。 – samdoj

相關問題