Skip to content

Instantly share code, notes, and snippets.

@noder123
Created March 22, 2014 07:29
Show Gist options
  • Save noder123/9702670 to your computer and use it in GitHub Desktop.
Save noder123/9702670 to your computer and use it in GitHub Desktop.
package fr.android.test.emptylistview;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class EmptyListViewTestActivity extends Activity {
private SimpleAdapter mAdapter;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ListView list = (ListView) findViewById(R.id.listView1);
list.setEmptyView(findViewById(R.id.progressBar1));
final ArrayList<HashMap<String, String>> listItem = new ArrayList<HashMap<String, String>>();
// we using thread to simulate the download of data
Thread thread = new Thread(new Runnable() {
public void run() {
// Waiting 3s
try {
Thread.sleep(2500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// fill the list with some data
HashMap<String, String> map;
map = new HashMap<String, String>();
map.put("titre", "1.0 - Apple Pie");
listItem.add(map);
map = new HashMap<String, String>();
map.put("titre", "1.1 - Banana Bread");
listItem.add(map);
map = new HashMap<String, String>();
map.put("titre", "1.5 - Cupcake");
listItem.add(map);
map = new HashMap<String, String>();
map.put("titre", "1.6 - Donut");
listItem.add(map);
map = new HashMap<String, String>();
map.put("titre", "2.1 - Eclair");
listItem.add(map);
map = new HashMap<String, String>();
map.put("titre", "2.2 - Froyo");
listItem.add(map);
map = new HashMap<String, String>();
map.put("titre", "2.3 - GingerBread");
listItem.add(map);
map = new HashMap<String, String>();
map.put("titre", "3.0 - Honeycomb");
listItem.add(map);
map = new HashMap<String, String>();
map.put("titre", "4.0 - Ice Cream Sandwich");
listItem.add(map);
map = new HashMap<String, String>();
map.put("titre", "5.0 - Jelly Bean");
listItem.add(map);
map = new HashMap<String, String>();
map.put("titre", "6.0 - Key Lime Pie");
listItem.add(map);
mAdapter = new SimpleAdapter(getBaseContext(), listItem, R.layout.item_view, new String[] { "titre" }, new int[] { R.id.item1 });
runOnUiThread(new Runnable() { // Run on the UI Thread to set the adapter to the list
public void run() {
list.setAdapter(mAdapter);
}
});
}
});
thread.start();
// Later there's no data
thread = new Thread(new Runnable() {
public void run() {
// Waiting 3s
try {
Thread.sleep(6000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
listItem.clear();
runOnUiThread(new Runnable() { // Run on the UI Thread to set the adapter to the list
public void run() {
list.setEmptyView(findViewById(R.id.emptyText)); // set a new empty view to tell that there's no more data
mAdapter.notifyDataSetChanged();
}
});
}
});
thread.start();
}
}
<?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:orientation="vertical" >
<TextView
android:id="@+id/item1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
android:padding="20dp" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<!-- Use a FrameLayout to center the empty views -->
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
<ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:visibility="gone" />
<TextView
android:id="@+id/emptyText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="No data available"
android:visibility="gone" />
</FrameLayout>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment