android – Change the text of a TextView that is inside an ArrayAdapter item

Question:

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

I want any event in the system to be observed by my Adapter and for a certain item in the list to be changed. For that I'm using EventBus .

In my Adapter constructor I use EventBus.getDefault().register(this);

The Adapter has an onEvent(MessageRunTask event){...}

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

What I don't know how to do is:

Inside the onEvent method, how do I get item X from the list and update TextView Y's Text property that is inside that item X?

UPDATE 1:

Here is the Adapter code with the tried solutions:

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;

    }


}

Those are the attempts I made.

Answer:

It is not recommended to save the View created in the ListView, you cannot guarantee that this view was destroyed.

If it's 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();
}
Scroll to Top