Android:notifyDataSetChanged无效

时间:2015-12-30 09:11:00

标签: android

大家好我正在我的项目中执行删除功能,似乎notifyDataSetChanged无效。我已经对此做过一些研究,但我并不安静理解

这是我在onCreate中的代码:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_dropped_student);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        SharedPreferences preferences = getSharedPreferences("MyApp", MODE_PRIVATE);
        subj_code = preferences.getString("code", "UNKNOWN");
        subj_code_lab = preferences.getString("code_lab", "UNKNOWN");

        studentList = new ArrayList<HashMap<String, String>>();

        mylistView = (ListView) findViewById(R.id.list);
        arrayAdapter = new StudAdapter(this, stud_List);
        mylistView.setAdapter(arrayAdapter);
        new LoadStudent().execute();
        mylistView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                final String studentId = ((TextView) (view.findViewById(R.id.stud_id))).getText().toString();
                final String studentName = ((TextView) (view.findViewById(R.id.studName))).getText().toString();
                class AttemptGetData extends AsyncTask<String, String, String>{
                    String code = subj_code.toString();
                    String id = studentId;
                    String stud_name = studentName;

                    @Override
                    protected void onPreExecute() {
                        super.onPreExecute();
                        pDialog = new ProgressDialog(ViewDroppedStudent.this);
                        pDialog.setMessage("In Progress...");
                        pDialog.setIndeterminate(false);
                        pDialog.setCancelable(true);
                        pDialog.show();
                    }

                    @Override
                    protected String doInBackground(String... params) {
                        JSONParser jsonParser = new JSONParser();
                        String url = null;
                        try {
                            url = "http://192.168.22.3/MobileClassRecord/undroppedStudent.php?stud_id="+ URLEncoder.encode(id, "UTF-8")+"&subj_code="+ URLEncoder.encode(subj_code, "UTF-8");
                        }catch (UnsupportedEncodingException e){
                            e.printStackTrace();
                        }
                        List<NameValuePair> mList = new ArrayList<NameValuePair>();
                        mList.add(new BasicNameValuePair("stud_id", id));
                        mList.add(new BasicNameValuePair("subj_code", code));

                        JSONObject jsonObject = jsonParser.makeHttpRequest(url, "POST", mList);
                        Log.d("Undrop Student", jsonObject.toString());
                        try {
                            verify = jsonObject.getString("Message");
                            return verify;
                        }catch (JSONException e){
                            e.printStackTrace();
                        }

                        return null;
                    }

                    @Override
                    protected void onPostExecute(String s) {
                        super.onPostExecute(s);
                        pDialog.dismiss();
                        if (s != null){
                            Toast.makeText(getApplicationContext(), verify, Toast.LENGTH_LONG).show();
                        }
                    }
                }
                final MaterialDialog materialDialog = new MaterialDialog(ViewDroppedStudent.this);
                materialDialog.setTitle("Undrop Student");
                materialDialog.setMessage("Name: " + studentName);
                materialDialog.setPositiveButton("UNDROP", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        materialDialog.dismiss();
                        new AttemptGetData().execute();
                        arrayAdapter.notifyDataSetChanged();

                    }
                }).setNegativeButton("CANCEL", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        materialDialog.dismiss();
                    }
                });
                materialDialog.show();
            }
        });
    }

任何帮助将不胜感激:)

3 个答案:

答案 0 :(得分:4)

您需要在onPostExecute方法

中编写UITextView
arrayAdapter.notifyDataSetChanged();

答案 1 :(得分:0)

您的asynctask异步运行,因此当您在asynctask上调用execute()时,它将在并行线程中运行,同时主线程的执行继续。因此,即使在notifydatasetchanged()完成之前,您的asynctask()也会被调用。请拨打notifyDataSetChanged()

内的onPostExecute()

答案 2 :(得分:0)

  1. stud_List
  2. 中删除特定数据
  3. arrayAdapter.notifyDataSetChanged();放入onPostExecute()
  4. 有时notifiDatasetChanged可能不适用于自定义适配器。

    您可以在适配器中创建自定义方法,如下所示:

    public void refresh(ArrayList<HashMap<String, String>> list)
    {
        this.list=list; //replace this.list with your adapter list variable.
        notifyDataSetChanged();
    }
    

    在onPostExecute()

    中调用它
    arrayAdapter.refresh(stud_List);