在对话框中添加项目后刷新ListView

时间:2015-10-25 08:32:49

标签: android listview dialog

您好我按照Tutorial填写了一个列表视图 并且我希望listview在我在对话框表单中添加项目后自动刷新(由EditText等组成)有建议我已经遵循但很遗憾仍然无法实现它。 这是我的代码

public class Attendance extends AppCompatActivity {
// Log tag
private static final String TAG = MainActivity.class.getSimpleName();
//Attendance List json url
private static final String url = "http://10.0.2.2/MobileClassRecord/getAttendanceList.php";
private ProgressDialog pDialog;
private List<AttendanceList> attList = new ArrayList<>();
private ListView mylistView;
private CustomAttendanceListAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_attendance);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    mylistView = (ListView) findViewById(R.id.list);
    adapter = new CustomAttendanceListAdapter(this, attList);
    mylistView.setAdapter(adapter);

    pDialog = new ProgressDialog(this);
    pDialog.setMessage("Loading...");
    pDialog.show();

    JsonArrayRequest attReq = new JsonArrayRequest(url, new Response.Listener<JSONArray>() {
        @Override
        public void onResponse(JSONArray response) {
            Log.d(TAG, response.toString());
            hidePDialog();

            for (int i = 0; i < response.length(); i++) {
                try {
                    JSONObject jsonObject = response.getJSONObject(i);
                    AttendanceList alist = new AttendanceList();
                    alist.setDate(jsonObject.getString("att_date"));
                    alist.setName(jsonObject.getString("att_name"));

                    attList.add(alist);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

            adapter.notifyDataSetChanged();

        }

    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError volleyError) {
            VolleyLog.d(TAG, "Error: " + volleyError.getMessage());
            hidePDialog();
        }

    });
    AppController.getInstance().addToRequestQueue(attReq);

}

@Override
protected void onDestroy() {
    super.onDestroy();
    hidePDialog();
}

private void hidePDialog() {
    if (pDialog != null) {
        pDialog.dismiss();
        pDialog = null;
    }
}

非常感谢任何帮助:)

1 个答案:

答案 0 :(得分:0)

我只是使用这种技术:在出勤活动中把这个

@Override
public void onWindowFocusChanged(boolean hasFocus) {
  super.onWindowFocusChanged(hasFocus);
  refreshData();  
}

一旦解除AlertDialog,它就会调用刷新ListView的therefreshData()方法