Reputation: 4649
My ListView
is not showing empty view when the list is empty.
Here is my code of Fragment
and the Layout
.
public class ViewSearchClientFragment extends BaseFragment implements
OnItemClickListener {
ArrayList<ClientDetails> clientArrayList;
ListView lvClientList;
ClientListAdapter adapter;
ServerDetails serverDetails;
long totalresult = 0;
int totalpages = 0;
Boolean flag_loading = false;
EditText etTerm;
TextView tvEmpty;
private int limitstart = 0;
private int limitnum = 30;
private int totalresults;
private int numreturned = 0;
public static ViewSearchClientFragment newInstance(int groupPosition,
int ChildPosition) {
ViewSearchClientFragment fragment = new ViewSearchClientFragment();
Bundle args = new Bundle();
args.putInt("group_position", groupPosition);
args.putInt("child_position", ChildPosition);
args.putString("title", "View/Search Clients");
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_view_search_clients,
container, false);
lvClientList = (ListView) rootView.findViewById(R.id.lvClients);
/*
* lvClientList.addHeaderView(View.inflate(mActivity,
* R.layout.client_listview_header, null));
*/
tvEmpty = (TextView) rootView.findViewById(android.R.id.empty);
tvEmpty.setText("");
clientArrayList = new ArrayList<ClientDetails>();
adapter = new ClientListAdapter(mActivity, clientArrayList);
lvClientList.setAdapter(adapter);
lvClientList.setEmptyView(tvEmpty);
lvClientList.setOnItemClickListener(this);
registerForContextMenu(lvClientList);
lvClientList.setOnScrollListener(new OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
if (firstVisibleItem + visibleItemCount == totalItemCount
&& totalItemCount != 0) {
if (!flag_loading) {
if ((limitstart + numreturned) < totalresults) {
limitstart += limitnum;
getClients(null);
}
}
}
}
});
serverDetails = new ServerDetails(mActivity);
getClients(null);
return rootView;
}
int positionLongClick;
private void getClients(final String[] params) {
flag_loading = true;
ProgressDialogManager.showDialog(mActivity);
Response.Listener<JSONObject> responseListner = new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject arg0) {
// TODO Auto-generated method stub
try {
JSONObject jobj = arg0;
String result = jobj.getString("result");
if (result.contains("success")) {
totalresults = Integer.parseInt(jobj
.getString("totalresults"));
limitstart = Integer.parseInt(jobj
.getString("startnumber"));
numreturned = Integer.parseInt(jobj
.getString("numreturned"));
JSONObject temp = jobj.getJSONObject("clients");
JSONArray array = temp.getJSONArray("client");
for (int i = 0; i < array.length(); i++) {
JSONObject tempobject = array.getJSONObject(i);
ClientDetails detail = new ClientDetails();
clientArrayList.add(detail);
detail.name = (tempobject.getString("firstname")
+ " " + tempobject.getString("lastname"));
detail.id = Integer.parseInt((tempobject
.getString("id")));
detail.company = (tempobject
.getString("companyname"));
detail.email = (tempobject.getString("email"));
detail.status = (tempobject.getString("status"));
}
} else if (result.contains("error")) {
ProgressDialogManager.hideDialog();
Dialog d = new Dialog(mActivity,
jobj.getString("message"));
d.show();
return;
}
} catch (JSONException e) {
// TODO Auto-generated catch block
ProgressDialogManager.hideDialog();
e.printStackTrace();
return;
}
tvEmpty.setText("No Client Found");
tvEmpty.setVisibility(View.VISIBLE);
ProgressDialogManager.hideDialog();
adapter.notifyDataSetChanged();
flag_loading = false;
}
};
JsonObjectRequest request = new JsonObjectRequest(Method.POST,
serverDetails.getUrl(), null, responseListner,
MyErrorListner.getListnerEmptyView(mActivity, tvEmpty,
"No Client Found")) {
@Override
public String getBodyContentType() {
// TODO Auto-generated method stub
return "application/x-www-form-urlencoded";
}
@Override
public byte[] getBody() {
Uri.Builder builder = serverDetails.getBodyBuilder();
builder.appendQueryParameter("action", "getclients");
if (params != null) {
builder.appendQueryParameter("search", params[0]);
}
builder.appendQueryParameter("limitstart", limitstart + "");
builder.appendQueryParameter("limitnum", limitnum + "");
return builder.build().getEncodedQuery().getBytes();
}
};
AppController.getInstance().addToRequestQueue(request);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
}
}
And this is the Layout
.
<?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:background="@drawable/gradiant_background"
android:orientation="vertical" >
<EditText
android:id="@+id/etFilterTerm"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:hint="Search Client"
android:imeOptions="actionSearch"
android:inputType="text"
android:drawableLeft="@android:drawable/ic_menu_search"/>
<ListView
android:id="@+id/lvClients"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="@null"
android:dividerHeight="5dp"
android:padding="8dp" />
<TextView
android:id="@android:id/empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="No Clients Found"
android:textAppearance="@android:style/TextAppearance.Large"
android:visibility="gone" />
</LinearLayout>
Upvotes: 0
Views: 839
Reputation: 4649
There was a problem in catch block of getClients method. The method was returning on exception and there was a silent exception. I removed the return statement from the catch block and my problem solved.
catch (JSONException e) {
// TODO Auto-generated catch block
ProgressDialogManager.hideDialog();
e.printStackTrace();
}
tvEmpty.setText("No Client Found");
tvEmpty.setVisibility(View.VISIBLE);
ProgressDialogManager.hideDialog();
adapter.notifyDataSetChanged();
flag_loading = false;
}
Upvotes: 0
Reputation: 3296
I can see 2 possible problems:
1) Your TextView (id==empty) may hide under the bottom of the screen - set visibility of your ListView
to View.GONE
when it's empty
OR to use ListView.setEmptyView()
remove TextView
from xml and just create it programmatically like
TextView tvEmpty = new TextView(context);
tvEmpty .setText(...);
lvClientList.setEmptyView(tvEmpty);
2) You search your TextView like
tvEmpty = (TextView) rootView.findViewById(android.R.id.empty);
android
here is a misprint - replace it by
tvEmpty = (TextView) rootView.findViewById(R.id.empty);
Upvotes: 1