问题
in this code after insterting new items to adapert my list could not refresh and update with notifyDataSetChanged()
for example for this line my adapter could set without any problem.
adapter = new ReceivedAdapter(context, items);
getRequestFromServer(0, 10);
after that i have 10 item 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);
}
setListAdapter(adapter);
} catch (Exception e) {
e.printStackTrace();
}
return received;
}
items.add(item);
count is that 10
. now in this bewlo function after get new i item from 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.setRow(items);
adapter.notifyDataSetChanged();
}
items.add(item);
count is that 11
.i'm update adapter with adapter.setRow()
and into ReceivedAdapter
my List
size is 11
. my list after using addDataToList()
function is 11
but adapter.notifyDataSetChanged();
dont work and i dont see new item in adapter and list view. all ways of add and update List
with new item with pass items
is OK and ReceivedAdapter and i dont have problem.
my Adapter is :
public class ReceivedAdapter extends BaseAdapter
{
private LayoutInflater inflater;
private List<ReceivedItemStructure> row;
public ReceivedAdapter(Context context, List<ReceivedItemStructure> row)
{
this.row = row;
inflater = LayoutInflater.from(context);
}
public void setRow(List<ReceivedItemStructure> row) {
this.row = row;
}
@Override
public int getCount() {
return row.size();
}
@Override
public ReceivedItemStructure getItem(int position) {
return row.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
if(convertView ==null){
convertView = inflater.inflate(R.layout.received_sms_list_fragment, null);
}
TextView tv_smsBody = (TextView)convertView.findViewById(R.id.tv_smsBody);
tv_smsBody.setText(getItem(position).getmSmsBody());
TextView tv_smsSender = (TextView)convertView.findViewById(R.id.tv_smsSender);
tv_smsSender.setText(getItem(position).getmSmsBody());
TextView tv_smsDate = (TextView)convertView.findViewById(R.id.tv_smsDate);
tv_smsDate.setText(getItem(position).getmDate());
return convertView;
}
}
回答1:
Instead of creating a new items
each time and setting it to adapter.. try to add and remove from the adapter directly.
First initialize the adapter with initial values you need or an empty list
.
- Next when you want to add.. call
adapter.add(obj)
- When you want to remove call
adapter.remove(obj)
- When you want to empty the adapter call
adapter.clear()
Finally call the adapter.notifyDatasetChanged()
whenever you need the changes to be reflected
回答2:
Try this way,hope this will help you to solve your problem.
public class ReceivedAdapter extends BaseAdapter
{
private Context context;
private List<ReceivedItemStructure> row;
public ReceivedAdapter(Context context, List<ReceivedItemStructure> row)
{
this.context = context;
this.row = row;
}
public void addItem(ReceivedItemStructure item) {
if(row!=null){
row.add(item);
notifyDataSetChanged();
}
}
public void removeItem(ReceivedItemStructure item) {
if(row!=null){
row.remove(item);
notifyDataSetChanged();
}
}
public void removeItemAtPosition(int position) {
if(row!=null){
row.remove(position);
notifyDataSetChanged();
}
}
public void clearAll() {
if(row!=null){
row.clear();
notifyDataSetChanged();
}
}
@Override
public int getCount() {
return row.size();
}
@Override
public ReceivedItemStructure getItem(int position) {
return row.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder holder;
if(convertView ==null){
holder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(R.layout.received_sms_list_fragment, null);
holder.tv_smsBody = (TextView)convertView.findViewById(R.id.tv_smsBody);
holder.tv_smsSender = (TextView)convertView.findViewById(R.id.tv_smsSender);
holder.tv_smsDate = (TextView)convertView.findViewById(R.id.tv_smsDate);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
holder.tv_smsBody.setText(getItem(position).getmSmsBody());
holder.tv_smsSender.setText(getItem(position).getmSmsBody());
holder.tv_smsDate.setText(getItem(position).getmDate());
return convertView;
}
class ViewHolder{
TextView tv_smsBody;
TextView tv_smsSender;
TextView tv_smsDate;
}
}
来源:https://stackoverflow.com/questions/26313277/android-refresh-adapter-with-baseadapter