ListFragment 中的数据未显示
Data in ListFragment is not being displayed
我正在尝试显示 ListFragment
中的员工列表,几天前它完全按照我的需要工作,但随后它突然停止显示数据。我尝试在 Log
中打印数据,它成功地显示了员工列表。
非常感谢任何帮助
ListFragment
public class ListEmployeeFragment extends ListFragment {
private List<DataSet> list = new ArrayList<>();
private Adapter adapter;
JSONObject obj;
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup
container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_my_leaves, container, false);
}
public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
indeterminateBarLeaveReview =
view.findViewById(R.id.indeterminateBarLeaveReview);
indeterminateBarLeaveReview.setVisibility(View.VISIBLE);
}
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
adapter = new Adapter(this.getActivity(), list);
final String tv = employerData.getString(Constants.EMAIL_SHARED_PREF,
"not available");
StringRequest request = new StringRequest(Request.Method.POST,
list_url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
if (response.equals("No record found")) {
indeterminateBar.setVisibility(View.GONE);
((TextView) getListView().getEmptyView()).setText("No
employee's to show, click on button below to add");
} else {
JSONArray jsonArray;
try {
jsonArray = new JSONArray(response);
for (int i = 0; i < jsonArray.length(); i++) {
obj = jsonArray.getJSONObject(i);
DataSet dataSet = new DataSet();
dataSet.setName(obj.getString("Name"));
dataSet.setEmail(obj.getString("Email"));
list.add(dataSet);
Log.d("ListEmp", obj.getString("Name"));
}
} catch (JSONException e) {
e.printStackTrace();
}
}
// adapter.setDataList(list);
adapter.notifyDataSetChanged();
Log.d("getting count", String.valueOf(adapter.getCount()));
indeterminateBar.setVisibility(View.GONE);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
indeterminateBar.setVisibility(View.GONE);
((TextView) getListView().getEmptyView()).setText("Failed
connecting server");
}
}) {
public Map<String, String> getParams() throws AuthFailureError {
HashMap<String, String> params = new HashMap<>();
params.put(Constants.EMAIL_SHARED_PREF, tv);
return params;
}
};
request.setRetryPolicy(new DefaultRetryPolicy(
50000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
if (getActivity() != null) {
RequestQueue requestQueue =
Volley.newRequestQueue(getActivity().getApplicationContext());
requestQueue.add(request);
setListAdapter(adapter);
}
}
}
适配器Class
public class Adapter extends BaseAdapter {
private Activity activity;
private LayoutInflater inflater;
private List<DataSet> DataList;
public Adapter(Activity activity, List<DataSet> DataList) {
this.activity = activity;
this.DataList = DataList;
}
public void setDataList(List<DataSet> DataList) {
this.DataList = DataList;
}
@Override
public int getCount() {
return DataList.size();
}
@Override
public Object getItem(int location) {
return DataList.get(location);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (inflater == null)
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
convertView = inflater.inflate(R.layout.row_layout, null);
TextView name = convertView.findViewById(R.id.tx_name);
TextView email = convertView.findViewById(R.id.tx_email);
DataSet m = DataList.get(position);
name.setText(m.getName());
email.setText(m.getEmail());
Log.d("InGetView", m.getName());
m.getCode();
m.getDesignation();
m.getPassword();
m.getDepartment();
return convertView;
}
}
fragment_list.xml
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.administrator.employeeattendance.ListEmployeeFragment">
<!-- TODO: Update blank fragment layout -->
<ProgressBar
android:id="@+id/indeterminateBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toTopOf="@+id/emp_button"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ListView
android:id="@+id/android:list"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_above="@+id/emp_button"
app:layout_constraintBottom_toTopOf="@+id/emp_button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@id/android:empty"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:gravity="center_horizontal"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
app:layout_constraintBottom_toTopOf="@+id/emp_button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<android.support.v7.widget.AppCompatImageButton
android:id="@+id/emp_button"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_alignParentBottom="true"
android:background="@color/Red"
android:src="@drawable/ic_add_employee"
android:text="@string/emp_button"
android:textSize="14sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
我自己想出来的。
只是一点点观察和阅读代码让我找到了问题。
我在 onViewCreated
中错过了一个 super
电话
public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
indeterminateBarLeaveReview = view.findViewById(R.id.indeterminateBarLeaveReview);
indeterminateBarLeaveReview.setVisibility(View.VISIBLE);
}
我正在尝试显示 ListFragment
中的员工列表,几天前它完全按照我的需要工作,但随后它突然停止显示数据。我尝试在 Log
中打印数据,它成功地显示了员工列表。
非常感谢任何帮助
ListFragment
public class ListEmployeeFragment extends ListFragment {
private List<DataSet> list = new ArrayList<>();
private Adapter adapter;
JSONObject obj;
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup
container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_my_leaves, container, false);
}
public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
indeterminateBarLeaveReview =
view.findViewById(R.id.indeterminateBarLeaveReview);
indeterminateBarLeaveReview.setVisibility(View.VISIBLE);
}
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
adapter = new Adapter(this.getActivity(), list);
final String tv = employerData.getString(Constants.EMAIL_SHARED_PREF,
"not available");
StringRequest request = new StringRequest(Request.Method.POST,
list_url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
if (response.equals("No record found")) {
indeterminateBar.setVisibility(View.GONE);
((TextView) getListView().getEmptyView()).setText("No
employee's to show, click on button below to add");
} else {
JSONArray jsonArray;
try {
jsonArray = new JSONArray(response);
for (int i = 0; i < jsonArray.length(); i++) {
obj = jsonArray.getJSONObject(i);
DataSet dataSet = new DataSet();
dataSet.setName(obj.getString("Name"));
dataSet.setEmail(obj.getString("Email"));
list.add(dataSet);
Log.d("ListEmp", obj.getString("Name"));
}
} catch (JSONException e) {
e.printStackTrace();
}
}
// adapter.setDataList(list);
adapter.notifyDataSetChanged();
Log.d("getting count", String.valueOf(adapter.getCount()));
indeterminateBar.setVisibility(View.GONE);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
indeterminateBar.setVisibility(View.GONE);
((TextView) getListView().getEmptyView()).setText("Failed
connecting server");
}
}) {
public Map<String, String> getParams() throws AuthFailureError {
HashMap<String, String> params = new HashMap<>();
params.put(Constants.EMAIL_SHARED_PREF, tv);
return params;
}
};
request.setRetryPolicy(new DefaultRetryPolicy(
50000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
if (getActivity() != null) {
RequestQueue requestQueue =
Volley.newRequestQueue(getActivity().getApplicationContext());
requestQueue.add(request);
setListAdapter(adapter);
}
}
}
适配器Class
public class Adapter extends BaseAdapter {
private Activity activity;
private LayoutInflater inflater;
private List<DataSet> DataList;
public Adapter(Activity activity, List<DataSet> DataList) {
this.activity = activity;
this.DataList = DataList;
}
public void setDataList(List<DataSet> DataList) {
this.DataList = DataList;
}
@Override
public int getCount() {
return DataList.size();
}
@Override
public Object getItem(int location) {
return DataList.get(location);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (inflater == null)
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
convertView = inflater.inflate(R.layout.row_layout, null);
TextView name = convertView.findViewById(R.id.tx_name);
TextView email = convertView.findViewById(R.id.tx_email);
DataSet m = DataList.get(position);
name.setText(m.getName());
email.setText(m.getEmail());
Log.d("InGetView", m.getName());
m.getCode();
m.getDesignation();
m.getPassword();
m.getDepartment();
return convertView;
}
}
fragment_list.xml
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.administrator.employeeattendance.ListEmployeeFragment">
<!-- TODO: Update blank fragment layout -->
<ProgressBar
android:id="@+id/indeterminateBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toTopOf="@+id/emp_button"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ListView
android:id="@+id/android:list"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_above="@+id/emp_button"
app:layout_constraintBottom_toTopOf="@+id/emp_button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@id/android:empty"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:gravity="center_horizontal"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
app:layout_constraintBottom_toTopOf="@+id/emp_button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<android.support.v7.widget.AppCompatImageButton
android:id="@+id/emp_button"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_alignParentBottom="true"
android:background="@color/Red"
android:src="@drawable/ic_add_employee"
android:text="@string/emp_button"
android:textSize="14sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
我自己想出来的。 只是一点点观察和阅读代码让我找到了问题。
我在 onViewCreated
super
电话
public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
indeterminateBarLeaveReview = view.findViewById(R.id.indeterminateBarLeaveReview);
indeterminateBarLeaveReview.setVisibility(View.VISIBLE);
}