MacaronLover MacaronLover - 2 months ago 10
Android Question

GridView item text not changing colour

I'm trying to change the text colour of specific items in my grid view but for some reason it is not working. Everytime I launch my app the text colour for all my gridview items remain white, which is not the outcome I expected despite the code used. Does anyone know what has gone wrong and what needs to be done in order to resolve this problem?

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<GridView
android:id="@+id/gridView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:listSelector="@android:color/transparent"
android:clickable="false"
android:numColumns="auto_fit"
android:columnWidth="90dp"
android:gravity="center"
android:stretchMode="columnWidth">
</GridView>
</RelativeLayout>


MainActivity.java

public class MainActivity extends AppCompatActivity {

GridView gridView;

static final String[] years = new String[]{
"1863", "1864", "1868", "1871", "1890",
"1898", "1900", "1906", "1968", "1979"};

@Override
public void onCreate (Bundle savedInstanceState){
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

gridView = (GridView) findViewById(R.id.gridView1);

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.gridview_item, R.id.item_gridview, years);
gridView.setAdapter(adapter);
}

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

TextView tv = (TextView) convertView.findViewById(R.id.item_gridview);

if(position == 4 | position == 6){
tv.setTextColor(Color.parseColor("#1e1eff"));
}
else if(position == 2){
tv.setTextColor(Color.parseColor("#e32017"));
}
else {
tv.setTextColor(Color.WHITE);
}


return tv;
}
}


layout/gridview_item.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<TextView
android:id="@+id/item_gridview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="12dp"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>

Answer

you need to initialize your layout, and custom adapter

UPDATE

public class MainActivity extends Activity {

    GridView gridView;
    public MyAdapter adapter;
    static final String[] years = new String[] {
        "1863",
        "1864",
        "1868",
        "1871",
        "1890",
        "1898",
        "1900",
        "1906",
        "1968",
        "1979"
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        gridView = (GridView) findViewById(R.id.gridView1);
        adapter = new MyAdapter(getApplicationContext(), 0);
        gridView.setAdapter(adapter);
        for (int i = 1; i < years.length; i++) {

            adapter.addAdapterItem(new AdapterItem(years[i]));
        }
    }

    public class MyAdapter extends ArrayAdapter < AdapterItem > {
        private List < AdapterItem > items = new ArrayList < AdapterItem > ();

        // private static LayoutInflater inflater=null;
        public MyAdapter(Context context, int textviewid) {
            super(context, textviewid);
        }

        public void addAdapterItem(AdapterItem item) {
            items.add(item);
        }

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

        @Override
        public AdapterItem getItem(int position) {
            return ((null != items) ? items.get(position) : null);
        }

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

        @SuppressWarnings("deprecation")
        @Override
        public View getView(final int position, View convertView,
            final ViewGroup parent) {
            View rowView;
            if (convertView == null) {
                rowView = getLayoutInflater().inflate(R.layout.gridview_item,
                    null);
            } else {
                rowView = convertView;
            }
            TextView tv = (TextView) rowView.findViewById(R.id.item_gridview);
            tv.setText(items.get(position).first);
            if (position == 4 || position == 6) {

                tv.setTextColor(Color.parseColor("#1e1eff"));
            } else if (position == 2) {
                tv.setTextColor(Color.parseColor("#e32017"));
            } else {
                // tv.setTextColor(Color.WHITE);
            }

            return rowView;
        }
    }

    public class AdapterItem {
        public String first;

        // add more item

        public AdapterItem(String first) {
            this.first = first;

        }
    }
}
Comments