2017-02-13 4 views
0

У меня есть ListView и я заселена, что ListView с TextView и кнопки с помощью пользовательских adapter.I было генерироваться событие щелчка для этого Buttoncustom adapter в этом .В случае щелчка я пытаюсь измените текст и цвет кнопки, вплоть до этого его рабочий тон, но когда я прокручиваю ListView вверх и вниз, цвет текста другой кнопки меняется. Я должен был остановиться здесь в последнюю пару дней ...Текст кнопки в ListView пункта изменяется при прокрутке

public class CustomAdapter extends BaseAdapter { 

Activity a; 
ArrayList<String> Rollno; 
ArrayList<String> Stdname; 
ArrayList<String> Stdstatus; 

public CustomAdapter(Activity a, ArrayList<String> rollno, ArrayList<String> stdname, ArrayList<String> stdstatus) { 
    this.a = a; 
    Rollno = rollno; 
    Stdname = stdname; 
    Stdstatus = stdstatus; 
} 


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

@Override 
public Object getItem(int position) { 
    return Rollno.get(position); 
} 

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


public class ViewHolder{ 
    TextView rollno,name; 
    Button status; 
} 



@Override 
public View getView(final int position, View convertView, ViewGroup parent) { 
    final ViewHolder viewHolder=new ViewHolder(); 
    LayoutInflater li=a.getLayoutInflater(); 
    View v=li.inflate(R.layout.custom,parent,false); 
    viewHolder.rollno=(TextView)v.findViewById(R.id.crollno); 
    viewHolder.name=(TextView)v.findViewById(R.id.cname); 
    viewHolder.status=(Button)v.findViewById(R.id.btn1); 


    viewHolder.rollno.setText(Rollno.get(position)); 
    viewHolder.name.setText(Stdname.get(position)); 
    viewHolder.status.setText(Stdstatus.get(position)); 

    viewHolder.status.setTag(0); 
    viewHolder.status.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 

      final int status1 = (Integer) v.getTag(); 

      if (status1 == 1) { 
       viewHolder.status.setText("P"); 
       viewHolder.status.setBackgroundColor(Color.GREEN); 

       v.setTag(0); 
      } else if (status1 == 2) { 
       viewHolder.status.setText("A"); 
       viewHolder.status.setBackgroundColor(Color.RED); 
       v.setTag(1); 
      } else if (status1 == 3) { 
       viewHolder.status.setText("L"); 
       viewHolder.status.setBackgroundColor(Color.BLUE); 
       v.setTag(2); 
      } else { 
       viewHolder.status.setText("H"); 
       viewHolder.status.setBackgroundColor(Color.YELLOW); 
       v.setTag(3); 

      } 

     } 
    }); 
    return v; 
} 
} 
+2

У вас есть эта проблема, потому что ListView повторно использует ячейки и, таким образом, отображается состояние предыдущей ячейки. Вам нужно сохранить содержимое кнопки где-нибудь и проверить состояние в '' 'getView'''. – danypata

+0

Как я могу сохранить состояние кнопки. Я новичок в android, могу дать мне этот код. Спасибо заранее за вашу помощь – Ajinkya

ответ

0

В основном вам нужно сохранить статус для позиции. Замените адаптер следующим кодом.

class CustomAdapter extends BaseAdapter { 

     Activity a; 
     ArrayList<String> Rollno; 
     ArrayList<String> Stdname; 
     ArrayList<String> Stdstatus; 
     HashMap<Integer, Integer> statusArray = new HashMap<>(); 

     public CustomAdapter(Activity a, ArrayList<String> rollno, ArrayList<String> stdname, ArrayList<String> stdstatus) { 
      this.a = a; 
      Rollno = rollno; 
      Stdname = stdname; 
      Stdstatus = stdstatus; 
     } 


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

     @Override 
     public Object getItem(int position) { 
      return Rollno.get(position); 
     } 

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


     public class ViewHolder{ 
      TextView rollno,name; 
      Button status; 
     } 



     @Override 
     public View getView(final int position, View convertView, ViewGroup parent) { 
      final ViewHolder viewHolder=new ViewHolder(); 
      LayoutInflater li=a.getLayoutInflater(); 
      View v=li.inflate(R.layout.custom,parent,false); 
      viewHolder.rollno=(TextView)v.findViewById(R.id.crollno); 
      viewHolder.name=(TextView)v.findViewById(R.id.cname); 
      viewHolder.status=(Button)v.findViewById(R.id.btn1); 


      viewHolder.rollno.setText(Rollno.get(position)); 
      viewHolder.name.setText(Stdname.get(position)); 
      viewHolder.status.setText(Stdstatus.get(position)); 

      int status = statusArray.get(position) != null ? statusArray.get(position) : 0; 
      switch (status){ 
       case 1: 
        viewHolder.status.setText("P"); 
        viewHolder.status.setBackgroundColor(Color.GREEN); 
        break; 
       case 2: 
        viewHolder.status.setText("A"); 
        viewHolder.status.setBackgroundColor(Color.RED); 
        break; 
       case 3: 
        viewHolder.status.setText("L"); 
        viewHolder.status.setBackgroundColor(Color.BLUE); 
        break; 
       default: 
        viewHolder.status.setText("H"); 
        viewHolder.status.setBackgroundColor(Color.YELLOW); 
        break; 
      } 

      viewHolder.status.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 

        int status1 = statusArray.get(position) != null ? statusArray.get(position) : 0; 
        if (status1 == 1) { 
         statusArray.put(position, 0); 
        } else if (status1 == 2) { 
         statusArray.put(position, 1); 
        } else if (status1 == 3) { 
         statusArray.put(position, 2); 
        } else { 
         statusArray.put(position, 3); 
        } 

        notifyDataSetChanged(); 
       } 
      }); 
      return v; 
     } 
    } 
0

Это выглядит как вы перезаписать тег, который приводит к вашей кнопке логика не работает правильно.

viewHolder.status.setTag(0); 

вы обычно работают на жидком что-то вроде этого:

if (convertview == null){ 
    //create and initialize new viewholder and pass it to the views tag field 
    //initialize the status tag here and dont set it again later 
}else{ 
//get your viewholder from the views tag 
} 
//update your values 

Example:

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

ViewHolderItem viewHolder; 

/* 
* The convertView argument is essentially a "ScrapView" as described is Lucas post 
* http://lucasr.org/2012/04/05/performance-tips-for-androids-listview/ 
* It will have a non-null value when ListView is asking you recycle the row layout. 
* So, when convertView is not null, you should simply update its contents instead of inflating a new row layout. 
*/ 
if(convertView==null){ 

    // inflate the layout 
    LayoutInflater inflater = ((Activity) mContext).getLayoutInflater(); 
    convertView = inflater.inflate(layoutResourceId, parent, false); 

    // well set up the ViewHolder 
    viewHolder = new ViewHolderItem(); 
    viewHolder.textViewItem = (TextView) convertView.findViewById(R.id.textViewItem); 

    // store the holder with the view. 
    convertView.setTag(viewHolder); 

}else{ 
    // we've just avoided calling findViewById() on resource everytime 
    // just use the viewHolder 
    viewHolder = (ViewHolderItem) convertView.getTag(); 
} 

// object item based on the position 
ObjectItem objectItem = data[position]; 

// assign values if the object is not null 
if(objectItem != null) { 
    // get the TextView from the ViewHolder and then set the text (item name) and tag (item ID) values 
    viewHolder.textViewItem.setText(objectItem.itemName); 
    viewHolder.textViewItem.setTag(objectItem.itemId); 
} 

return convertView; 

}

0

использование вид карты такая же проблема я столкнулся использовать подобное.

   if (readvalue.equals("0")) { 
     holder.cardView.setCardBackgroundColor(context.getResources().getColor(R.color.texthintcolor)); 
     // holder.itemView.setBackgroundResource(0); 
     //cardView.setRadius(5); 

    } else { 
     //  holder.itemView.setBackgroundColor()); 
     holder.cardView.setCardBackgroundColor(context.getResources().getColor(R.color.winter)); 


     // cardView.setRadius(5); 

    } 
0

место this 'viewHolder.status.setTag (0);' ниже onClickListener я думаю, что он будет работать

viewHolder.status.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 

     final int status1 = (Integer) v.getTag(); 

     if (status1 == 1) { 
      viewHolder.status.setText("P"); 
      viewHolder.status.setBackgroundColor(Color.GREEN); 

      v.setTag(0); 
     } else if (status1 == 2) { 
      viewHolder.status.setText("A"); 
      viewHolder.status.setBackgroundColor(Color.RED); 
      v.setTag(1); 
     } else if (status1 == 3) { 
      viewHolder.status.setText("L"); 
      viewHolder.status.setBackgroundColor(Color.BLUE); 
      v.setTag(2); 
     } else { 
      viewHolder.status.setText("H"); 
      viewHolder.status.setBackgroundColor(Color.YELLOW); 
      v.setTag(3); 

     } 

    } 
}); 
viewHolder.status.setTag(0); 
return v; 

}}

0

Представления воссозданы каждый раз, когда они выходят на передний план, поэтому только просмотр позиции действительно может быть уникальным. Вместо использования тегов просмотра вы можете создать Hashmap позиции просмотра и свой собственный тег (Integer). Каждый раз, когда создается вид, вы проверяете, существует ли позиция в hashmap, и задайте цвет кнопки в соответствии с тегом hashmap. Разместите линию

private HashMap<Integer,Integer> tagMap = new HashMap<>(); 

в начале customAdapter. В GetView проверить, если позиция была добавлена, если не добавить его с соответствующей кнопки тег

int tag=1; //desired default tag for button 
    //add to hashmap if not exist 
if(!tagMap.containsKey(position)){ 
     tagMap.put(position,tag); 
    } 

В OnClick listerner, вы могли бы получить hasmap тег по позиции и установить цвет кнопки соответственно

final int status1 = tagMap.get(position);; 

     if (status1 == 1) { 
      viewHolder.status.setText("P"); 
      viewHolder.status.setBackgroundColor(Color.GREEN); 

      tagMap.put(position,0);//this will update tag for current position 
     } else if (status1 == 2) { 
      viewHolder.status.setText("A"); 
      viewHolder.status.setBackgroundColor(Color.RED); 
      tagMap.put(position,1); 
     } else if (status1 == 3) { 
      viewHolder.status.setText("L"); 
      viewHolder.status.setBackgroundColor(Color.BLUE); 
      tagMap.put(position,2); 
     } else { 
      viewHolder.status.setText("H"); 
      viewHolder.status.setBackgroundColor(Color.YELLOW); 
      tagMap.put(position,3); 
     } 

Надежда это помогает

 Смежные вопросы

  • Нет связанных вопросов^_^