Reputation:
In this code, after inserting new items to adaper, my list could not refresh and update with notifyDataSetChanged()
. For example, for this line my adapter could set without any problem:
getRequestFromServer(0, 10);
adapter = new ReceivedAdapter(G.context, items);
setListAdapter(adapter);
After that I have 10 items in list and adapter.
private String getRequestFromServer(long lastID, int count) {
String received = "";
try {
received = new JsonService(config_username, config_password, lastID, count, G.F_RECEIVE_SMS).request();
JSONArray data_array = new JSONArray(received);
String mUserID = config_username;
for (int i = 0; i < data_array.length(); i++) {
JSONObject json_obj = data_array.getJSONObject(i);
String mLastID = json_obj.getString("id_recived_sms");
String mSmsBody = json_obj.getString("sms_body");
String mSmsNumber = json_obj.getString("sms_number");
String mSenderName = json_obj.getString("mobile_number");
String mContactName = json_obj.getString("contact_name");
String mDate = json_obj.getString("recived_date");
ReceivedItemStructure item = new ReceivedItemStructure(
mLastID,
mUserID,
mSmsBody,
mSmsNumber,
mSenderName,
mContactName,
mDate
);
items.add(item);
}
} catch (Exception e) {
e.printStackTrace();
}
return received;
}
items.add(item);
count is that 10
. Now, in this below function after getting a new item from the server, my list count can change and update to 11:
private void addDataToList(String LastID, String SmsBody, String SmsNumber, String SenderName, String ContactName, String Date) {
String mLastID = LastID;
String mUserID = config_username;
String mSmsBody = SmsBody;
String mSmsNumber = SmsNumber;
String mSenderName = SenderName;
String mContactName = ContactName;
String mDate = Date;
ReceivedItemStructure item = new ReceivedItemStructure(
mLastID,
mUserID,
mSmsBody,
mSmsNumber,
mSenderName,
mContactName,
mDate
);
items.add(item);
adapter.update(items);
adapter.notifyDataSetChanged();
}
items.add(item);
count is that 11
. I update the adapter with adapter.update()
, and in the adapter my list after using the addDataToList()
function is 11
, but adapter.notifyDataSetChanged();
doesn't work and I don't see the new item in adapter and list view. Inserting and adding a new item into items
is always OK and I don't have any problem.
My adapter is:
public class ReceivedAdapter extends ArrayAdapter<ReceivedItemStructure> {
private ArrayList<ReceivedItemStructure> list;
public ReceivedAdapter(Context c, ArrayList<ReceivedItemStructure> items) {
super(c,0,items);
list = items;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ReceiveItemView itemView = (ReceiveItemView)convertView;
if (null == itemView)
itemView = ReceiveItemView.inflate(parent);
itemView.setItem(getItem(position));
return itemView;
}
public void update(ArrayList<ReceivedItemStructure> items) {
list = items;
/* this line could update list method*/
}
}
Upvotes: 0
Views: 2467
Reputation: 2560
Change the following code:
items.add(item);
adapter.update(items);
adapter.notifyDataSetChanged();
to:
adapter.add(item);
adapter.notifyDataSetChanged();
I think this should solve your problem.
You don't need to use a list to store items in an Adapter that is extended the ArrayAdatper, because it will maintain a list of items for you.
Update:
Then try to pass a copy of your items List to your adapter. Try the following change:
adapter = new ReceivedAdapter(G.context, items);
to:
adapter = new ReceivedAdapter(G.context, new ArrayList<ReceivedItemStructure>(items));
When you pass a List instance to an ArrayAdapter via its constructor, it will hold than instance directly, means that every change you make to that List instance outside the ArrayAdapter will affect its data set as well.
Upvotes: 1