Nikhil
Nikhil

Reputation: 185

in listview notifyDataSetChanged() methode has error

this is my main class where i am setting the adapter and calling the notifydatasetchanged() method the aim is to show the history of user in listview and refresh as the data is changed,but having error as stated earlier

public class HistoryActivity extends AppCompatActivity {

// Log tag
private static final String TAG = HistoryActivity.class.getSimpleName();
private static final String url ="http://192.168.0.102/android_login_api/historyfetch.php";
private ProgressDialog pDialog;
private List<HistoryItems> historyList = new ArrayList<HistoryItems>();
private ListView listView;
private CustomListAdapter adapter;
private SQLiteHandler db;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_history);

    listView = (ListView) findViewById(R.id.list);
    adapter = new CustomListAdapter(this, historyList);
    listView.setAdapter(adapter);

    pDialog = new ProgressDialog(this);
    // Showing progress dialog before making http request
    pDialog.setMessage("Loading...");
    pDialog.show();

    db = new SQLiteHandler(getApplicationContext());

    HashMap<String, String> user = db.getUserDetails();
    String user_email = user.get("email");
    gethistory(user_email);
}

private void gethistory(final String email)
{
    StringRequest stringRequest= new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            Log.d(TAG,response.toString());
            hideDialog();
            try {
                JSONArray jsonArray = new JSONArray(response);
                for(int i=0;i<response.length();i++)
                {
                    JSONObject obj = jsonArray.getJSONObject(i);
                    HistoryItems historyItems = new HistoryItems();
                    historyItems.setName(obj.getString("user_email"));
                    historyItems.setLat_from(obj.getDouble("latitude_from"));
                    historyItems.setLon_from(obj.getDouble("longitude_from"));
                    historyItems.setLat_to(obj.getDouble("latitude_to"));
                    historyItems.setLon_to(obj.getDouble("longitude"));
                    historyItems.setDate(obj.getString("created_at"));

                    historyList.add(historyItems);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        adapter;
    })

    {
        protected Map<String,String>getParams(){
            Map<String, String> params=new HashMap<>();
            params.put("user_email",email);
            return params;
        }
    };
    AppController.getInstance().addToRequestQueue(stringRequest);
}
private void showDialog() {
    if (!pDialog.isShowing())
        pDialog.show();
}

private void hideDialog() {
    if (pDialog.isShowing())
        pDialog.dismiss();
}

}

this is my custom adapter:-

public class CustomListAdapter extends BaseAdapter {

private Activity activity;
private LayoutInflater inflater;
private List<HistoryItems> historyItems;


public CustomListAdapter(Activity activity, List<HistoryItems> historyItems) {
    this.activity = activity;
    this.historyItems = historyItems;
}

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

@Override
public Object getItem(int location) {
    return historyItems.get(location);
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    if (inflater == null)
        inflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if (convertView == null)
        convertView = inflater.inflate(R.layout.list_row, null);


    TextView user_email = (TextView) convertView.findViewById(R.id.tv_user_email);
    TextView lat_from = (TextView) convertView.findViewById(R.id.tv_lat_from);
    TextView lon_from = (TextView) convertView.findViewById(R.id.tv_lon_from);
    TextView lat_to = (TextView) convertView.findViewById(R.id.tv_lat_to);
    TextView lon_to = (TextView) convertView.findViewById(R.id.tv_lon_to);
    TextView date = (TextView) convertView.findViewById(R.id.tv_date);

    // getting billionaires data for the row
    HistoryItems m = historyItems.get(position);


    // name
    user_email.setText(m.getUser_email());

    //
    lat_from.setText(String.valueOf(m.getLat_from()));


    lon_from.setText(String.valueOf(m.getLon_from()));

    lat_to.setText(String.valueOf(m.getLat_to()));

    lon_to.setText(String.valueOf(m.getLon_to()));

    date.setText(String.valueOf(m.getDate()));


    return convertView;
}

}

i am not getting where i made mistake any help will be helpful....

Upvotes: 0

Views: 45

Answers (3)

Shahadat Hossain Shaki
Shahadat Hossain Shaki

Reputation: 806

every time u add new item adapter will add new item in the listview.

for(int i=0;i<response.length();i++)
            {
                JSONObject obj = jsonArray.getJSONObject(i);
                HistoryItems historyItems = new HistoryItems();
                historyItems.setName(obj.getString("user_email"));
                historyItems.setLat_from(obj.getDouble("latitude_from"));
                historyItems.setLon_from(obj.getDouble("longitude_from"));
                historyItems.setLat_to(obj.getDouble("latitude_to"));
                historyItems.setLon_to(obj.getDouble("longitude"));
                historyItems.setDate(obj.getString("created_at"));

                historyList.add(historyItems);
                adapter.notifyDataSetChanged();

            }

Upvotes: 0

Raja
Raja

Reputation: 2815

you have to give notifyDataSetChanged() for your adapter...

like this

    for(int i=0;i<response.length();i++)
       {
          JSONObject obj = jsonArray.getJSONObject(i);
          HistoryItems historyItems = new HistoryItems();
          .
          .
          .
          .

          historyList.add(historyItems);
        }
adapter.notifyDataSetChanged();

Hope, It will help you :)

Upvotes: 1

ahmed shaaban
ahmed shaaban

Reputation: 116

hey man i didn't see any notifydatasetchanged() in your code i just see adapter; Solution ==> adapter.notifydatasetchanged(); should be put after catch not outside the method

Upvotes: 1

Related Questions