Android的刷新适配器,带BaseAdapter [英] Android refresh Adapter with BaseAdapter
问题描述
在insterting新项目adapert我的名单未能刷新和更新 notifyDataSetChanged()
例如,对于此行我的适配器可以设置没有任何在此之后code问题。
适配器=新ReceivedAdapter(背景下,项目);
getRequestFromServer(0,10);
之后,我有10个项目在列表中的适配器。
私人字符串getRequestFromServer(长lastID,诠释计数){
串接收=;
尝试{
收到=新JsonService(config_username,config_password,lastID,计数,G.F_RECEIVE_SMS).request();
JSONArray data_array中=新JSONArray(收); 字符串mUserID = config_username;
的for(int i = 0; I< data_array.length();我++){
JSONObject的json_obj = data_array.getJSONObject(I) 串mLastID = json_obj.getString(id_recived_sms);
串mSmsBody = json_obj.getString(SMS_BODY);
串mSmsNumber = json_obj.getString(sms_number);
串mSenderName = json_obj.getString(MOBILE_NUMBER);
字符串mContactName = json_obj.getString(CONTACT_NAME);
串mDate = json_obj.getString(recived_date); ReceivedItemStructure项目=新ReceivedItemStructure(
mLastID,
mUserID,
mSmsBody,
mSmsNumber,
mSenderName,
mContactName,
mDate
);
items.add(项目);
}
setListAdapter(适配器);
}赶上(例外五){
e.printStackTrace();
}
收到回报;
}
items.add(项目);
计数是 10
。现在在这个bewlo功能得到之后,从服务器我的名单数量可以改变和更新新的I项目11
私人无效addDataToList(字符串LastID,字符串SmsBody,字符串SmsNumber,字符串发送者姓名,联系人姓名字符串,字符串日期){
字符串mLastID = LastID;
字符串mUserID = config_username;
字符串mSmsBody = SmsBody;
字符串mSmsNumber = SmsNumber;
字符串mSenderName =发送者姓名;
字符串mContactName =联系人姓名;
字符串mDate =日期;
ReceivedItemStructure项目=新ReceivedItemStructure(
mLastID,
mUserID,
mSmsBody,
mSmsNumber,
mSenderName,
mContactName,
mDate
);
items.add(项目);
adapter.setRow(项目);
adapter.notifyDataSetChanged();
}
items.add(项目);
计数是 11
.i'm与<$ C更新适配器$ C> adapter.setRow()进入 ReceivedAdapter
我的列表
尺寸 11
。我的名单后,使用 addDataToList()
功能 11
,但 adapter.notifyDataSetChanged();
不工作,我没有看到适配器和列表视图新项目。添加和更新各种方式列表
与通项目
新项目确定,ReceivedAdapter和我没有问题。
我的适配器是:
公共类ReceivedAdapter延伸BaseAdapter
{
私人LayoutInflater吹气;
私人列表&LT; ReceivedItemStructure&GT;行;
公共ReceivedAdapter(上下文的背景下,列表与LT; ReceivedItemStructure&GT;排)
{
this.row =排;
充气= LayoutInflater.from(上下文);
}
公共无效setRow(列表&LT; ReceivedItemStructure&GT;排){
this.row =排;
}
@覆盖
公众诠释的getCount(){
返回row.size();
}
@覆盖
公共ReceivedItemStructure的getItem(INT位置){
返回row.get(位置);
}
@覆盖
众长getItemId(INT位置){
返回0;
} @覆盖
公共查看getView(INT位置,查看convertView,父母的ViewGroup)
{
如果(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(位置).getmSmsBody()); TextView的tv_smsSender =(TextView中)convertView.findViewById(R.id.tv_smsSender);
tv_smsSender.setText(的getItem(位置).getmSmsBody()); TextView的tv_smsDate =(TextView中)convertView.findViewById(R.id.tv_smsDate);
tv_smsDate.setText(的getItem(位置).getmDate());
返回convertView;
}
}
项目
每次并将其设置的到适配器..尝试添加和删除从适配器直接。
首先初始化需要初始值或空列表
。
- 接下来,当你想添加...呼叫
adapter.add(OBJ)
- 当要删除通话
adapter.remove(OBJ)
- 当你想清空适配器调用
adapter.clear()
最后调用 adapter.notifyDatasetChanged()
当您需要将反映变化
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;
}
}
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
这篇关于Android的刷新适配器,带BaseAdapter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!