Edit item from a listview

5

I would like to know how you can modify an item x of a list view through the java code ...

I have the entry class object class:

public class Lista_entrada {

    private String textoEncima;
    private String textoDebajo;
    private String texto_id;

    public Lista_entrada(String textoEncima, String textoDebajo, String texto_id) {

        this.textoEncima = textoEncima;
        this.textoDebajo = textoDebajo;
        this.texto_id = texto_id;
    }

    public String getTexto_id() {
        return texto_id;
    }

    public String get_textoEncima() {
        return textoEncima;
    }

    public String get_textoDebajo() {
        return textoDebajo;
    }


}

the adapter class

public abstract class Lista_adaptador extends BaseAdapter {

    private ArrayList<?> entradas;
    private int R_layout_IdView;
    private Context contexto;

    public Lista_adaptador(Context contexto, int R_layout_IdView, ArrayList<?> entradas) {
        super();
        this.contexto = contexto;
        this.entradas = entradas;
        this.R_layout_IdView = R_layout_IdView;
    }

    public Lista_adaptador( int entrada, ArrayList<Lista_entrada> datos) {
    }

    @Override
    public View getView(int posicion, View view, ViewGroup pariente) {
        if (view == null) {
            LayoutInflater vi = (LayoutInflater) contexto.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = vi.inflate(R_layout_IdView, null);
        }
        onEntrada (entradas.get(posicion), view);
        return view;
    }

    @Override
    public int getCount() {
        return entradas.size();
    }

    @Override
    public Object getItem(int posicion) {
        return entradas.get(posicion);
    }

    @Override
    public long getItemId(int posicion) {
        return posicion;
    }

    /** Devuelve cada una de las entradas con cada una de las vistas a la que debe de ser asociada
     * @param entrada La entrada que será la asociada a la view. La entrada es del tipo del paquete/handler
     * @param view View particular que contendrá los datos del paquete/handler
     */
    public abstract void onEntrada (Object entrada, View view);

}

In my activity I fill the list and show it:

datos.add(new Lista_entrada("BUHO", "Búho es el nombre","textol bla bla"));
        datos.add(new Lista_entrada("COLIBRÍ", "Los troquilinos","texto bla bla"));


        lista = (ListView) findViewById(R.id.ListView_listado);

        lista.setAdapter(new Lista_adaptador(this, R.layout.entrada, datos) {
            @Override
            public void onEntrada(Object entrada, View view) {
                if (entrada != null) {
                    TextView texto_superior_entrada = (TextView) view.findViewById(R.id.textView_superior);
                    if (texto_superior_entrada != null)
                        texto_superior_entrada.setText(((Lista_entrada) entrada).get_textoEncima());

                    TextView texto_inferior_entrada = (TextView) view.findViewById(R.id.textView_inferior);
                    if (texto_inferior_entrada != null)
                        texto_inferior_entrada.setText(((Lista_entrada) entrada).get_textoDebajo());

                    TextView texto_id_entrada = (TextView) view.findViewById(R.id.textView_id);
                    if (texto_id_entrada != null)
                        texto_id_entrada.setText(((Lista_entrada) entrada).getTexto_id());


                }
            }
        });

What I want to do is give a position of an item in the listview, modify it only to the, for example change the name of the item [0], Bhuo for Eagle, to give an example ....

    
asked by Felix A Marrero Pentón 31.03.2017 в 04:15
source

2 answers

4

This would be done in Adapter , but you must first modify your Lista_entrada object so you can modify the property:

 private String textoEncima;

add a setter:

public void set_textoEncima(String textoNuevo ) {
    this.textoEncima = textoNuevo;
}

Adding this method to your object, now within getView() you can find if the text is "Owl" and change it to "Eagle":

@Override
public View getView(int posicion, View view, ViewGroup pariente) {
    if (view == null) {
        LayoutInflater vi = (LayoutInflater) contexto.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = vi.inflate(R_layout_IdView, null);
    }
    /************************************/        
    if(entradas.get(posicion).get_textoEncima().equalsIgnoreCase("Buho")){ //encuentra Buho.
        //Cambia texto a Aguila.
        entradas.get(posicion).set_textoEncima("Aguila");
    }
    /************************************/

    onEntrada (entradas.get(posicion), view);
    return view;
}

Update:

What you need is that by clicking on a cell and opening another Activity, the description of this in the layout contains the text: "Usted salió de aquí!" for this simply get the reference of the summary and modify its text by clicking on the Element:

    convertView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            mHolder.txtDescripcion.setText("Usted salió de aquí!");

            Intent intent = new Intent(context, MainActivity2.class);
            intent.putExtra("nombre", entradas.get(position).getTexto());
            intent.putExtra("descripcion", entradas.get(position).getDescripcion());
            context.startActivity(intent);

            notifyDataSetChanged();
        }
    });

This would be the complete method getView() :

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        // inflate the layout.
        final ViewHolder mHolder;

        if (convertView == null) {
            convertView = LayoutInflater.from(context).
                    inflate(R.layout.item_row, parent, false);
            mHolder = new ViewHolder();
            mHolder.txtTitulo=(TextView) convertView.findViewById(R.id.titulo);
            mHolder.txtDescripcion=(TextView) convertView.findViewById(R.id.descripcion);
            mHolder.mImage=(ImageView) convertView.findViewById(R.id.androidImage);
            convertView.setTag(mHolder);


        }else{
            mHolder = (ViewHolder) convertView.getTag();
        }

        myObjeto item = entradas.get(position);
        mHolder.txtTitulo.setText(item.getTexto());


        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mHolder.txtDescripcion.setText("Usted salió de aquí!");

                Intent intent = new Intent(context, MainActivity2.class);
                intent.putExtra("nombre", entradas.get(position).getTexto());
                intent.putExtra("descripcion", entradas.get(position).getDescripcion());
                context.startActivity(intent);

                notifyDataSetChanged();
            }
        });

        convertView.setTag(mHolder);
        return convertView;
        //return convertView;
    }

link

    
answered by 04.04.2017 в 22:50
3

Theoretically you can just call notifyDataSetChanged() after manipulating data in your list.

// despues de actualizar datos
BaseAdapter adapter = (BaseAdapter) lista.getAdapter();
adapter.notifyDataSetChanged();

You use a VO ( value object ) for your entries, but you reserve the option to edit partial data. That only makes sense in a multi-threaded context. Make sure you keep in mind that while you edit values that the object will change that way, so take care of your references.

As an edition implies replacing one VO with another, the proposed solution would be to add the following method to the adapter:

public remplazar (int posicion, Object remplazo){
    entradas.set(posicion, remplazo);
    notifyDataSetChanged(); // aqui se inicia la actualisacion de la lista y sus vistas
}

TLDR end;

Some tips about your code:

  • Get more consistent with code style. If you use lista_entrada or listaEntrada (camelCase is very preferred in Java) in the background is something to your liking, but your code is clearer if you choose one and you stay with that.
  • If you use generics, use it well, or leave them completely. Your abstract class with generics should be declared as public abstract class ListaAdapter<T> extends BaseAdapter , and consequently you should use T in statements like ArrayList<T> entradas; and in methods like public T getItem(int pos){...}) .
  • You use a VO ( value object ) for your entries, but you reserve the option to edit partial data. That only makes sense in a multi-threaded context. Make sure you keep in mind that while you edit values that the object will change that way, so take care of your references.
  • If you allow to change values of an object with a unique id, check if the VO pattern is ideal for your logic.
  • The adapter is called adapter for something: if you use it, it exposes what you need to update the list dynamically and avoids accessing the encapsulated list directly.
answered by 03.04.2017 в 03:47