Reputation: 1150
I want to show a refresh Button and a TextView if there is no data in the ListView adapter. I also want to be able to add a click listener to the button that will reload the list. Here is how I have my current activity defined:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.foods);
arrList=new ArrayList<FoodsData>();
listView=(ListView)findViewById(R.id.list_foods);
this.listView.setEmptyView(findViewById(R.id.emptyView));
.....
.....
}
Here is my xml file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:orientation="vertical"
android:layout_centerInParent="true"
android:id="@+id/emptyView">
<TextView
android:layout_width="wrap_content"
android:textColor="@android:color/white"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="Click Refresh to load data"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn_retry"
android:textColor="@android:color/white"
android:background="@drawable/orange_button_selecter"
android:layout_gravity="center"
android:textSize="25sp"
android:text="@string/retry"/>
</LinearLayout>
<ListView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:dividerHeight="2dp"
android:id="@+id/list_foods"/>
Where would I set the click listener for my refresh Button?
Upvotes: 4
Views: 9604
Reputation: 31
To my knowledge there is no need to clear the adapter and set notifyDataSetChanged() to the adapter. You have used List,ArrayList for the item or the Array. First clear it, and then reinitialize it, and after passing the data to costume adapter, clear the used array/list. It will work fine!
Upvotes: 0
Reputation: 765
Change your xml like below:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:orientation="vertical"
android:layout_height="fill_parent">
<FrameLayout
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_centerInParent="true"
android:id="@+id/emptyView">
<TextView
android:layout_width="wrap_content"
android:textColor="@android:color/white"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="Click Refresh to load data"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn_retry"
android:textColor="@android:color/white"
android:background="@drawable/orange_button_selecter"
android:layout_gravity="center"
android:textSize="25sp"
android:text="@string/retry"/>
</FrameLayout>
<ListView
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:dividerHeight="2dp"
android:id="@+id/list_foods"/>
</RelativeLayout>
put that lines in java
View empty = findViewById(R.id.emptyView);
btn_retry=(Button)empty.findViewById(R.id.btn_retry);
listView.setEmptyView(empty);
Now you can write onClickListener() in same Activity as it belongs with same xml layout.
btn_retry.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//Do the refresh thing or verify with Toast.
}
});
Here, If the listview is an empty it will show button refresh. Otherwise it will display the filled list.
Upvotes: 10
Reputation: 14128
First you must set Adapter to ListView, then if you need refresh listView content use: adapter.notifyDataSetChanged();
If you need remove all item use :
adapter.clear();
adapter.notifyDataSetChanged();
For example :
String[] items = new String[] { "Item1", "Item2", "Item3", "Item4" };
final ListView listView = (ListView)findViewById(R.id.listView1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_single_choice, android.R.id.text1, items);
listView.setAdapter(adapter);
Button refreshButton = (Button)findViewById(R.id.button1);
refreshButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
ArrayAdapter<String> adapter = (ArrayAdapter<String>)listview.getAdapter();
if(adapter!= null)
{
adapter.clear();
adapter.notifyDataSetChanged();
//here you can write your listView.setEmptyView(findViewById(R.id.emptyView)); code
}
});
Upvotes: 0
Reputation: 4659
You need to create a custom adapter for the list view in the following way by extending BaseAdapter
public class ListViewAdapter extends BaseAdapter {
LayoutInflater layoutInflater;
Context mContext;
ArrayList<String> slist_name;
ArrayList<String> list_deviceId;
/** constructor is initializing */
public ListViewAdapter(Context c, ArrayList<String> name,ArrayList<String> devId) {
mContext = c;
layoutInflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
slist_name = name;
list_deviceId=list_deviceId;
}
public int getCount() {
return slist_name.size();
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi = null;
convertView = null;
vi = convertView;
if (convertView == null) {
vi = layoutInflater.inflate(R.layout.listview_item, null);
TextView name_textview = (TextView) vi
.findViewById(R.id.textEmp_id);
name_textview.setText(slist_name.get(position));
Button btn = (Button) vi
.findViewById(R.id.btn_id);
btn.setOnClickListener(this);
//add relevent code in onclickListner
}
return vi;
}
}
In your activity add the custom adapter to the listView in the following way :
listView = (ListView) findViewById(R.id.listview_employee_id);
adapter = new ListViewAdapter(this, listarray, listarrayDev);
listView.setAdapter(adapter);
Upvotes: 0