2015-12-30 3 views
0

Привет, ребята, я делаю функцию delete в моем проекте, и кажется, что notifyDataSetChanged не работает. Я уже сделал некоторые исследования по этому поводу, но я не понимаю, тихо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(); 
      } 
     }); 
    } 

любая помощь была бы оценена :)

+0

вы пытались поставить этот arrayAdapter.notifyDataSetChanged(); in onPostExecute() – AbuQauod

+0

Я не уверен, но попробуйте вызвать этот метод (notifyDataSetChanged) в onPostExecute(). Потому что вы вызываете этот метод после asyntask. –

+0

hi sir @MohammadAbuQauod не повезло :( – CallMeJeo

ответ

4

Вы должны написать arrayAdapter.notifyDataSetChanged(); в методе onPostExecute

    @Override 
        protected void onPostExecute(String s) { 
         super.onPostExecute(s); 
         pDialog.dismiss(); 
         if (s != null){ 
          Toast.makeText(getApplicationContext(), verify, Toast.LENGTH_LONG).show(); 
         } 
         arrayAdapter.notifyDataSetChanged(); 
        } 
0

Ваш asynctask работает асинхронном так при вызове Execute() на asynctask он будет работать в параллель при продолжении выполнения основного потока. Таким образом, ваш notifydatasetchanged() вызывается еще до завершения вашего asynctask(). Так называют notifyDataSetChanged() внутри onPostExecute()

0
  1. Удалить perticular данных из stud_List
  2. поставил arrayAdapter.notifyDataSetChanged(); в onPostExecute()

Вполне возможно, что когда-то 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); 
+0

привет, сэр, пожалуйста, см. мой полный код https://drive.google.com/file/d/0BzRtxsi3HCNWSjRRZXVTRU54NjQ/view?usp=sharing I все еще тихо понимаю :( – CallMeJeo

Смежные вопросы