即使在调用notify数据集更改后,ListView也不刷新其视图



大家好。我搜索了整个互联网。以这种或那种方式尝试了所有代码。但是什么也帮不了我!我有简单的列表视图与简单的自定义适配器与简单的自定义视图。我在infor()循环中添加了一些信息。两个不同的地方,我调用该死的适配器。notifydatasetchanged()在循环和循环结束时。但那个该死的列表视图不会更新它的视图。我调用了invalidate,invalidate views,调用了10次notify但那个愚蠢的列表视图仍然不会更新它的视图。把整个类的代码都放进去。首先是适配器

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;
    }

}

second是这个适配器的持有者;

/**
 * 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;
    }
}

最后是我的主类,里面有asynctask,在上面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();
    }

我做错了什么????编辑:这是我如何init所有这些类的代码;

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;
}

最新更新