0

Maybe there is a simple solution to my problem, but i´m not able to find it. I have a ListView with a list of users. Each row has an EditText to enter the username. I want to set hint text for each user like: "user1, user2, user3, etc", used as default name. The user can click on the EditText and change this name, so the hint text dissapears and the user can enter his name, or leave this default name if he wants.

When the ListView is too long, I have the problem of the view recycling, that duplicates the names. I solved it by using a setOnFocusChangeListener for the EditText, and storing the name for each row, and it´s working fine, but I´d like that when I have a long list, keep the hint text or text introduced by the user for each EditText when scrolling the list.

I don´t know how to modify my adapter to set the name/hint for each EditText.

Any idea?

Thanks a lot.

jgonzal
  • 23
  • 2

2 Answers2

0

Create one list in your activity

List<String> yourlist = new ArrayList();
yourlist.add("user1");
yourlist.add("user2");
yourlist.add("user3");....

pass this list to adapter then in adapter,

holder.youredittext.setHint(yourlist.get(position)); 

if your passing number of items to adapter then create one model class then pass to adapter.

  • If i use holder.youredittext.setHint(yourlist.get(position));, it will look always as hint text when scrolling, even if the text of the EditText has been edited. – jgonzal Jan 20 '16 at 13:26
0

I have an adapter like this:

public View getView(final int position, View convertView, ViewGroup parent)
    {
        View item = convertView;

        if(item == null)
        {
            LayoutInflater inflater = context.getLayoutInflater();
            item = inflater.inflate(R.layout.setup_jugador, null);

            holder = new ViewHolder();
            holder.nombre = (EditText) item.findViewById(R.id.nombre);

            // Establecemos el tag
            item.setTag(holder);
        }            
        else
        {
            holder = (ViewHolder)item.getTag();
        }

        holder.nombre.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if(!hasFocus){
                    EditText et = (EditText) v.findViewById(R.id.nombre);
                    // Here I store the name
                    jugadores.get(position).setNombre(et.getText().toString());
                }
            }
        });

        // Update EditText
        holder.nombre.setText(jugadores.get(position).getNombre());
        // Another option is
        holder.nombre.setHint(jugadores.get(position).getNombre());

        return(item);
}

jugadores is a list that stores the names. Initially it has user1, user2, etc. I could setHint when Item==null, but I have to update the text at the end of the adapter, and when scrolling, the view recycling changes the items that are invisible.

I only see 8 items, and when scrolling, if I change first item, item number 9 also change. If I use setText, it becomes black, and if I use setHint, first item becomes grey.

I can´t put hint value in layout because I´d like to add the row number to the name. I tried using a boolean value in the class used as model in the adapter to show that the name has been modified, check this value using position index in the list, and use setText or setHint according to that, but doesn´t work.

jgonzal
  • 23
  • 2