Harsha M V
Harsha M V

Reputation: 54969

Android ListView Text Disappears on Load

When my Activity Loads on Samsung Galaxy Y and Ace Plus - mDPI/Ldpi Screens. The List View Text and Content doesnt load. On Touching and trying to scroll the Content appears.

But it works perfectly on my Development Phone Samsung S2

After some Testing i found that this is happening only on 2.X version of Android.

enter image description here

enter image description here

MainActivity.java

private final String[] places = new String[] { "Mysore", "Bangalore",
        "Mangalore", "Wayanad", "Bandipur National Park National Park",
        "Chickmaglur", "Bandipura", "Coorg", "Kodaikanal", "Hampi",
        "Ghati Subramanya", "Mekedatu", "Muththathhi", "Shivasamudram",
        "Talakadu", "Savana Durga" };

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
    setContentView(R.layout.activity_main);

    context = this;

    Log.i("Nomad", "onCreate");

    List<Place> thePlaces = new ArrayList<Place>();
    for (int i = 0; i < places.length; i++) {
        Place pl = new Place("NO_ID", places[i], "NO_DISTANCE",
                "NO_CATEGORYICON");
        thePlaces.add(pl);
    }

    listView = (ListView) findViewById(R.id.place_list);
    listView.setEmptyView(findViewById(R.id.empty));

    adapter = new PlacesListAdapter(MainActivity.this,
            R.layout.list_item_place, thePlaces);

    listView.setAdapter(adapter);
    listView.setTextFilterEnabled(true);

    mSearchView = (SearchView) findViewById(R.id.action_search);

    listView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> a, View view, int position,
                long id) {

            startActivity(new Intent(MainActivity.this, PlaceActivity.class));
        }
    });

}

PlacesListAdapter.java

public class PlacesListAdapter extends ArrayAdapter<Place> implements
        Filterable {
    public Context context;
    private List<Place> orig, itemDetailsrrayList;
    private PlaceFilter filter;

    public PlacesListAdapter(Context context, int textViewResourceId) {
        super(context, textViewResourceId);
    }

    public PlacesListAdapter(Context context, int resource, List<Place> places) {
        super(context, resource, places);
        this.context = context;
        // this.places = places;

        itemDetailsrrayList = places;
        orig = new ArrayList<Place>(itemDetailsrrayList);

        filter = new PlaceFilter();
        // imageLoader = new ImageLoader(context.getApplicationContext());

    }

    public int getCount() {
        return itemDetailsrrayList.size();
    }

    public Place getItem(int position) {
        return itemDetailsrrayList.get(position);
    }

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

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

        ViewHolder holder;
        // View view = convertView;
        // Place p = places.get(position);

        if (convertView == null) {
            LayoutInflater viewInflater;
            viewInflater = LayoutInflater.from(getContext());
            convertView = viewInflater.inflate(R.layout.list_item_place, null);

            holder = new ViewHolder();
            holder.placeTitle = (TextView) convertView
                    .findViewById(R.id.place_title);
            holder.placeDistance = (TextView) convertView
                    .findViewById(R.id.place_distance);
            holder.placeCategoryIcon = (ImageView) convertView
                    .findViewById(R.id.place_category_icon);

            convertView.setTag(holder);

        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.placeTitle.setText(itemDetailsrrayList.get(position)
                .getPlaceTitle());
        holder.placeDistance.setText("200");
        holder.placeCategoryIcon.setImageResource(R.drawable.icon_category);

        // Setting Alternative Row Colors
        if (position % 2 == 0) {
            convertView.setBackgroundResource(R.drawable.list_view_place_row_1);
        } else {
            convertView.setBackgroundResource(R.drawable.list_view_place_row_2);
        }

        return convertView;
    }

    static class ViewHolder {
        TextView placeId;
        TextView placeTitle;
        TextView placeDistance;
        ImageView placeCategoryIcon;
    }

    @Override
    public Filter getFilter() {
        // TODO Auto-generated method stub
        return filter;
    }

    private class PlaceFilter extends Filter {

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            FilterResults oReturn = new FilterResults();
            ArrayList<Place> results = new ArrayList<Place>();
            if (orig == null)
                orig = itemDetailsrrayList;
            if (constraint != null) {
                if (orig != null && orig.size() > 0) {
                    for (Place g : orig) {
                        if (g.getPlaceTitle()
                                .toLowerCase()
                                .startsWith(constraint.toString().toLowerCase()))
                            results.add(g);
                    }
                }
                oReturn.values = results;
            }
            return oReturn;
        }

        @SuppressWarnings("unchecked")
        @Override
        protected void publishResults(CharSequence constraint,
                FilterResults results) {
            itemDetailsrrayList = (ArrayList<Place>) results.values;
            notifyDataSetChanged();
        }

    }
}

acitivty_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/primary_white"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/place_list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:cacheColorHint="#00000000"
        android:fadingEdge="none" >
    </ListView>

    <TextView
        android:id="@+id/empty"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:padding="20dp"
        android:text="@string/list_view_place_empty"
        android:textColor="@color/black"
        android:textSize="18sp" />

</LinearLayout>

List View Row XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="10dp"
    android:paddingTop="10dp" >

    <ImageView
        android:id="@+id/place_category_icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:contentDescription="ss"
        android:paddingLeft="10dp"
        android:paddingRight="15dp"
        android:src="@drawable/icon_category" />

    <TextView
        android:id="@+id/place_distance"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:paddingRight="15dp"
        android:text="320" />

    <TextView
        android:id="@+id/place_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@+id/place_category_icon"
        android:ellipsize="end"
        android:paddingRight="50dp"
        android:singleLine="true"
        android:text="Place Name"
        android:textColor="#191919"
        android:textSize="18sp" />

</RelativeLayout>

Found this in the Logcat

12-23 19:58:28.033: E/ResourceType(631): Style contains key with bad entry: 0x010102f3
12-23 19:58:28.083: E/ResourceType(631): Style contains key with bad entry: 0x0101030b
12-23 19:58:28.083: E/ResourceType(631): Style contains key with bad entry: 0x0101039c
12-23 19:58:28.083: E/ResourceType(631): Style contains key with bad entry: 0x010103e1
12-23 19:58:31.852: E/ResourceType(631): Style contains key with bad entry: 0x010102f3
12-23 19:58:31.852: E/ResourceType(631): Style contains key with bad entry: 0x0101030b
12-23 19:58:31.852: E/ResourceType(631): Style contains key with bad entry: 0x0101039c
12-23 19:58:31.852: E/ResourceType(631): Style contains key with bad entry: 0x010103e1
12-23 19:58:31.934: I/Nomad(631): onCreate
12-23 19:58:32.074: I/Nomad(631): onCreateOptionsMenu
12-23 19:58:32.144: D/dalvikvm(631): GC_EXTERNAL_ALLOC freed 140K, 50% free 2834K/5639K, external 2844K/2844K, paused 59ms
12-23 19:58:32.264: I/Nomad(631): after setupSearchView()
12-23 19:58:32.264: W/KeyCharacterMap(631): No keyboard for id -1
12-23 19:58:32.264: W/KeyCharacterMap(631): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
12-23 19:58:32.473: D/dalvikvm(631): GC_EXTERNAL_ALLOC freed 41K, 50% free 2847K/5639K, external 3060K/3369K, paused 60ms

color.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <color name="white">#FFFFFF</color>
    <color name="black">#333333</color>
    <color name="primary_white">#f2f2f2</color>
    <color name="dark_bg">#161616</color>

    <!-- Place List View -->
    <color name="list_view_place_row_1">#f2f2f2</color>
    <color name="list_view_place_row_2">#e7e7e7</color>
    <color name="list_view_place_selected">#CFCFCF</color>

    <!-- Place List View -->
    <color name="primary_color">#E5492A</color>
    <color name="primary_color_inverse">#c33518</color>

</resources>

styles.xml

<resources xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- Nomad Theme -->
    <style name="Theme.Nomad" parent="Theme.Sherlock.Light">
        <item name="android:actionBarStyle">@style/Widget.Nomad.ActionBar</item>
        <item name="actionBarStyle">@style/Widget.Nomad.ActionBar</item>
        <item name="android:actionBarItemBackground">@drawable/actionbar_selectable_background</item>
        <item name="actionBarItemBackground">@drawable/actionbar_selectable_background</item>
        <item name="android:actionBarTabStyle">@style/MyActionBarTabStyle</item>
        <item name="actionBarTabStyle">@style/MyActionBarTabStyle</item>
        <item name="android:homeAsUpIndicator">@drawable/abs__ic_ab_back_holo_light</item>
        <item name="homeAsUpIndicator">@drawable/abs__ic_ab_back_holo_light</item>
        <item name="*android:actionModeShareDrawable">@drawable/ic_menu_share</item>
        <item name="actionModeShareDrawable">@drawable/ic_menu_share</item>
        <item name="searchViewSearchIcon">@drawable/ic_menu_search</item>
    </style>

    <style name="Widget.Nomad.ActionBar" parent="Widget.Sherlock.ActionBar">
        <item name="android:background">#E5492A</item>
        <item name="background">#E5492A</item>
        <item name="android:titleTextStyle">@style/TitleText</item>
        <item name="titleTextStyle">@style/TitleText</item>
    </style>

    <style name="TitleText" parent="TextAppearance.Sherlock.Widget.ActionBar.Title">
        <item name="android:textColor">@color/white</item>
    </style>

    <style name="MyActionBarTabStyle" parent="Widget.Sherlock.Light.ActionBar.TabBar">
        <item name="android:background">@drawable/actionbar_tab_bg</item>
        <item name="android:textAlignment">center</item>
        <item name="android:gravity">center</item>
    </style>

</resources>

Upvotes: 2

Views: 2117

Answers (2)

Ritesh Kumar Dubey
Ritesh Kumar Dubey

Reputation: 759

Hi @Harsha I tried your code with some basic functionality and its all working fine on my Galaxy Y. May be there is some issue caused by your color.xml file. You can download this test project and see if it helps.

Upvotes: 1

Mohammad Ersan
Mohammad Ersan

Reputation: 12444

please try to add this attribute to your ListView in your xml

android:cacheColorHint = "@android:color/transparent"

and to remove the black area, use android:fadingEdge="none" in your ListView

Upvotes: 2

Related Questions