EditText在ListView中正常工作但CheckBoxes没有

时间:2015-12-04 08:38:34

标签: java android listview checkbox

所以我有一个自定义列表适配器,并且我已经了解到将监听器附加到该列表中的特定项目是非常令人困惑的。我了解ListView回收视图,并且您必须使用持有者和该项目的参考来引用每个项目。滚动时我的EditTexts工作,但我的CheckBoxes采用了回收视图的值。这是我的代码。一切都正确显示,因此它不是XML问题。

 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;
}
}

1 个答案:

答案 0 :(得分: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);

if if(v == null)