adapter.notifyDataSetChanged();不工作

时间:2017-09-04 10:06:21

标签: android mysql

我想更新我的列表视图作为我的MySQL数据库更新。为此,我使用了notifyDataSetChanged();但不知怎的,它不起作用。我用它与适配器,但仍然,我得到一个方法的错误无法解决。你能告诉我我做错了什么吗?我不明白。感谢任何帮助

代码:

package com.example.mi.mikpiadmin;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

public class See_Feedback extends AppCompatActivity implements ListView.OnItemClickListener {

    private ListView listView;

    private String JSON_STRING;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.see_feedback);
        listView=(ListView)findViewById(R.id.list_view) ;
        listView.setOnItemClickListener(this);
        getJSON();

    }


    private void showEmployee(){
        JSONObject jsonObject = null;
        ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String, String>>();
        try {
            jsonObject = new JSONObject(JSON_STRING);
            JSONArray result = jsonObject.getJSONArray(Config.TAG_JSON_ARRAY);

            for(int i = 0; i<result.length(); i++){
                JSONObject jo = result.getJSONObject(i);
                String id = jo.getString(Config.TAG_NAME);
                String name = jo.getString(Config.TAG_FEEDBACK);

                HashMap<String,String> employees = new HashMap<>();
                employees.put(Config.TAG_NAME,id);
                employees.put(Config.TAG_FEEDBACK,name);
                list.add(employees);

            }

        } catch (JSONException e) {
            e.printStackTrace();
        }




         ListAdapter adapter = new SimpleAdapter(
                See_Feedback.this, list, R.layout.list_item,
                new String[]{Config.TAG_NAME,Config.TAG_FEEDBACK},
                new int[]{R.id.id, R.id.name});


        listView.setAdapter(adapter);
       adapter.notifyDataSetChanged();
    }



    private void getJSON(){
        class GetJSON extends AsyncTask<Void,Void,String> {

           private ProgressDialog loading;
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                loading = ProgressDialog.show(See_Feedback.this,"Fetching Data","Wait...",false,false);
            }

            @Override
            protected void onPostExecute(String s) {
                super.onPostExecute(s);
                loading.dismiss();
                JSON_STRING = s;
                showEmployee();
            }

            @Override
            protected String doInBackground(Void... params) {
                RequestHandler rh = new RequestHandler();
                String s  = rh.sendGetRequest(Config.URL_GET_ALL);
                return s;
            }
        }
        GetJSON gj = new GetJSON();
        gj.execute();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        Intent intent = new Intent(this, See_Feedback.class);
        HashMap<String,String> map =(HashMap)parent.getItemAtPosition(position);

        String empId = map.get(Config.TAG_ID).toString();
        intent.putExtra(Config.EMP_ID,empId);
        startActivity(intent);
    }


}

1 个答案:

答案 0 :(得分:0)

protected void onPostExecute(ArrayList<String> itemsStrArrList) {
    arrListItems.clear();
    arrListItems.addAll(itemsStrArrList);
    adapter.notifyDataSetChanged();
}