未显示ListFragment中的数据

时间:2018-02-15 06:12:09

标签: android json fragment android-volley android-listfragment

我正在尝试显示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);
    }
  }
}

适配器类

  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" />

1 个答案:

答案 0 :(得分:0)

我自己想通了。 只需一点观察并阅读代码就可以找到问题所在。

我错过了super

中的onViewCreated来电
   public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    indeterminateBarLeaveReview = view.findViewById(R.id.indeterminateBarLeaveReview);
    indeterminateBarLeaveReview.setVisibility(View.VISIBLE);
}