So I have a custom list adapter, and I've learned that attaching listeners to particular items in that list is incredibly confusing. I understand that ListView
recycles views, and that you must reference each item using a holder and a reference for that item. My EditTexts
work when I scroll, but my CheckBoxes
take the value of recycled views. Here is my code. Everything displays correctly, so it's not an XML issue.
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;
}
}
Aucun commentaire:
Enregistrer un commentaire