Search code examples
androidlistviewbackgroundlistactivity

how to change background of a row (special) in listview with MainActivity extends ListActivity


I have a project, which have MainActivity extends ListActivity. I want change background color of a row in listview. I can change if MainActivity is extend Activity and I have a CustomAdapter. Then I override a method getView(), then i can do it.

But i haven't way to change it with my project now, i can't override method getView(). please show me a way.

I'm sory if the artice is ambigouos.


Solution

  • MyAdapter class:

        public View getView(int position, View convertView, ViewGroup parent) {
        // View view= super.getView(position, convertView, parent);
        // if (position == 0) {
        // view.setBackgroundColor(Color.GRAY);
        // }
    
        // TODO Auto-generated method stub
        int pos = position;
        View row = convertView;
        WeatherHolder holder = null;
    
        if (row == null) {
    
            LayoutInflater inflater = ((MainActivity) context)
                    .getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);
    
            holder = new WeatherHolder();
            holder.imgIcon = (ImageView) row.findViewById(R.id.imgIcon);
            holder.txtTitle = (TextView) row.findViewById(R.id.txtTitle);
    
            row.setTag(holder);
        } else {
            holder = (WeatherHolder) row.getTag();
        }
        Weather weather = data[position];
        holder.txtTitle.setText(weather.titleviet);
        holder.imgIcon.setImageResource(weather.iconviet);
        if (position == 0) {
            row.setBackgroundColor(Color.GRAY);
        }
    
        return row;
    }
    
    static class WeatherHolder {
        ImageView imgIcon;
        TextView txtTitle;
    }
    

    and they show at positon:0, 8, 16,... I want it show only postion:0