-1

I'm using view pager with swiping tab layouts. And i'm displaying list view of data using custom adapter. And also onclick of list view i have a list view detail activity where I'm displaying data in more detail. In these detail activity i'm performing some changes to the data(some post method). after that I create an instance of customAdapter class and call notifyDataSetChanged() in order to refresh list view. My problem over here is the list view some times refreshes quickly and some times there is a delay of some seconds. So, Can somebody suggest me proper usage of list view and what changes needs to be done in order to refresh list view whenever a post method is performed. My code Fragment class:

private void showJsonData(String response) {

    try {
        String serviceID = LoggedInUserStore.getLoggedInServiceId(getContext());
        List<Complaint> userList = new ArrayList<>(); //ArrayList of type user(POJO CLASS)
        JSONArray jsonArray = new JSONArray(response);

        for (int i = 0; i < jsonArray.length(); i++) {
            if (serviceID.equals(jsonArray.getJSONObject(i).getString("ServiceID"))) {
                if (jsonArray.getJSONObject(i).getString("CallStatusID").equalsIgnoreCase("1")) {
                    userList.add(0, Complaint.fromJson(jsonArray.getJSONObject(i))); //
                }
            }

        }

        assignAdapter = new AssignAdapter(getActivity(), userList);
        listView.setAdapter(assignAdapter);
        listView.invalidateViews();
        assignAdapter.notifyDataSetChanged();

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

}

My custom adapter class
public class AssignAdapter extends BaseAdapter implements Filterable {
List<Complaint> ticket = new ArrayList<>();
private Context context;
String ticketNo, complaint, raiseDate;
Complaint user;
List<Complaint> temporaryList = new ArrayList<>();

/*String status, priority;*/

public AssignAdapter(Context context, List<Complaint> ticket) {
    this.context = context;
    this.ticket = ticket;
    this.temporaryList = ticket;
}

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

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

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

public class viewHolderItem {
    TextView ticketNumberText, complaintNameText, raisedDateText;
}

//Set the layout for the fragment and return it.
@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    viewHolderItem holder;
    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.custom_list_view, null, true);

        holder = new viewHolderItem();
        holder.ticketNumberText = (TextView) convertView.findViewById(R.id.ticketIdSupervisor);
        holder.complaintNameText = (TextView) convertView.findViewById(R.id.complaintNameSupervisor);
        convertView.setTag(holder);
    } else {
        holder = (viewHolderItem) convertView.getTag();
    }

    user = temporaryList.get(position);
    if (user != null) {
        //Get the Ticket Number
        Typeface custom_font = Typeface.createFromAsset(context.getAssets(), "fonts/DroidSerif.ttf");
        ticketNo = temporaryList.get(position).getTicketNumber();
        holder.ticketNumberText.setText(ticketNo);
        holder.ticketNumberText.setTag("ticketNumber");
        holder.ticketNumberText.setTypeface(custom_font);


        //Get the complaint Name
        complaint = temporaryList.get(position).getComplaintDetails();
        holder.complaintNameText.setText(complaint);
        holder.complaintNameText.setTag("complaint");
        holder.complaintNameText.setTypeface(custom_font);

    }
    convertView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(context.getApplicationContext(), ComplaintDetailsSupervisor.class);
            i.putExtra("COMPLAINT NAME", temporaryList.get(position).getComplaintDetails());
            i.putExtra("RAISED DATE", temporaryList.get(position).getRaisedDate().substring(0, 10));
            context.startActivity(i);
        }
    });
    notifyDataSetChanged();
    return convertView;
}
public void notifyDataSetChanged() {
    super.notifyDataSetChanged();
}

}

My List view detail activity class

 dialogButtonOk.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        try {
                            assignComplaint();
                            al.remove(position);

                            AssignAdapter assignAdapter = new AssignAdapter(getApplicationContext(), al);
                            assignAdapter.notifyDataSetChanged();

                            ComplaintDetailsSupervisor.this.finish();


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

                dialogButtonNo.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                    }
                });

                dialog.show();
            }
        }
    });


}

public void assignComplaint() throws JSONException {
    //my custom method...
}

In the list view detail activity class i'm doing this

al.remove(position);
AssignAdapter assignAdapter = new AssignAdapter(getApplicationContext(), al);
assignAdapter.notifyDataSetChanged();
ComplaintDetailsSupervisor.this.finish();

Removing the position of list view and immediately calling adapter. This works fine but I don't know why sometimes it does not refreshes..May be when list view has a single item it does not refreshes immediately.

Rakesh
  • 43
  • 1
  • 1
  • 10
  • In what cases does it not refresh immediately? – barq Apr 26 '16 at 06:02
  • Say when there are some 5-6 items in list view when I try to perform some post method the item is not removed.. When I perform post method item has to be removed from list view. But some times it does not goes off..until I do swipe up to refresh. – Rakesh Apr 26 '16 at 06:06
  • I'm finishing lv detail activity when users perform post method and will send him back to fragment where data is displayed in list view with little information. The item needs to be removed from that fragment which does not happens some times. – Rakesh Apr 26 '16 at 06:09

1 Answers1

0

You are creating a new adapter and calling notifyDatasetChanged on it but have not called setAdapter with the new adapter as a parameter, hence why your list ist not refreshed.

You need to call

setAdapter(assignAdapter)

or reuse your existing assignAdapter and then call notifyDatasetChanged() on it.

barq
  • 3,681
  • 4
  • 26
  • 39
  • I cant set the adapter to my list view. because it is defined in my fragment. & I'm removing the item from the array list in my activity and calling notifyDataSetChanged. – Rakesh Apr 26 '16 at 06:49
  • In the onClickListener of your button you are creating a new assignAdapter and never setting it as an adapter to the list. So this adapter is never used. – barq Apr 26 '16 at 07:18
  • I did a lot of search but didn't find a solution.. Sir, Can you tell me how to set adapter for the list view which is defined in Fragment class. – Rakesh Apr 26 '16 at 09:15
  • I have used a view pager and defined fragments. The view pager is defined in home activity. And The list view is in one of those fragments. So I'm not getting on how to set the adapter for that list view. – Rakesh Apr 26 '16 at 09:27