1

Tengo un ListView con un CustomArrayAdapter con checkbox , dentro del Onclick del checkbox en el CustomArrayAdapter quiero modificar el texto de un TextView del Fragment , he intentado esto pero no lo consigo, no me modifica el texto en el Fragment :

 public class CustomArrayAdapter extends ArrayAdapter<Row> implements View.OnClickListener{

    private LayoutInflater layoutInflater;
    TextView crec;
private Context mcontext;

    public CustomArrayAdapter(Context context, List < Row > objects)
    {
        super(context, 0, objects);
        layoutInflater = LayoutInflater.from(context);
        this.mcontext = context;

    }

    @Override
    public View getView ( int position, View convertView, ViewGroup parent)
    {
        // holder pattern
        Holder holder = null;
        if (convertView == null) {
            holder = new Holder();


            convertView = layoutInflater.inflate(R.layout.listadapter_multiple, null);
            View convertView2 = layoutInflater.inflate(R.layout.fragment_recomendacion, null);

            holder.setTextViewTitle((TextView) convertView.findViewById(R.id.textViewTitle));
            holder.setTextViewSubtitle((TextView) convertView.findViewById(R.id.textViewSubtitle));
            holder.setCheckBox((CheckBox) convertView
                    .findViewById(R.id.checkBox));
            holder.helloTxt= (TextView) convertView2.findViewById(R.id.crec);


            convertView.setTag(holder);


        } else {
            holder = (Holder) convertView.getTag();
        }

        final Row row = getItem(position);
        holder.getTextViewTitle().setText(row.getTitle());
        holder.getTextViewSubtitle().setText(row.getSubtitle());
        holder.getCheckBox().setTag(position);
        holder.getCheckBox().setChecked(row.isChecked());
        holder.getCheckBox().setOnClickListener(this);




        return convertView;
    }


@Override
public void onClick(View v) {
    Holder holder = new Holder();

    CheckBox checkBox = (CheckBox) v;
    int position = (Integer) v.getTag();
    final Row row = getItem(position);

    if(checkBox.isChecked()) {
       holder.helloTxt.setText("eltexto");
        notifyDataSetChanged();


    }

}

Mi Holder :

  static class Holder{

    TextView textViewTitle;
    TextView textViewSubtitle;
    CheckBox checkBox;
    public TextView helloTxt;

    public TextView getTextViewTitle()
    {
        return textViewTitle;
    }

    public void setTextViewTitle(TextView textViewTitle)
    {
        this.textViewTitle = textViewTitle;
    }
    public TextView getTextViewSubtitle()
    {
        return textViewSubtitle;
    }

    public void setTextViewSubtitle(TextView textViewSubtitle)
    {
        this.textViewSubtitle = textViewSubtitle;
    }
    public CheckBox getCheckBox()
    {
        return checkBox;
    }
    public void setCheckBox(CheckBox checkBox)
    {
        this.checkBox = checkBox;
    }

}

El TextView esta declarado en el Fragment , es un TextView normal:

public class Recomendacion extends Fragment {
   ...
   TextView crec;
   //codigo
   @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
       ...
       crec = (TextView) view.findViewById(R.id.crec);
       //codigo
3
  • Fen Dev, por faor agrega tu clase Holder y especifica con una imagen o a más detalle que TextView deseas cambiar el texto
    – Jorgesys
    el 28 jul. 2016 a las 20:30
  • Ya está agregado al final del todo
    – Fen Dev
    el 30 jul. 2016 a las 15:27
  • Disculpa Fen Dev estaba con mucho trabajo, he agregado una respuesta!, dime que resultados tienes, saludos.
    – Jorgesys
    el 1 ago. 2016 a las 18:35

2 respuestas 2

1

Lo solucione llamando una función en la Activity contenedor del Fragment :

@Override
public void onClick(View v) {
    Holder holder = new Holder();

    CheckBox checkBox = (CheckBox) v;
    int position = (Integer) v.getTag();
    final Row row = getItem(position);

    if(checkBox.isChecked()) {

        String retur = ((Principal) getContext()).Contador(true);

    }

}

En la Activity creo la función que modifica el TextView :

 public String Contador(boolean foo){
    TextView t = (TextView) fragmentView.findViewById(R.id.textview);
    Fragment someFragment = getSupportFragmentManager().findFragmentById(R.id.output);
    View fragmentView = someFragment.getView();
    String result = "Texto";

    if (foo) {

            t.setText(result);
        }

    }
0

Una propuesta sería agregar un método publico en el cual cambies el texto del TextView que deseas, esto dentro de la clase Recomendacion :

public void cambiaTexto(String Texto){
  //Cambia texto en TextView.
  crec.setText(Texto);
}

Dentro del método onClick() de tu Adapter CustomArrayAdapter envia el texto:

@Override
public void onClick(View v) {
   CustomArrayAdapter.cambiaTexto("eltexto");
   ...
   ...
}

Con esto podrás cambiar el texto e tu TextView dentro de tu Fragment Recomendacion.

1
  • Lo probé me seteaba la variable pero no me refleja el cambio en el TextView , me pasa lo mismo que con la solución anterior que tenia.
    – Fen Dev
    el 2 ago. 2016 a las 10:28

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.