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>