android – Modify Custom Adapter Listview Item Text

Question:

I have a Lisview where custom items have a Button (among other components). The Button displays an AlertDialog.Builder with a dynamic list of options:

  1. If you select the last option in the list: close the current AlertDialog.Builder and open a new AlertDialog, requesting to enter a value in a text box.
  2. If you select any other item in the dynamic list: Close the current AlertDialog.Builder.

Whichever option is selected, the Listview Button should modify its text as selected.

The problem occurs in the Listview's Adapter, specifically in the getView method. When modifying the ViewHolder I get the error: the ViewHolder variable is not final.

How is it possible to modify the text of a button of a Listview item, according to what was selected / entered from a Builder / AlertDialog that the same button showed?

I show relevant part of the code:

...
AlertDialog.Builder bprecios;
AlertDialog adnuevoprec;
...

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if(convertView == null){
            LayoutInflater vi = (LayoutInflater)act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = vi.inflate(R.layout.it_selectitem, null);         
            holder = new ViewHolder();
            holder.precio = (Button)convertView.findViewById(R.id.bt_sa_prec);
            holder.precio.setOnClickListener(new OnClickListener() {                
                @Override
                public void onClick(View v) {
                    final List<String> list = new ArrayList<String>();
                    list.add("Precio 1");
                    list.add("Precio 2");                   
                    list.add("** mostrar otro dialog **");
                    CharSequence[] items = list.toArray(new CharSequence[list.size()]);

                    bprecios.setItems(items, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int item) {

                            if(item == list.size() - 1){ // el ultimo elemento
                                et_nuevoprecio = new EditText(act);
                                et_nuevoprecio.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);
                                adnuevoprec = new AlertDialog.Builder(act)
                                .setMessage("Ingrese Nuevo Precio [S/.]")
                                .setTitle("Nuevo Precio")
                                .setView(et_nuevoprecio)
                                .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int id) {
                                        holder.precio.setText(String.format("%.2f",et_nuevoprecio.getText()));// ERROR HOLDER NO FINAL
                                    }
                                })                               
                                .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int id) {
                                    dialog.dismiss();
                                    }
                                })
                                .create();

                            }else{
                                holder.precio.setText("");  // ERROR HOLDER NO FINAL                            
                            }
                            dialog.dismiss();                
                        }
                    }).show();
                }
            });
        }else{
            holder = (ViewHolder) convertView.getTag(); 
        }
        ...
        //Valor texto que se muestra por primera vez en los items del ListView.
        holder.precio.setText(String.format("%.2f",         miLista.get(position).getPreciocontado())); 
        ...
    return convertView;
    }

Perhaps there are better ways to modify the text of a component of an item in a Listview without using AlertDialog, it is expected not to leave the Activity of the ListView to modify.

Thank you.

Answer:

@ Oz92, you just need to add the final word when you declare your ViewHolder at the start of getView :

final ViewHolder holder=null;

EDIT

Ok, I'm noticing that in the else part you are not doing anything with the holder variable, if so, you could eliminate the else and declare the variable directly inside the if when you assign a value in the following way:

if(convertView == null){
    LayoutInflater vi = (LayoutInflater)act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    convertView = vi.inflate(R.layout.it_selectitem, null);         
    final ViewHolder holder = new ViewHolder();
    holder.precio = (Button)convertView.findViewById(R.id.bt_sa_prec);
    holder.precio.setOnClickListener(new OnClickListener() {                
        @Override
        public void onClick(View v) {
            final List<String> list = new ArrayList<String>();
            list.add("Precio 1");
            list.add("Precio 2");                   
            list.add("** mostrar otro dialog **");
            CharSequence[] items = list.toArray(new CharSequence[list.size()]);

            bprecios.setItems(items, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int item) {

                    if(item == list.size() - 1){ // el ultimo elemento
                        et_nuevoprecio = new EditText(act);
                        et_nuevoprecio.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);
                        adnuevoprec = new AlertDialog.Builder(act)
                        .setMessage("Ingrese Nuevo Precio [S/.]")
                        .setTitle("Nuevo Precio")
                        .setView(et_nuevoprecio)
                        .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int id) {
                                holder.precio.setText(String.format("%.2f",et_nuevoprecio.getText()));// ERROR HOLDER NO FINAL
                            }
                        })                               
                        .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int id) {
                            dialog.dismiss();
                            }
                        })
                        .create();

                    }else{
                        holder.precio.setText("");  // ERROR HOLDER NO FINAL                            
                    }
                    dialog.dismiss();                
                }
            }).show();
        }
    });
}else{
    ViewHolder holder = (ViewHolder) convertView.getTag(); 
}
Scroll to Top