Changing the text of a Textview that is inside an Arrayadapter entry

Asked

Viewed 484 times

3

I made a Adapter that extends of ArrayAdapter. My layout contains only a series of TextViews, one below the other, very simple.

I want some event in the system to be watched by my Adapter and that a certain item in the list is changed. I am using the EventBus.

In the builder of my Adapter i use EventBus.getDefault().register(this);

The Adapter has a way onEvent(MessageRunTask event){...}

I created a Job to run and test, it runs EventBus.getDefault().post(new MessageRunTask(r)); when it ends, and using the Log.i I can confirm that the medoto onEvent of Adapter is running successfully.

What I can’t do is:

In the way onEvent, how do I take item X from the list and update the property Text of TextView Y that is inside that item X?

UPDATE 1:

Follows the code of Adapter with the solutions tried:

public class TarefaAdapter extends ArrayAdapter<Tarefa> {

    private List<Tarefa> items;
    private int layoutResourceId;
    private Context context;
    private List<View> itensExibidos;

    public TarefaAdapter(Context context, int layoutResourceId, List<Tarefa> items) {
        super(context, layoutResourceId, items);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.items = items;

        this.itensExibidos = new ArrayList<View>();

        EventBus.getDefault().register(this);

    }

    public void onEvent(MessageRunTask event){



        if (!itensExibidos.isEmpty()) {

            //ESSE LOG APARECE
            Log.i("DEBUGEVENT", " teste");


            //TENTATIVA 1
            View v = itensExibidos.get(0);
            TarefasHolder ta = (TarefasHolder) v.getTag();
            ta.url.setText("Teste texto");

            //TENTATIVA 2
            TextView t = (TextView)v.findViewById(R.id.lblURL);
            t.setText("Teste texto");

            //TENTATIVA 3
            Tarefa teste = this.items.get(0);
            teste.url  = "Teste texto";

        }





    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {


        View row = convertView;
        TarefasHolder holder = null;

        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        row = inflater.inflate(layoutResourceId, parent, false);

        holder = new TarefasHolder();
        holder.tarefa = items.get(position);
        holder.removePaymentButton = (Button)row.findViewById(R.id.btnExcluir);
        holder.removePaymentButton.setTag(holder.tarefa);

        holder.url = (TextView)row.findViewById(R.id.lblURL);
        holder.status = (TextView)row.findViewById(R.id.lblStatus);
        holder.lastexecute = (TextView)row.findViewById(R.id.lblLastExecute);
        holder.connectivity = (TextView)row.findViewById(R.id.lblConnectivity);
        holder.idle = (TextView)row.findViewById(R.id.lblIdle);
        holder.charging = (TextView)row.findViewById(R.id.lblCharging);
        holder.delay = (TextView)row.findViewById(R.id.lblDelay);


        row.setTag(holder);

        setupItem(holder);

        itensExibidos.add(row);

        return row;


    }

    private void setupItem(TarefasHolder holder) {
        holder.url.setText(holder.tarefa.url);
        holder.status.setText(trataStatus(holder.tarefa.status));
        holder.lastexecute.setText(trataData(holder.tarefa.lastexecute));
        holder.charging.setText(trataCharging(holder.tarefa.charging));
        holder.idle.setText(trataIdle(holder.tarefa.idle));
        holder.delay.setText(trataDelay(holder.tarefa.periodic));
        holder.connectivity.setText(trataNetworkType(holder.tarefa.networktype));
    }

    public static class TarefasHolder{

        Tarefa tarefa;
        TextView url;
        TextView status;
        TextView lastexecute;
        TextView connectivity;
        TextView delay;
        TextView charging;
        TextView idle;
        Button removePaymentButton;

    }


}

These are the attempts I’ve made.

  • I think you’re complicating something that should be simple. If that Textview is in the Adapter then its value should be in the array that is managed by it. So simply change this value in the array for that change to be reflected in the Listview.

  • So, he is inside, but I want to change the value of it, every line of it is a Job running in the system, when the job ends I have to go in the specific item and change it. That is, the value that was used to create the Adapter changed after the Adapter was created, I need to go back into it and change it.

  • You just have to change that "specific item" array. If this is not the case, when this item exits the screen and is displayed again, the change disappears.

  • Adapter must have a List object, what you have to do is change the list items.

  • I tried to change the List, could not do. I changed the question and added all the code I did.

1 answer

1


It is not recommended to save the View created in Listview, you have no way to guarantee that this view has been destroyed.

If it is a text list, you create a String list and when you want to change the list just change the text of the list, example below:

(Inside the Adapter)

List<String> itens;

public void onEvent(MeuEvento me){
    //Sua lógica para saber qual item será alterado
    String s = itens.get(0);
    s = "mudei";
    notifyDataSetChanged();
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.