3

Here is my custom List view layout.

I've added an "empty list" text view with id "android:empty", but the 'empty text' view is not displayed when the list is empty.

Any advice please.

UPDATE - updated the layout

<?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="?android:attr/listPreferredItemHeight"
    android:paddingLeft="5dip" android:paddingRight="5dip" 
    android:paddingTop="15dip" android:paddingBottom="15dip"
    android:cacheColorHint="@color/match_bg2" android:background="@color/match_bg">
    <ImageView android:id="@+id/flag_left" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:background="@drawable/flag"
        android:src="@drawable/flag_default" />
    <LinearLayout android:orientation="vertical"
        android:padding="10dip" android:layout_width="0dip" android:gravity="center"
        android:layout_weight="1" android:layout_height="fill_parent">
        <TextView android:id="@+id/item_match_label"
            android:layout_width="wrap_content" android:layout_height="0dip"
            android:layout_weight="1" android:gravity="center"
            android:textColor="@color/match_fg"  android:textStyle="bold"
            android:singleLine="true"
            android:ellipsize="marquee"
            android:focusableInTouchMode="true"
            />
    </LinearLayout>
    <ImageView android:id="@+id/flag_right" android:layout_width="wrap_content" 
        android:layout_height="wrap_content" android:background="@drawable/flag"
        android:src="@drawable/flag_default" />

    <TextView android:id="@id/android:empty"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:background="#FF0000"
              android:text="No data"/>
</LinearLayout>

Here is my Listview Activity

public class LatestMatchesListActivity extends ListActivity {
...

private void createMatchList() {

    /*
    Log.i(MY_DEBUG_TAG, "Checking Match store size before passing it to custom adapter: "+mStore.size());
    if(mStore.size() == 0){
        Log.i(MY_DEBUG_TAG, "Got Empty match store, so checking connectivity..");
        Match m = new Match();
        if(isOnline()){
            Log.i(MY_DEBUG_TAG, "Internet is accessible, so adding no matches object: ");
            m.setId(-1);
        } else{
            Log.i(MY_DEBUG_TAG, "No Internet accessible, so adding mo caonnectivity match  object: ");
            m.setId(-2);
        }
        mStore.add(m);
    } else {
        Log.e(MY_DEBUG_TAG, "Match store is not empty ");
    }
    */
    //mStore.clear();
    Log.i(MY_DEBUG_TAG, "Passing match list to custom adapter: "+mStore.toString());
    this.mAdapter = new MatchAdapter(this, R.layout.list_matches, mStore);
    this.setListAdapter(this.mAdapter);

    ListView lv = getListView();
    lv.setTextFilterEnabled(true);
    lv.setBackgroundDrawable(null);
    lv.setBackgroundResource(0);

}

and the custom ArrayAdapter

public class MatchAdapter extends ArrayAdapter<Match> {
    private ArrayList<Match> items;
    private final String MY_DEBUG_TAG = "MatchAdapter";

    public MatchAdapter(Context context, int textViewResourceId, ArrayList<Match> items) {
        super(context, textViewResourceId, items);
        this.items = items;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.list_matches, null);
        }
        Match m = items.get(position);
        if (m != null) {
            TextView itemLabel = (TextView) v.findViewById(R.id.item_match_label);
            ImageView imageFlagLeft = (ImageView) v.findViewById(R.id.flag_left);
            ImageView imageFlagRight = (ImageView) v.findViewById(R.id.flag_right);

            if(m.getId() == -1){
                itemLabel.setText("No Live Matches.");
                imageFlagLeft.setVisibility(View.INVISIBLE);
                imageFlagRight.setVisibility(View.INVISIBLE);
            } else if(m.getId() == -2){
                itemLabel.setText("No Intenet connectivity.");
                imageFlagLeft.setVisibility(View.INVISIBLE);
                imageFlagRight.setVisibility(View.INVISIBLE);
            } else {
                itemLabel.setText(m.getTeam1() + " v/s " + m.getTeam2());
                imageFlagLeft.setImageResource(getFlagResource(m.getTeam1()));
                imageFlagRight.setImageResource(getFlagResource(m.getTeam2()));
            }

        }
        return v;
    }

    private int getFlagResource(String teamName) {
        Log.i(MY_DEBUG_TAG, "Getting the flag of " + teamName);
        String flagString = "flag_" + teamName.replace(" ", "_").toLowerCase();
        int resId = getContext().getResources().getIdentifier(flagString, "drawable", "com.itflux.DroidChirp");
        if (resId != 0) {
            return resId;
        }
        return R.drawable.flag_default;
    }
}
3
  • textview is not displayed in your layout? Commented May 2, 2011 at 5:28
  • yes @vnshetty, text view is not displayed when the list is empty. Commented May 2, 2011 at 5:30
  • could you see your textview in graphical layout? Commented May 2, 2011 at 8:35

4 Answers 4

3

You have your ListView fit the screen, and in LinearLayout your TextView is displayed under that ListView so it's not visible (it would be, if you'd use a ScrollView).

You need to use as the root element of your layout a RelativeLayout for instance, to have the TextView on top of the ListView.

Sign up to request clarification or add additional context in comments.

2 Comments

I tried wrapping my layout withy a RelativeLayout and moved the TextView as you mentioned, current code can be viewed at pastebin.com/xFBqErgq But the empty text view still not displayed.
How are you triggering the TextView to show up? Can you share the relevant code as an extension to your question? thank you!
2

Seems like the question is still unsolved. So here is how I solved it. I triggered a function which will basically refresh the list layout whenever the data changes as below:

    ListView lv = getListView();
    lv.requestLayout();
    if(aa.arrayValue.size() > 0) {
        lv.setVisibility(ListView.VISIBLE);
        setListAdapter(new Adaptor(this, R.layout.main_list_item, aa.arrayValue));
    }
    else {
        lv.setVisibility(ListView.INVISIBLE);
        TextView tv = (TextView) findViewById(android.R.id.empty);
        tv.requestLayout();
        tv.setVisibility(TextView.VISIBLE);
    }

Hope this helps you out!

Comments

1

Use @+id in empty and @id in list : android:id="@+id/android:empty"

5 Comments

@Sandy added + in the id, it didn't help
It should work, can you post your Java code where you are using list?
Can you post this method: ListView lv = getListView();
@Sandy, getListView is from [here](developer.android.com/reference/android/app/… ), and i haven't overridden it.
Do you have layout which contains listView?You should add setContentView(R.layout.WhichHasListView), then ListView lv = (ListView)findViewByID(android.R.id.list);
0

Your problem is with the ListView height property. Set it to wrap_content.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.