4

I'm currently having an issue with using custom ListView adapters with the Holo.Light theme. Within the activities and fragments, any TextViews are displayed in the theme's normal colours (textColorPrimary). However, any text within the custom ListAdapter uses textColorPrimary from the default Holo theme, effectively making the text unreadable.

Here is an example from my app's main menu:


list_main_menu.xml - The row layout for the ListAdapter

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

    <ImageView
        android:id="@+id/imgIcon"
        android:layout_height="48dip"
        android:layout_width="48dip"
        android:src="@drawable/ic_launcher"
        />

    <TextView 
        android:id="@+id/txtFirstLine"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/imgIcon"
        android:text="Line 1"
        android:textSize="12pt"
        />

    <TextView
        android:id="@+id/txtSecondLine"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/imgIcon"
        android:layout_below="@id/txtFirstLine"
        android:text="Line 2"
        />
</RelativeLayout>

Note: I'm currently having to use android:textColor="?android:attr/textColorPrimaryInverse" to make the text readable.


fragment_main_menu.xml - The Main Menu fragment.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:orientation="vertical" >
    <TextView
        android:id="@+id/txtWelcome"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Username"
        android:textSize="18sp"
        />
    <ListView
        android:id="@id/android:list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        />     
</LinearLayout>

AndroidManifext.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="net.michaeldodd.treasurehunter"
    android:versionCode="1"
    android:versionName="0.0.1" >

    <uses-sdk android:minSdkVersion="11" />
    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name" android:name=".gui.Login" 
            android:theme="@android:style/Theme.Holo.Light">

            <intent-filter >
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity android:name=".gui.Home" android:theme="@android:style/Theme.Holo.Light" />
        <activity android:name=".gui.UserProfile" android:theme="@android:style/Theme.Holo.Light" />
        <activity android:name=".gui.MapList" android:theme="@android:style/Theme.Holo.Light" /> 
    </application>
</manifest>

I'm not currently using any custom styles. Thanks for reading, and any useful comments will be greatly appreciated.

EDIT 1: Here's the requested screenshots. asdf This is how it looks if I don't specify the text colour, it seems to be using the default text colour for Holo Dark

asdf Manually specifying android:textColor="?android:attr/textColorPrimaryInverse" gives this result, but I'm uneasy at using a workaround like this.

Michael Dodd
  • 10,102
  • 12
  • 51
  • 64

2 Answers2

23

You haven't posted any actual code, but since I've had exactly the same problem, I would guess that you are passing the wrong Context to your custom adapter constructor.

I've been doing something like this (within my Fragment):

dataSource = new MyCursorAdapter(getActivity().getApplicationContext(), R.layout.myrow, data,
            fields, new int[] { R.id.field1, R.id.field2 });

All I had to do to fix the problem, was to replace getActivity().getApplicationContext() by getActivity():

dataSource = new MyCursorAdapter(getActivity(), R.layout.myrow, data,
            fields, new int[] { R.id.field1, R.id.field2 });

And it started to work as expected.

MyCursorAdapter (extending SimpleCursorAdapter) constructor:

public MyCursorAdapter(Context context, int layout, Cursor c,
        String[] from, int[] to) { //... }
Pratik Butani
  • 60,504
  • 58
  • 273
  • 437
rockford
  • 281
  • 2
  • 2
1

Yes, I had the same problem and reading what rockford posted I could solve it, don't be necessary to use getApplicationContext() when you're implementing an adapter.

Here is the code of my ListFragment class:

public class TipoProveedorFavoritoActivity extends ListFragment {

    Context mContext;
    MiApp myApp;
    Usuario usuario;
    ProgressDialog progressDialog;

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);
        //getListView().setCacheColorHint(Color.BLACK);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        mContext = getActivity();
        myApp = (MiApp)getActivity().getApplication();
        usuario = myApp.usuario;

        // Progress Dialog

        progressDialog = new ProgressDialog(mContext);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressDialog.setMessage("Espere...");
        progressDialog.setProgress(0);


        Log.d("usuario", String.valueOf(usuario.id));

        ConsultaDB consulta = new ConsultaDB();
        consulta.execute();

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        return inflater.inflate(R.layout.tipoproveedorfavorito_activity, container, false);
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        // TODO Auto-generated method stub
        super.onListItemClick(l, v, position, id);
    }

    // Creamos la clase para el adaptador de los tipos de proveedores
    public static class TipoProveedorArrayAdapter extends ArrayAdapter<TipoProveedor>
    {
        public Context context;
        public List<TipoProveedor> listaTipoProveedor;

        public TipoProveedorArrayAdapter(Context context,List<TipoProveedor> listaTipoProveedor){
            super(context, R.layout.tipoproveedorfavorito_activity,listaTipoProveedor);

            this.context = context;
            this.listaTipoProveedor = listaTipoProveedor;
        }


        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            View view;
            if(convertView==null){

                LayoutInflater layoutInflater =  (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                view = layoutInflater.inflate(R.layout.tipoproveedor_content, parent,false);                
                final ViewHolder holder = new ViewHolder();

                //holder.txtId=(TextView)view.findViewById(R.id.txtId);
                holder.txtTipoProveedor=(TextView)view.findViewById(R.id.txtTipoProveedor);

                view.setTag(holder);
                holder.txtTipoProveedor.setTag(listaTipoProveedor.get(position));
            }else{
                view=convertView;
                ViewHolder holder2 = (ViewHolder)view.getTag();
                holder2.txtTipoProveedor.setTag(listaTipoProveedor.get(position));
            }

            ViewHolder holder = (ViewHolder)view.getTag();
            TipoProveedor tipoProveedor = listaTipoProveedor.get(position);
            //holder.txtId.setText(String.valueOf(tipoProveedor.id) );
            holder.txtTipoProveedor.setText(tipoProveedor.tipoProveedor);

            return view;

        }

        private class ViewHolder{
            //public TextView txtId;
            public TextView txtTipoProveedor;

        }

    }

    private class ConsultaDB extends AsyncTask<Void, Integer, TipoProveedorArrayAdapter>{

        @Override
        protected void onPostExecute(TipoProveedorArrayAdapter result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            setListAdapter(result);
            progressDialog.dismiss();
        }

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();
            progressDialog.show();
        }

        @Override
        protected TipoProveedorArrayAdapter doInBackground(Void... params) {
            // TODO Auto-generated method stub
            String SQL = "SELECT t2.tipoProveedor AS id,t3.descripcion From Favoritos t1 " +
                    "INNER JOIN Proveedor t2 ON t2.id = t1.idProveedor " +
                    "INNER JOIN TipoProveedor t3 ON t3.id = t2.tipoProveedor " +
                    "Where t1.idUsuario = ? GROUP BY t2.tipoProveedor,t3.descripcion ORDER BY t2.tipoProveedor";

            UtilDB db = UtilDB.GetUtilDb(mContext);
            db.openDataBase();

            Cursor cursor = db.getDataBase().rawQuery(SQL,new String[]{String.valueOf(usuario.id)});
            //Cursor cursor = db.getDataBase().rawQuery(SQL,null);

            List<TipoProveedor> tipoProveedor = new ArrayList<TipoProveedor>();

            while(cursor.moveToNext()){

                long id = cursor.getLong(cursor.getColumnIndex("id"));
                String descripcion = cursor.getString(cursor.getColumnIndex("descripcion")); 

                TipoProveedor tp = new TipoProveedor();
                tp.id=id;
                tp.tipoProveedor=descripcion;
                tipoProveedor.add(tp);

            }

            cursor.close();
            db.close();

            TipoProveedorArrayAdapter adapter = new TipoProveedorArrayAdapter(mContext, tipoProveedor);
            //setListAdapter(adapter);

            return adapter;
        }

    }

}
falinsky
  • 7,229
  • 3
  • 32
  • 56
Francotte
  • 11
  • 2