【问题标题】:EditText works correctly in ListView but CheckBoxes do notEditText 在 ListView 中正常工作,但 CheckBoxes 不能
【发布时间】:2015-12-04 17:15:50
【问题描述】:

所以我有一个自定义列表适配器,并且我了解到将侦听器附加到该列表中的特定项目非常令人困惑。我了解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;
}
}

【问题讨论】:

    标签: java android listview checkbox


    【解决方案1】:

    编辑你的代码,移动

    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 (v == null) 内

    【讨论】:

      猜你喜欢
      • 2016-04-26
      • 2013-06-29
      • 2021-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多