当列表为空时,我的ListView
未显示空视图。
这是我的Fragment
代码和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) {
}
}
这是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>
答案 0 :(得分:1)
我可以看到两个可能的问题:
1)您的TextView(id == empty)可能隐藏在屏幕底部 - 设置ListView
到View.GONE
的可见性空
或者使用ListView.setEmptyView()
从xml中删除TextView
并以编程方式创建
TextView tvEmpty = new TextView(context);
tvEmpty .setText(...);
lvClientList.setEmptyView(tvEmpty);
2)您搜索TextView就像
tvEmpty = (TextView) rootView.findViewById(android.R.id.empty);
android
这是一个印刷错误 - 用
tvEmpty = (TextView) rootView.findViewById(R.id.empty);
击> <击> 撞击>
答案 1 :(得分:0)
在设置适配器
之前设置setEmptyView()方法答案 2 :(得分:0)
getClients方法的catch块存在问题。该方法在异常时返回,并且存在静默异常。 我从catch块中删除了return语句,解决了我的问题。
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;
}