简体   繁体   中英

ListView doesn't refresh with notifydatasetchanged

I have a MySQL database of two columns displayed in an android ListView . I use Retrofit 1.9 to get the data from MySQL. The adapter for the ListView is a BaseAdapter and I don't have a DatabaseHelper class. When I add a data in the ListView from my mobile phone, it doesn't refresh the ListView . I have to close and restart the app. I try to refresh with listViewAdapter.notifyDataSetChanged(); .

This is the fragment where the listview is:

public class rightFragment extends Fragment {

String BASE_URL = "http://awebsite.com";

View view;
ListView listView;
ListViewAdapter listViewAdapter;

Button buttondisplay;
Button buttonadd;
EditText new_id;
EditText newWordFra;
EditText newWordDeu;

ArrayList<String> id = new ArrayList<>();
ArrayList<String> fra = new ArrayList<>();
ArrayList<String> deu = new ArrayList<>();

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    view = inflater.inflate(com.example.geeko.Deu.R.layout.fragment_right, container, false);

    listView = (ListView) view.findViewById(com.example.geeko.Deu.R.id.listView); //<<<< ADDED NOTE use your id
    listViewAdapter = new ListViewAdapter(getActivity(), id, fra, deu);

    displayData();

    buttondisplay = (Button) view.findViewById(com.example.geeko.Deu.R.id.buttondisplay);
    buttonadd = (Button) view.findViewById(com.example.geeko.Deu.R.id.buttonadd);
    buttondelete = (Button) view.findViewById(com.example.geeko.Deu.R.id.newID);
    newWordFra = (EditText) view.findViewById(com.example.geeko.Deu.R.id.newWordFra);
    newWordDeu = (EditText) view.findViewById(com.example.geeko.Deu.R.id.newWordDeu);

    buttonadd.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //Toast.makeText(MainActivity.this , "button add", Toast.LENGTH_LONG).show();
            insert_data();
           listViewAdapter.notifyDataSetChanged();

        }
    });

    buttondisplay.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if (buttondisplay.getText().toString().contains("Show")) {
                listView.setAdapter(listViewAdapter);
                buttondisplay.setText("Hide");
            } else {
                listView.setAdapter(null);
                buttondisplay.setText("Show");
            }
        }
    });


    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long ids) {

            new_id.setText(id.get(position));
            newWordFra.setText(fra.get(position));
            newWordDeu.setText(deu.get(position));

        }
    });

    return view;

}

public void insert_data() {

    RestAdapter adapter = new RestAdapter.Builder()
            .setEndpoint(BASE_URL) //Setting the Root URL
            .build();

    AppConfig.insert api = adapter.create(AppConfig.insert.class);

    api.insertData(
            newWordFra.getText().toString(),
            newWordDeu.getText().toString(),
            new Callback<Response>() {
                @Override
                public void success(Response result, Response response) {

                    try {

                        BufferedReader reader = new BufferedReader(new InputStreamReader(result.getBody().in()));
                        String resp;
                        resp = reader.readLine();
                        Log.d("success", "" + resp);

                        JSONObject jObj = new JSONObject(resp);
                        int success = jObj.getInt("success");

                        if(success == 1){
                            Toast.makeText(getActivity(), "Successfully inserted", Toast.LENGTH_SHORT).show();
                        } else{
                            Toast.makeText(getActivity(), "Insertion Failed", Toast.LENGTH_SHORT).show();
                        }

                    } catch (IOException e) {
                        Log.d("Exception", e.toString());
                    } catch (JSONException e) {
                        Log.d("JsonException", e.toString());
                    }
                }

                @Override
                public void failure(RetrofitError error) {
                    Toast.makeText(getActivity(), error.toString(), Toast.LENGTH_LONG).show();
                }
            }
    );
}

public void displayData() {

    RestAdapter adapter = new RestAdapter.Builder()
            .setEndpoint(BASE_URL) //Setting the Root URL
            .build();

    AppConfig.read api = adapter.create(AppConfig.read.class);

    api.readData(new Callback<JsonElement>() {
                     @Override
                     public void success(JsonElement result, Response response) {

                         String myResponse = result.toString();
                         Log.d("response", "" + myResponse);

                         try {
                             JSONObject jObj = new JSONObject(myResponse);

                             int success = jObj.getInt("success");

                             if (success == 1) {

                                 JSONArray jsonArray = jObj.getJSONArray("details");
                                 for (int i = 0; i < jsonArray.length(); i++) {

                                     JSONObject jo = jsonArray.getJSONObject(i);

                                     id.add(jo.getString("id"));
                                     fra.add(jo.getString("fra"));
                                     deu.add(jo.getString("deu"));


                                 }

                                 listView.setAdapter(listViewAdapter);


                             } else {
                                 Toast.makeText(getActivity(), "No Details Found", Toast.LENGTH_SHORT).show();
                             }
                         } catch (JSONException e) {
                             Log.d("exception", e.toString());
                         }
                     }

                     @Override
                     public void failure(RetrofitError error) {
                         Log.d("Failure", error.toString());
                         Toast.makeText(getActivity(), error.toString(), Toast.LENGTH_LONG).show();
                     }
                 }
    );
  }

}

This is the listViewAdpater class :

public class ListViewAdapter extends BaseAdapter {

private final Context context;
private ArrayList<String> id = new ArrayList<String>();
private ArrayList<String> fra = new ArrayList<String>();
private ArrayList<String> deu = new ArrayList<String>();
LayoutInflater layoutInflater;

public ListViewAdapter(Context ctx, ArrayList<String> id, ArrayList<String> fra, ArrayList<String> deu) {

    this.context = ctx;
    this.id = id;
    this.fra = fra;
    this.deu = deu;

}

@Override
public int getCount() {
    return id.size();
}

@Override
public Object getItem(int position) {
    return id.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@SuppressLint("ViewHolder")
@Override
public View getView(final int position, View view, ViewGroup parent) {

    final Holder holder;
    if (view == null) {
        layoutInflater = (LayoutInflater) context.getSystemService
                (Context.LAYOUT_INFLATER_SERVICE);

        view = layoutInflater.inflate(R.layout.list_item, null);
        holder = new Holder();
        holder.txt_fra = (TextView) view.findViewById(R.id.fra);
        holder.txt_deu = (TextView) view.findViewById(R.id.deu);
        view.setTag(holder);
    } else {

        holder = (Holder) view.getTag();
    }

        holder.txt_fra.setText(fra.get(position));
        holder.txt_deu.setText(deu.get(position));


        return view;
    }

    static class Holder {
        TextView txt_fra, txt_deu;
    }

}

Should I create a method to refresh my ListView ?

First of all I would suggest to remove the unnecessary initialization of your variables inside the Adapter:

private ArrayList<String> id = new ArrayList<String>();
private ArrayList<String> fra = new ArrayList<String>();
private ArrayList<String> deu = new ArrayList<String>();

By assigning the pointer from your fragment you will already assign an initialized ArrayList. You want both pointers to point at the same ArrayList Object so changes apply to both.

Apparently, the data stored in the Adapter is not being updated correctly. I would suggest to debug your app - setting the breakpoint so that you can see the data that the Adapter stores after an update.

The idea of writing a method for updates has already been implemented with the notifyDataSetChanged() . Just override the method in your adapter and do eventual changes before calling the super.notifyDataSetChanged() .

If you have any success with those changes let us know.

buttonadd.setOnClickListener(new View.OnClickListener() {
        ...
        insert_data();
       listViewAdapter.notifyDataSetChanged();

    }
});

In your onClickListener , you are calling insert_data() , followed by listViewAdapter.notifyDataSetChanged()

insert_data() method is sending a network request to retrieve the data to populate the list. BUT, you are calling notifyDataSetChanged BEFORE the network request is done. That's why the listView is empty, and will stay empty. You need to wait AFTER the network request and AFTER you have populated your ArrayList with the data to call notifyDataSetChanged . How do we know the network request is done? Simply at the end of the callback (which you've implemented):

new Callback<Response>() {
            @Override
            public void success(Response result, Response response) {...

At the end of the success method, you call listViewAdapter.notifyDataSetChanged() method.

Hope that makes sense! Try it and let us know what happens

I've founded a solution. I've added getActivity.recreate(); in the method insert_data .

public void success(Response result, Response response) {

                    try {

                        BufferedReader reader = new BufferedReader
                                (new InputStreamReader(result.getBody().in()));
                        String resp;
                        resp = reader.readLine();
                        Log.d("success", "" + resp);

                        JSONObject jObj = new JSONObject(resp);
                        int success = jObj.getInt("success");

                        if(success == 1){
                            Toast.makeText(getActivity(), "Successfully inserted",
                                    Toast.LENGTH_SHORT).show();
                            getActivity().recreate();

                        } else{
                            Toast.makeText(getActivity(), "Insertion Failed",
                                    Toast.LENGTH_SHORT).show();
                        }

                    } catch (IOException e) {
                        Log.d("Exception", e.toString());
                    } catch (JSONException e) {
                        Log.d("JsonException", e.toString());
                    }


                }

I'm not sure that is the best solution but it works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM