Change text in a TextView within a CustomArrayAdapter

1

I have a ListView with a CustomArrayAdapter with checkbox , within Onclick of checkbox in CustomArrayAdapter I want to modify the text of a TextView of Fragment , I tried this but I do not get it, it does not modify the text in 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();


    }

}

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

}

The TextView is declared in the Fragment, it is a normal TextView:

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
    
asked by Fen Dev 28.07.2016 в 20:00
source

2 answers

1

Fix it by calling a function in the Activity container of 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);

    }

}

In the Activity I create the function that modifies the 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);
        }

    }
    
answered by 02.08.2016 / 12:27
source
0

A proposal would be to add a public method in which you change the text of the TextView you want, this within the class Recomendacion :

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

Inside the onClick() method of your Adapter CustomArrayAdapter send the text:

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

With this you can change the text of your TextView within your Fragment Recomendacion .

    
answered by 01.08.2016 в 20:34