I have i ListView that is made like this:
CheckBox - ImageView - TextView - EditText
Now the listView works it remeber the entries and it doesn't mix editText when scrolling, but now i can't do the same with checkbox. Checkboxes do not want to disable the right editext and the same when they need to renable it. I've tried a lot of possibilities but i have no success, what i'am doing wrong?
package it.polito.mad17.viral.sliceapp;
import android.app.Activity;
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
/**
* Created by abdel on 03/04/2017.
*/
public class CheckBoxTextViewAdapter extends ArrayAdapter<Persona> {
private int layoutResourceId;
private List<Persona> memberNames;
private boolean[] checkMarks;
private Float[] percentages;
private Context context;
private String value;
private Gruppo gruppo;
private EditText et;
private PayerHolder holder = null;
private CheckBox cb;
public CheckBoxTextViewAdapter(Context context, int layoutResourceId, List<Persona> memberNames, Gruppo gruppo) {
super(context,layoutResourceId,memberNames);
this.memberNames = memberNames;
this.checkMarks = new boolean[memberNames.size()];
this.percentages = new Float[memberNames.size()];
for(int i=0;i<memberNames.size();i++) {
checkMarks[i] = true;
percentages[i] = 0.0f;
}
this.context = context;
this.layoutResourceId=layoutResourceId;
this.gruppo=gruppo;
}
@Override
public View getView(final int position, View view, ViewGroup parent){
View row = view;
if(row == null){
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new PayerHolder();
TextView tv = (TextView) row.findViewById(R.id.member_name);
holder.name=tv;
cb = (CheckBox) row.findViewById(R.id.check);
holder.cb = cb;
et = (EditText)row.findViewById(R.id.member_percentage);
holder.percentage=et;
row.setTag(holder);
}
else holder = (PayerHolder) row.getTag();
cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
et.setEnabled(true);
checkMarks[position] = true;
} else {
holder.percentage.setEnabled(false);
et.setEnabled(false);
checkMarks[position] = false;
et.setText("");
percentages[position]=new Float (0);
}
}
});
holder.cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
et.setEnabled(true);
checkMarks[position] = true;
} else {
holder.percentage.setEnabled(false);
et.setEnabled(false);
checkMarks[position] = false;
et.setText("");
percentages[position]=new Float (0);
}
}
});
holder.percentage.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){
String st = holder.percentage.getText().toString();
if(!st.equals(""))
percentages[position] = Float.parseFloat(st);
else
percentages[position] = 0.0f;
}
});
holder.cb.setTag(position);
cb.setTag(position);
cb.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if(!hasFocus){
final int position = (Integer)v.getTag();
CheckBox Caption = (CheckBox) v;
if(!Caption.isChecked()) {
holder.percentage.setEnabled(false);
holder.percentage.setText("");
percentages[position]=new Float (0);
}else
holder.percentage.setEnabled(true);
}
}
});
holder.cb.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if(!hasFocus){
final int position = (Integer)v.getTag();
CheckBox Caption = (CheckBox) v;
if(!Caption.isChecked()) {
holder.percentage.setEnabled(false);
holder.percentage.setText("");
percentages[position]=new Float (0);
}else
holder.percentage.setEnabled(true);
}
}
});
holder.name.setText(memberNames.get(position).getName()+ " " +memberNames.get(position).getSurname());
holder.percentage.setTag(position);
holder.percentage.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if(!hasFocus){
final int position = (Integer)v.getTag();
EditText Caption = (EditText) v;
if(!Caption.getText().toString().equals(""))
percentages[position] = Float.parseFloat(Caption.getText().toString());
else
percentages[position] = new Float(0);
}
}
});
return row;
}
public boolean[] getChecks(){ return checkMarks; }
public Float[] getPercentages() {
Float[] percentages_ordinato = new Float[gruppo.getN_partecipanti()];
int k=0;
Integer i= 0;
for(Persona p: memberNames){
i=p.getPosizione(gruppo);
percentages_ordinato[i.intValue()]=percentages[k];
k++;
}
return percentages_ordinato;}
static class PayerHolder{
public CheckBox cb;
TextView name;
EditText percentage;
}
@Override
public int getViewTypeCount() {
return getCount();
}
@Override
public int getItemViewType(int position) {
return position;
}
}
i've set the listener on both the checkbox because i want to try what will happen, but i'have got the same result. This is my ArrayAdapter for the ListView Thanks
Aucun commentaire:
Enregistrer un commentaire