0

I want to change text color of selected item in listview.

Main

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sub_alimentacao);

    List<Tag> tags = getTagsSubAlimentacao();

    final ListView listView = (ListView) findViewById(R.id.subAlimentacao);
    listView.setAdapter(new TagSubAlimentacaoAdapter(this, tags));



    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {


        @Override
        public void onItemClick(AdapterView<?> parent, final View view, final int position, long id) {


            TextView c = (TextView) view.findViewById(R.id.local);
            LikeButton lk = (LikeButton) view.findViewById(R.id.gostei);

            lk.setEnabled(false);


            //OBTEM A COR EM INTEIRO E CONVERTE PARA HEXADECIMAL
            Integer intColor = c.getCurrentTextColor();
            String hexColor = "#" + Integer.toHexString(intColor).substring(2);

            if (hexColor.equalsIgnoreCase("#2196F3")){
                c.setTextColor(Color.parseColor("#aaaaaa"));
                lk.setLiked(false);

            }else{
                c.setTextColor(Color.parseColor("#2196F3"));
                lk.setEnabled(true);
                lk.setLiked(true);

            }



        }
    });

}

Adapter

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

    Tag tag = tags.get(position);

    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View layoute = inflater.inflate(R.layout.item_sub, null);

    TextView titulo = (TextView) layoute.findViewById(R.id.local);
    LikeButton lk = (LikeButton) layoute.findViewById(R.id.gostei);
    titulo.setText(tag.getTitulo());
    lk.setLiked(tag.getAtivo());
    return layoute;
}

it's working fine when was clicked. The color was changed. But i have one problem i.e.,

For example i am having 10 items in listview at first only 5 items visible(because of screen resolution) if i am scrolling i can visible the next 5 items.

When I select the first 5 elements, the colors are changed. However if rolling for next 5 elements

The colors of the 5 first elements are back to the original state.

2 Answers2

0

Use View Holder Pattern... use this answers make changes as per your requirment.

Community
  • 1
  • 1
Anuj Zunjarrao
  • 426
  • 2
  • 14
0

In ListView while scrolling, it always sets adapter so inside adapter you have to set text color and it is better if you prefer ViewHolder pattern

Use the below inside adapter to get your output

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

    Tag tag = tags.get(position);

    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View layoute = inflater.inflate(R.layout.item_sub, null);

    TextView titulo = (TextView) layoute.findViewById(R.id.local);
    LikeButton lk = (LikeButton) layoute.findViewById(R.id.gostei);
    titulo.setText(tag.getTitulo());
    boolean likeValue=tag.getAtivo();
    if(likeValue){
       titulo.setTextColor(Color.parseColor("#2196F3"));
    }else{
       titulo.setTextColor(Color.parseColor("#aaaaaa"));
    }
    lk.setLiked(likeValue);

    return layoute;
}
Aditi
  • 455
  • 4
  • 13
  • implementing the methods to enable and disable in adapter and then calling the main with his suggestion solved my problem. `public void alteraCor(int position){ tags.get(position).setAtivo(true); } public void removeCor(int position){ tags.get(position).setAtivo(false); } ` Thank you! – Douglas Siqueira Oct 05 '16 at 16:41