在此代码中,在将新项目添加到adapert后,我的列表无法刷新并使用notifyDataSetChanged()
进行更新,例如对于此行,我的适配器可以设置没有任何问题。
adapter = new ReceivedAdapter(context, items);
getRequestFromServer(0, 10);
之后我在列表和适配器中有10个项目。
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);
计数是10
。现在在这个bewlo函数中从服务器获取新的i项后我的列表计数可以更改并更新为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);
计数是11
。我是adapter.setRow()
的更新适配器,ReceivedAdapter
我的List
尺寸是11
。使用addDataToList()
函数后我的列表是11
但adapter.notifyDataSetChanged();
不起作用,我在适配器和列表视图中看不到新项。使用通过List
的新项目添加和更新items
的所有方式都可以,并且ReceivedAdapter并且我没有问题。
我的适配器是:
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;
}
}
答案 0 :(得分:1)
不是每次都创建一个新的items
并将其设置为适配器..尝试直接从适配器添加和删除。
首先使用您需要的初始值或空list
初始化适配器。
adapter.add(obj)
adapter.remove(obj)
adapter.clear()
最后,只要您需要反映更改,请致电adapter.notifyDatasetChanged()
答案 1 :(得分:0)
请尝试这种方式,希望这有助于您解决问题。
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;
}
}