Custom ListView in fragment without sticking to parent theme

I am currently having a problem using custom ListView adapters with the Holo.Light theme. Within actions and fragments, any text objects are displayed in normal theme colors ( textColorPrimary ). However, any text in the custom ListAdapter uses textColorPrimary from the default Holo theme, which makes the text unreadable.

Here is an example from the main menu of my application:




list_main_menu.xml . Row Layout for 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 need to use android:textColor="?android:attr/textColorPrimaryInverse" to read text.




fragment_main_menu.xml - fragment of the main menu.

 <?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 am not currently using any custom styles. Thanks for reading, and any helpful comments would be greatly appreciated.

EDIT 1: Here are the requested screenshots. asdf This is how it looks, if I don’t specify the text color, it seems to use the default text color for Holo Dark

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

+4
android android-listview android-theme
Jan 24 2018-12-12T00:
source share
2 answers

You have not posted any valid code, but since I had the same problem, I would suggest that you pass the wrong Context to your own adapter constructor.

I did something like this (inside my snippet):

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

All I needed to do to fix the problem was to replace getActivity().getApplicationContext() with getActivity() :

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

And he began to work as expected.

MyCursorAdapter Constructor ( SimpleCursorAdapter ):

 public MyCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to) { //... } 
+22
Feb 09 2018-12-12T00:
source share

Yes, I had the same problem and I read what Rockford wrote, I could solve it, no need to use getApplicationContext () when you implement the adapter.

Here is the code for 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; } } } 
+1
Aug 19 '13 at 21:43
source share



All Articles