wbk727
wbk727

Reputation: 8408

ListView items not changing text colour

I've used a colouring adapter to change the text colour of my list view items but after debugging the app crashes and I get an error that I don't know how to fix. I know that the problem lies with line 77 but the reason is not clear to me. Any ideas on how to rectify this?

    package com.apptacularapps.exitsexpertlondonlite;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;


public class StationChooserActivity extends ActionBarActivity {

    ListView list_linechooser;

    String[] listContent = {
            "Bakerloo line", "Central line", "Circle line",
            "District line", "Hammersmith & City line",
            "Jubilee line", "Metropolitan line",
            "Northern line", "Piccadilly line",
            "Victoria line", "Waterloo & City line",
            "Docklands Light Railway", "London Overground",
            "Tramlink"
    };

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


        list_linechooser = (ListView)findViewById(R.id.list_linechooser);
        MyColoringAdapter adapter = new MyColoringAdapter(this,listContent);
        list_linechooser.setAdapter(adapter);
    }

    private class MyColoringAdapter extends ArrayAdapter<String> {
        private final Context context;
        private final String[] values;

        public MyColoringAdapter(Context context, String[] values) {
            super(context, R.layout.list_item, values);
            this.context = context;
            this.values = values;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View rowView = inflater.inflate(R.layout.list_item, parent, false);
            TextView textView = (TextView) rowView.findViewById(R.id.list_item);
            // Set text
            textView.setText(values[position]);
            // Set color depending on position
            int textColorId = R.color.white; // Default color
            switch (position) {
                case 0:
                    textColorId = R.color.bakerloo; break;
                case 1:
                    textColorId = R.color.central; break;
                case 2:
                    textColorId = R.color.circle; break;
                case 3:
                    textColorId = R.color.district; break;
                case 4:
                    textColorId = R.color.hc; break;
                case 5:
                    textColorId = R.color.jubilee; break;
                case 6:
                    textColorId = R.color.metropolitan; break;
                case 7:
                    textColorId = R.color.white; break;
                case 8:
                    textColorId = R.color.piccadilly; break;
                case 9:
                    textColorId = R.color.victoria; break;
                case 10:
                    textColorId = R.color.wc; break;
                case 11:
                    textColorId = R.color.dlr; break;
                case 12:
                    textColorId = R.color.overground; break;
                case 13:
                    textColorId = R.color.tramlink; break;
            }
            textView.setTextColor(getResources().getColor(textColorId));
            return rowView;
        }
    }
}

enter image description here

enter image description here

Upvotes: 0

Views: 62

Answers (2)

Yousef khan
Yousef khan

Reputation: 3204

Your list item layout file name is list_item but i think you are not giving the correct id for textview. Here

TextView textView = (TextView) rowView.findViewById(R.id.list_item);

Make double sure you text view id in xml file is list_item (i dont think so). In that case just change it to correct id and it will work hopefully.

Upvotes: 1

Daniel Arg&#252;elles
Daniel Arg&#252;elles

Reputation: 2349

The error is a NullPointerException on Line 77 is ( Please, avoid using an image for share the error log, text is better ). That line is:

textView.setText(values[position]);

I assumed that you don't pass a null array to constructor, so the other possible NullExcepction problem is in textView. I would said that textView is null. Can you check that?

Upvotes: 0

Related Questions