Tony
Tony

Reputation: 3805

Something really wrong with adapter

I've got next adapter:

public class CityAdapter extends ArrayAdapter<String> {     

    public CityAdapter(Context context, int resource) {
        super(context, resource, Cities.TITLES);

    }    

    public int getCount() {
       return Cities.ARRAY.length;
    }

    @SuppressLint("InflateParams")
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {    
        View v = convertView;    
        if (v == null) {    
            LayoutInflater vi;
            vi = LayoutInflater.from(getContext());
            v = vi.inflate(R.layout.city_list_row, null);    
        }

        TextView title = (TextView) v.findViewById(R.id.cityTitle);    
        title.setText(Cities.ARRAY[position].getName());    
        if (position==4) {    
            title.setTextColor(Color.parseColor("#ff0000"));
        }    
        return v;    
    }    
}

Please take a closer look at this line:

if (position==4) 

I don't know how, but that statement works for position 0 too.

enter image description here

Seems really impossible. What is wrong? Also if I change my code like this:

if (position==4) {
    title.setText("hey!!!");
    title.setTextColor(Color.parseColor("#ff0000"));
}

I will get this:enter image description here

Upvotes: 0

Views: 59

Answers (2)

Raghu Mudem
Raghu Mudem

Reputation: 6963

Add an else case for your color settings like this

if (position==4) {    
    title.setTextColor(Color.parseColor("#ff0000"));
} 
else {
    title.setTextColor(Color.parseColor("#ffffff"));
}

Upvotes: 1

Amsheer
Amsheer

Reputation: 7131

You are not implementing the else part.

ViewHolder holder;
if (v == null) {    
    LayoutInflater vi;
    vi = LayoutInflater.from(getContext());
    v = vi.inflate(R.layout.city_list_row, null);   
    holder.title = (TextView) v.findViewById(R.id.cityTitle); 
    v.setTag(holder); 
} else {
    holder = (ViewHolder) convertView.getTag();
}

And then you need to create a class

class ViewHolder {
    TextView title;
}

Also you need else part of this.

if (position==4) {
    title.setText("hey!!!");
    title.setTextColor(Color.parseColor("#ff0000"));
}else{
 title.setText("heyfffgg!!!");
    title.setTextColor(Color.parseColor("#ff0000"));
}

Upvotes: 1

Related Questions