即使在调用通知数据集更改后,ListView 也不会刷新其视图
ListView not refreshing its views even after calling notify data set changes
很好 day.I 搜索整个 internet.Tried 每个代码这个或那个 way.But 对我一点帮助都没有!我有简单的列表视图和简单的自定义适配器和简单的自定义 view.Im 在 for() loop.Two 不同的地方添加一些信息,我在循环中调用该死的 adapter.notifyDataSetChanged() 并且当循环是 over.But 时,该死的列表视图不会更新其 view.I 调用无效,使视图无效,我调用了 10 次通知,但愚蠢的列表视图仍然不会更新其视图。
把整个 class 的代码放在那儿。
首先是适配器
public class FindPaqNearMeAdapter extends BaseAdapter {
static int myint = 0;
Context context;
public static int mypositon = 0;
View myview = null;
protected List<FindPaqNearMeSimpleClass> followsSimpleClasses;
LayoutInflater inflater;
public OnGetPositionForView getPositionForView;
public FindPaqNearMeAdapter(Context context, List<FindPaqNearMeSimpleClass> listCars) {
this.followsSimpleClasses = listCars;
this.inflater = LayoutInflater.from(context);
this.context = context;
}
public int getCount() {
return followsSimpleClasses.size();
}
public FindPaqNearMeSimpleClass getItem(int position) {
return followsSimpleClasses.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
holder = new ViewHolder();
if (convertView == null) {
convertView = this.inflater.inflate(R.layout.findpaqnearmesingleitem,
parent, false);
holder.userimage = (de.hdodenhof.circleimageview.CircleImageView)convertView.findViewById(R.id.userimage);
holder.name = (TextView)convertView.findViewById(R.id.name);
holder.startdate = (TextView)convertView.findViewById(R.id.startdate);
holder.enddate = (TextView)convertView.findViewById(R.id.enddate);
holder.price = (TextView)convertView.findViewById(R.id.price);
holder.to = (TextView)convertView.findViewById(R.id.to);
holder.type = (TextView)convertView.findViewById(R.id.paqtype);
holder.from = (TextView)convertView.findViewById(R.id.from);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
FindPaqNearMeSimpleClass feedback = followsSimpleClasses.get(position);
holder.name.setText(feedback.getName());
holder.startdate.setText(feedback.getStartdatre());
holder.enddate.setText(feedback.getEnddate());
holder.price.setText(feedback.getPrice());
holder.to.setText(feedback.getTo());
holder.type.setText(feedback.getType());
holder.from.setText(feedback.getFrom());
holder.userimage.setImageBitmap(feedback.getFollowersiamge());
}
return convertView;
}
private class ViewHolder {
TextView name;
TextView startdate;
TextView enddate;
TextView price;
TextView from;
TextView to;
TextView type;
de.hdodenhof.circleimageview.CircleImageView userimage;
}
}
第二个是这个适配器的持有人;
/**
* Created by vladimir on 07.08.15.
*/
public class FindPaqNearMeSimpleClass {
private Bitmap userimage;
private String name;
private String type;
private String startdatre;
private String from;
private String to;
private String enddate;
private String price;
private String longitude;
private String latitude;
private String paqid;
private String userid;
public String getUserid() {
return userid;
}
public void setUserid(String userid) {
this.userid = userid;
}
public String getPaqid() {
return paqid;
}
public void setPaqid(String paqid) {
this.paqid = paqid;
}
public String getLongitude() {
return longitude;
}
public void setLongitude(String longitude) {
this.longitude = longitude;
}
public String getLatitude() {
return latitude;
}
public void setLatitude(String latitude) {
this.latitude = latitude;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getStartdatre() {
return startdatre;
}
public void setStartdatre(String startdatre) {
this.startdatre = startdatre;
}
public String getFrom() {
return from;
}
public void setFrom(String from) {
this.from = from;
}
public String getTo() {
return to;
}
public void setTo(String to) {
this.to = to;
}
public String getEnddate() {
return enddate;
}
public void setEnddate(String enddate) {
this.enddate = enddate;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public FindPaqNearMeSimpleClass(Bitmap userimage, String name, String type, String startdatre, String from, String to, String enddate, String price) {
this.userimage = userimage;
this.name = name;
this.paqid = paqid;
this.type = type;
this.startdatre = startdatre;
this.from = from;
this.to = to;
this.userid=userid;
this.enddate = enddate;
this.price = price;
this.longitude=longitude;
this.latitude=latitude;
}
public void setFollowersiamge(Bitmap followersiamge) {
this.userimage = followersiamge;
}
public Bitmap getFollowersiamge () {
return userimage;
}
}
最后是我的主程序 class,其中有异步任务,我在其中调用了 onPostExecute() 方法 addpaqs(result)
,代码在这里完成了所有这些事情;
public class RequestPaqs extends AsyncTask<Void, Integer, String> {
@Override
protected String doInBackground(Void... params) {
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(ServerUrlConstants.getAllPaqs);
MultipartEntity reqEntity = new MultipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE);
try {
reqEntity.addPart("type", new StringBody("2"));
reqEntity.addPart("lat",new StringBody(readLatitude()));
reqEntity.addPart("lng", new StringBody(readLongitude()));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
post.setEntity(reqEntity);
// post.setEntity(new UrlEncodedFormEntity(pairs));
} finally {
}
// Execute HTTP Post Request
HttpResponse response = null;
try {
response = client.execute(post);
} catch (IOException e1) {
e1.printStackTrace();
}
try {
String responseBody = EntityUtils.toString(response.getEntity());
paqsresponse = responseBody;
Log.d("anasun", responseBody);
} catch (IOException e1) {
e1.printStackTrace();
}
return paqsresponse;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onPostExecute(String result) {
if(result.length()<7){
File file = new File(Environment.getExternalStorageDirectory(),"latitude.txt");
File file2 = new File(Environment.getExternalStorageDirectory(),"longitue.txt");
DeleteRecursive(file);
DeleteRecursive(file2);
return;
}else{
File file = new File(Environment.getExternalStorageDirectory(),"latitude.txt");
File file2 = new File(Environment.getExternalStorageDirectory(),"longitue.txt");
DeleteRecursive(file);
DeleteRecursive(file2);
try {
addpaqs(result);
} catch (JSONException e) {
e.printStackTrace();
}
}
super.onPostExecute(result);
}
}
这是我的 addpaqs 方法
private void addpaqs(String result) throws JSONException {
JSONArray array = new JSONArray(result);
for(int i =0;i<array.length();i++){
JSONObject object = array.getJSONObject(i);
creator_first_name = object.optString("creator_first_name");
creator_last_name = object.optString("creator_last_name");
paqtype = object.optString("paq_type");
from_country = object.optString("from_country");
from_city = object.optString("from_city");
from_street = object.optString("from_street");
from_state = object.optString("from_state");
to_city = object.optString("to_city");
to_country = object.optString("to_country");
to_street = object.optString("to_street");
to_state = object.optString("to_state");
startdate = object.optString("start_date");
enddate = object.optString("end_date");
price = object.optString("price");
creator_first_name = object.optString("creator_first_name");
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.com_facebook_profile_picture_blank_square);
findPaqNearMeSimpleClass = new FindPaqNearMeSimpleClass(bitmap,creator_first_name+" "+creator_last_name,paqtype,startdate,from_country+"/"+from_city+"/"+from_state+"/"+from_street,to_country+"/"+to_city+"/"+to_state+"/"+to_street,enddate,price);
findPaqNearMeSimpleClassArrayList.add(findPaqNearMeSimpleClass);
listview.invalidate();
listview.invalidateViews();
findPaqNearMeAdapter.notifyDataSetInvalidated();
findPaqNearMeAdapter.notifyDataSetChanged();
Log.d("safhkjasfas","logger");
}
loadingbar.setVisibility(View.GONE);
findPaqNearMeAdapter.notifyDataSetChanged();
}
Antyhing 我做错了吗????
编辑:这是我如何初始化所有这些 classes;
的代码
listview = (ListView) findViewById(R.id.listview);
findPaqNearMeSimpleClassArrayList = new ArrayList<>();
findPaqNearMeAdapter = new FindPaqNearMeAdapter(this,findPaqNearMeSimpleClassArrayList);
listview.setAdapter(findPaqNearMeAdapter);
更改适配器中的 getView() 方法
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
holder = new ViewHolder();
if (convertView == null) {
convertView = this.inflater.inflate(R.layout.findpaqnearmesingleitem,
parent, false);
holder.userimage = (de.hdodenhof.circleimageview.CircleImageView)convertView.findViewById(R.id.userimage);
holder.name = (TextView)convertView.findViewById(R.id.name);
holder.startdate = (TextView)convertView.findViewById(R.id.startdate);
holder.enddate = (TextView)convertView.findViewById(R.id.enddate);
holder.price = (TextView)convertView.findViewById(R.id.price);
holder.to = (TextView)convertView.findViewById(R.id.to);
holder.type = (TextView)convertView.findViewById(R.id.paqtype);
holder.from = (TextView)convertView.findViewById(R.id.from);
convertView.setTag(holder);
}
holder = (ViewHolder) convertView.getTag();
FindPaqNearMeSimpleClass feedback = followsSimpleClasses.get(position);
holder.name.setText(feedback.getName());
holder.startdate.setText(feedback.getStartdatre());
holder.enddate.setText(feedback.getEnddate());
holder.price.setText(feedback.getPrice());
holder.to.setText(feedback.getTo());
holder.type.setText(feedback.getType());
holder.from.setText(feedback.getFrom());
holder.userimage.setImageBitmap(feedback.getFollowersiamge());
return convertView;
}
很好 day.I 搜索整个 internet.Tried 每个代码这个或那个 way.But 对我一点帮助都没有!我有简单的列表视图和简单的自定义适配器和简单的自定义 view.Im 在 for() loop.Two 不同的地方添加一些信息,我在循环中调用该死的 adapter.notifyDataSetChanged() 并且当循环是 over.But 时,该死的列表视图不会更新其 view.I 调用无效,使视图无效,我调用了 10 次通知,但愚蠢的列表视图仍然不会更新其视图。 把整个 class 的代码放在那儿。 首先是适配器
public class FindPaqNearMeAdapter extends BaseAdapter {
static int myint = 0;
Context context;
public static int mypositon = 0;
View myview = null;
protected List<FindPaqNearMeSimpleClass> followsSimpleClasses;
LayoutInflater inflater;
public OnGetPositionForView getPositionForView;
public FindPaqNearMeAdapter(Context context, List<FindPaqNearMeSimpleClass> listCars) {
this.followsSimpleClasses = listCars;
this.inflater = LayoutInflater.from(context);
this.context = context;
}
public int getCount() {
return followsSimpleClasses.size();
}
public FindPaqNearMeSimpleClass getItem(int position) {
return followsSimpleClasses.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
holder = new ViewHolder();
if (convertView == null) {
convertView = this.inflater.inflate(R.layout.findpaqnearmesingleitem,
parent, false);
holder.userimage = (de.hdodenhof.circleimageview.CircleImageView)convertView.findViewById(R.id.userimage);
holder.name = (TextView)convertView.findViewById(R.id.name);
holder.startdate = (TextView)convertView.findViewById(R.id.startdate);
holder.enddate = (TextView)convertView.findViewById(R.id.enddate);
holder.price = (TextView)convertView.findViewById(R.id.price);
holder.to = (TextView)convertView.findViewById(R.id.to);
holder.type = (TextView)convertView.findViewById(R.id.paqtype);
holder.from = (TextView)convertView.findViewById(R.id.from);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
FindPaqNearMeSimpleClass feedback = followsSimpleClasses.get(position);
holder.name.setText(feedback.getName());
holder.startdate.setText(feedback.getStartdatre());
holder.enddate.setText(feedback.getEnddate());
holder.price.setText(feedback.getPrice());
holder.to.setText(feedback.getTo());
holder.type.setText(feedback.getType());
holder.from.setText(feedback.getFrom());
holder.userimage.setImageBitmap(feedback.getFollowersiamge());
}
return convertView;
}
private class ViewHolder {
TextView name;
TextView startdate;
TextView enddate;
TextView price;
TextView from;
TextView to;
TextView type;
de.hdodenhof.circleimageview.CircleImageView userimage;
}
}
第二个是这个适配器的持有人;
/**
* Created by vladimir on 07.08.15.
*/
public class FindPaqNearMeSimpleClass {
private Bitmap userimage;
private String name;
private String type;
private String startdatre;
private String from;
private String to;
private String enddate;
private String price;
private String longitude;
private String latitude;
private String paqid;
private String userid;
public String getUserid() {
return userid;
}
public void setUserid(String userid) {
this.userid = userid;
}
public String getPaqid() {
return paqid;
}
public void setPaqid(String paqid) {
this.paqid = paqid;
}
public String getLongitude() {
return longitude;
}
public void setLongitude(String longitude) {
this.longitude = longitude;
}
public String getLatitude() {
return latitude;
}
public void setLatitude(String latitude) {
this.latitude = latitude;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getStartdatre() {
return startdatre;
}
public void setStartdatre(String startdatre) {
this.startdatre = startdatre;
}
public String getFrom() {
return from;
}
public void setFrom(String from) {
this.from = from;
}
public String getTo() {
return to;
}
public void setTo(String to) {
this.to = to;
}
public String getEnddate() {
return enddate;
}
public void setEnddate(String enddate) {
this.enddate = enddate;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public FindPaqNearMeSimpleClass(Bitmap userimage, String name, String type, String startdatre, String from, String to, String enddate, String price) {
this.userimage = userimage;
this.name = name;
this.paqid = paqid;
this.type = type;
this.startdatre = startdatre;
this.from = from;
this.to = to;
this.userid=userid;
this.enddate = enddate;
this.price = price;
this.longitude=longitude;
this.latitude=latitude;
}
public void setFollowersiamge(Bitmap followersiamge) {
this.userimage = followersiamge;
}
public Bitmap getFollowersiamge () {
return userimage;
}
}
最后是我的主程序 class,其中有异步任务,我在其中调用了 onPostExecute() 方法 addpaqs(result)
,代码在这里完成了所有这些事情;
public class RequestPaqs extends AsyncTask<Void, Integer, String> {
@Override
protected String doInBackground(Void... params) {
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(ServerUrlConstants.getAllPaqs);
MultipartEntity reqEntity = new MultipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE);
try {
reqEntity.addPart("type", new StringBody("2"));
reqEntity.addPart("lat",new StringBody(readLatitude()));
reqEntity.addPart("lng", new StringBody(readLongitude()));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
post.setEntity(reqEntity);
// post.setEntity(new UrlEncodedFormEntity(pairs));
} finally {
}
// Execute HTTP Post Request
HttpResponse response = null;
try {
response = client.execute(post);
} catch (IOException e1) {
e1.printStackTrace();
}
try {
String responseBody = EntityUtils.toString(response.getEntity());
paqsresponse = responseBody;
Log.d("anasun", responseBody);
} catch (IOException e1) {
e1.printStackTrace();
}
return paqsresponse;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onPostExecute(String result) {
if(result.length()<7){
File file = new File(Environment.getExternalStorageDirectory(),"latitude.txt");
File file2 = new File(Environment.getExternalStorageDirectory(),"longitue.txt");
DeleteRecursive(file);
DeleteRecursive(file2);
return;
}else{
File file = new File(Environment.getExternalStorageDirectory(),"latitude.txt");
File file2 = new File(Environment.getExternalStorageDirectory(),"longitue.txt");
DeleteRecursive(file);
DeleteRecursive(file2);
try {
addpaqs(result);
} catch (JSONException e) {
e.printStackTrace();
}
}
super.onPostExecute(result);
}
}
这是我的 addpaqs 方法
private void addpaqs(String result) throws JSONException {
JSONArray array = new JSONArray(result);
for(int i =0;i<array.length();i++){
JSONObject object = array.getJSONObject(i);
creator_first_name = object.optString("creator_first_name");
creator_last_name = object.optString("creator_last_name");
paqtype = object.optString("paq_type");
from_country = object.optString("from_country");
from_city = object.optString("from_city");
from_street = object.optString("from_street");
from_state = object.optString("from_state");
to_city = object.optString("to_city");
to_country = object.optString("to_country");
to_street = object.optString("to_street");
to_state = object.optString("to_state");
startdate = object.optString("start_date");
enddate = object.optString("end_date");
price = object.optString("price");
creator_first_name = object.optString("creator_first_name");
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.com_facebook_profile_picture_blank_square);
findPaqNearMeSimpleClass = new FindPaqNearMeSimpleClass(bitmap,creator_first_name+" "+creator_last_name,paqtype,startdate,from_country+"/"+from_city+"/"+from_state+"/"+from_street,to_country+"/"+to_city+"/"+to_state+"/"+to_street,enddate,price);
findPaqNearMeSimpleClassArrayList.add(findPaqNearMeSimpleClass);
listview.invalidate();
listview.invalidateViews();
findPaqNearMeAdapter.notifyDataSetInvalidated();
findPaqNearMeAdapter.notifyDataSetChanged();
Log.d("safhkjasfas","logger");
}
loadingbar.setVisibility(View.GONE);
findPaqNearMeAdapter.notifyDataSetChanged();
}
Antyhing 我做错了吗???? 编辑:这是我如何初始化所有这些 classes;
的代码listview = (ListView) findViewById(R.id.listview);
findPaqNearMeSimpleClassArrayList = new ArrayList<>();
findPaqNearMeAdapter = new FindPaqNearMeAdapter(this,findPaqNearMeSimpleClassArrayList);
listview.setAdapter(findPaqNearMeAdapter);
更改适配器中的 getView() 方法
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
holder = new ViewHolder();
if (convertView == null) {
convertView = this.inflater.inflate(R.layout.findpaqnearmesingleitem,
parent, false);
holder.userimage = (de.hdodenhof.circleimageview.CircleImageView)convertView.findViewById(R.id.userimage);
holder.name = (TextView)convertView.findViewById(R.id.name);
holder.startdate = (TextView)convertView.findViewById(R.id.startdate);
holder.enddate = (TextView)convertView.findViewById(R.id.enddate);
holder.price = (TextView)convertView.findViewById(R.id.price);
holder.to = (TextView)convertView.findViewById(R.id.to);
holder.type = (TextView)convertView.findViewById(R.id.paqtype);
holder.from = (TextView)convertView.findViewById(R.id.from);
convertView.setTag(holder);
}
holder = (ViewHolder) convertView.getTag();
FindPaqNearMeSimpleClass feedback = followsSimpleClasses.get(position);
holder.name.setText(feedback.getName());
holder.startdate.setText(feedback.getStartdatre());
holder.enddate.setText(feedback.getEnddate());
holder.price.setText(feedback.getPrice());
holder.to.setText(feedback.getTo());
holder.type.setText(feedback.getType());
holder.from.setText(feedback.getFrom());
holder.userimage.setImageBitmap(feedback.getFollowersiamge());
return convertView;
}