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

时间:2015-08-07 07:40:54

标签: android listview view refresh

美好的一天。我搜索整个互联网。这个或那个方式干掉了每一点代码。但是没有任何帮助我!我有简单的自定义适配器和简单的自定义视图的简单列表视图。我将一些信息添加到in for()loop.Two不同的地方我在循环中调用该死的adapter.notifyDataSetChanged()并且当循环结束时。但是那个怪异的列表视图将不会更新它的视图。我调用invalidate,使视图无效,我调用10次通知但仍然那个愚蠢的列表视图不会更新其观点。 把整个类的代码放在哪里。 首先是适配器

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

}

最后是我的主类,其中有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();

    }

Antyhing我做错了???? 编辑:这是我如何初始化所有这些类的代码;

listview = (ListView) findViewById(R.id.listview);
        findPaqNearMeSimpleClassArrayList = new ArrayList<>();
        findPaqNearMeAdapter = new FindPaqNearMeAdapter(this,findPaqNearMeSimpleClassArrayList);
        listview.setAdapter(findPaqNearMeAdapter);

1 个答案:

答案 0 :(得分:1)

更改适配器

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