列表视图在第一次滚动时两次加载相同的数据,但效果很好

时间:2019-03-08 07:34:03

标签: java android

查看投诉活动:

在此我已经完成调试,但是我不知道滚动后两次出现数据的原因。我什至通过邮递员检查了来自服务器的数据,但是这些数据并没有完全重复,因为当我们通过适配器将数据放入活动中进行滚动时,问题似乎在前端。

public class ViewComplaintActivity extends AppCompatActivity implements AbsListView.OnScrollListener, View.OnClickListener {

   TextView tv_notData;
   List<Ticket> _data = new ArrayList<Ticket>();
   ComplaintsAdapter settlementAdapter;
   private String TAG = "ViewComplaintActivity";
   private int visibleThreshold = 5;
   private int currentPage = 0;
   private int previousTotal = 0;
   private boolean loading = true;
   private boolean userScrolled = false;

a)这是一个投诉活动,我们在其中显示通过适配器从服务器发送的数据通信。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_view_compliants);
    ButterKnife.bind(this);
    try {
        settlementAdapter = new ComplaintsAdapter(ViewComplaintActivity.this, _data);
        AlphaInAnimationAdapter animationAdapter = new AlphaInAnimationAdapter(settlementAdapter);
        animationAdapter.setAbsListView(ll_complaint_list);
        ll_complaint_list.setAdapter(animationAdapter);

        getComplaints(Utility.getTerminalId(ViewComplaintActivity.this), 0);

        ll_complaint_list.setOnScrollListener(this);
        fab_raise_complaint.setOnClickListener(this);
        fab_call_customer.setOnClickListener(this);
        ivBackButton.setOnClickListener(this);
    } catch (NullPointerException e) {
        Log.e(TAG, e.toString());
    } catch (Exception e) {
        Log.e(TAG, e.toString());
    }
}

private void getComplaints(String terminalid, int pagenumber) {
    if (!NetworkConnection.isConnected(this)) {
        Toast.makeText(this, R.string.no_connection, Toast.LENGTH_SHORT).show();
        return;
    }
    progressBar.setVisibility(View.VISIBLE);
    JsonObject jsonObject = new JsonObject();
    jsonObject.addProperty("subIdentifier", terminalid);
    jsonObject.addProperty("pageLimit", "10");
    jsonObject.addProperty("lastPageIndex", "" + pagenumber);

    IRetrofit iRetrofit = MoApplication.getInstance().getRetrofitOsTickets(this).create(IRetrofit.class);

    Log.e(TAG,"jsonObject "+jsonObject);

    iRetrofit.getAllTicket(jsonObject, new Callback<Response>() {
        @Override
        public void success(Response response, Response response2) {
            progressBar.setVisibility(View.GONE);
            try {
                if (response.getStatus() == 200) {
                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(response.getBody().in()));
                    String resultFromJson = bufferedReader.readLine();
                   /* Log.e(TAG, "resultFromJson " + resultFromJson);*/

                    GetTicketsResponse getSettlementsResDTO = new Gson().fromJson(resultFromJson, GetTicketsResponse.class);

                    switch (getSettlementsResDTO.getResCode()) {
                        case "0001":
                            if (null != getSettlementsResDTO.getTickets()) {

                              _data.addAll(getSettlementsResDTO.getTickets());

                                settlementAdapter.notifyDataSetChanged();
                                if (_data.isEmpty()) {
                                    tv_notData.setVisibility(View.VISIBLE);
                                } else {
                                    tv_notData.setVisibility(View.GONE);
                                }

                            } else {
                                Utility.showToast(ViewComplaintActivity.this, "No data found.");
                            }
                            break;
                        default:
                            Utility.showToast(ViewComplaintActivity.this, "Please try later.");
                            break;
                    }
                }
            } catch (NullPointerException e) {
                Log.e(TAG, "" + e.toString());
            } catch (Exception e) {
                Log.e(TAG, "" + e.toString());
            }
        }

        @Override
        public void failure(RetrofitError error) {
            if (Utility.parseException(error)){
                Utility.mPosLogoutService((AppCompatActivity) ViewComplaintActivity.this);
            }
            progressBar.setVisibility(View.GONE);
        }
    });
}

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        /*Log.e(TAG,"one");*/
        Log.e(TAG,"scrollState "+scrollState);
        if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
            userScrolled = true;
            fab_call_customer.hide();
            fab_raise_complaint.hide();
        } else {
            fab_call_customer.show();
            fab_raise_complaint.show();
        }
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        /*Log.e(TAG,"two");*/


        view.getSelectedItemPosition();

        if (userScrolled) {
            Log.e(TAG,"loading "+loading);

            if (loading) {
                Log.e(TAG,"totalItemCount "+totalItemCount);
                Log.e(TAG,"previousTotal "+previousTotal);
                if (totalItemCount > previousTotal) {
                    loading = false;
                    previousTotal = totalItemCount;
                    currentPage++;
                }
            }

            Log.e(TAG,"loadin after scroll "+loading);

            if (!loading && ((totalItemCount - visibleItemCount) <= (firstVisibleItem + visibleThreshold))) {
                getComplaints(Utility.getTerminalId(ViewComplaintActivity.this), currentPage);
                loading = true;
            }
        }
        else {
            Log.e(TAG,"user is idle");
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
    }

    @Override
    public void onClick(View v) {
        if (NetworkConnection.isConnected(this)) {
            if (v == fab_raise_complaint) {
                Utility.navigate(this, RaiseComplaintActivity.class);
            } else if (v == fab_call_customer) {
                Intent callIntent = new Intent(Intent.ACTION_DIAL);
                callIntent.setData(Uri.parse("tel:" + Uri.encode(Constant.helplineNumber.trim())));
                callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(callIntent);
            } else if (v == ivBackButton) {
                startActivity(new Intent(ViewComplaintActivity.this, HomeQrActivity.class));
                finish();
            }
        } else {
            Toast.makeText(this, R.string.no_connection, Toast.LENGTH_SHORT).show();
        }
    }

    public class ComplaintsAdapter extends BaseAdapter {
    private static String TAG = "ComplaintsAdapter";
    List<Ticket> _data;
    Context _c;
    ViewHolder holder;

    public ComplaintsAdapter(Context context, List<Ticket> getData) {
        _data = getData;
        _c = context;
    }
    @Override
    public int getCount() {
        Log.e(TAG,"_data.size()"+_data.size());
        return _data.size();

    }

    @Override
    public Object getItem(int position) {
        Log.e(TAG,"_data.get(position)"+_data.get(position));
        return _data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        /*Log.e(TAG,"getData"+_data.toString());*/
        try {
            if (view == null) {
                /*Log.e(TAG,"getData1"+_data.toString());*/
                LayoutInflater li = (LayoutInflater) 
                _c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                view = li.inflate(R.layout.row_item_view_complaints, 
                null);
            } else {
                /*Log.e(TAG,"getData2"+_data.toString());*/
                view = convertView;
            }

            holder = new ViewHolder();
            holder.tv_ticketNo = (TextView) 
            view.findViewById(R.id.tv_ticketNo);
            holder.tv_submitDate = (TextView) 
            view.findViewById(R.id.tv_submitDate); //ticket comments
            holder.tv_updatedDate = (TextView) 
            view.findViewById(R.id.tv_updatedDate);//will act as ticket 
            last modified time
            holder.tv_ticketStatus = (TextView) 
            view.findViewById(R.id.tv_ticketStatus); //ticket status open 
            or close
            holder.tv_comment = (TextView) 
            view.findViewById(R.id.tv_comment);
            holder.tv_subject = (TextView) 
           view.findViewById(R.id.tv_subject);

            final Ticket ticket = _data.get(position);
            holder.tv_ticketNo.setText("#" + ticket.getTicketNo());
            holder.tv_submitDate.setText(Html.fromHtml("<b> Created On: 
            </b>" + Utility.dateReformatting("yyyy-MM-dd HH:mm:ss", "dd 
            MMM yyyy", ticket.getCreated())));
            holder.tv_updatedDate.setText(Html.fromHtml("<b> Modified On: 
            </b>" + Utility.dateReformatting("yyyy-MM-dd HH:mm:ss", "dd 
            MMM yyyy", ticket.getModified())));
            holder.tv_ticketStatus.setText(ticket.getStatus());
            holder.tv_subject.setText(null != ticket.getSubject() && !ticket.getSubject().isEmpty() ? ticket.getSubject().trim().replace("|", "") : "NA"); //comments
            if (null != ticket.getComment() && !ticket.getComment().isEmpty()) {

            holder.tv_comment.setText(Html.fromHtml(ticket.getComment()));
                holder.tv_comment.setVisibility(View.VISIBLE);
            } else {
                holder.tv_comment.setVisibility(View.GONE);
            }
                //holder.tv_comment.setText(null != ticket.getComment() && !ticket.getComment().isEmpty() ? ticket.getComment() : ""); //comments
            if (ticket.getStatus().startsWith("Open"))
              holder.tv_ticketStatus.setTextColor(_c.getResources().getColor(R.color.green_success));
            else
                Log.e(TAG,"getData4"+_data.toString());
             holder.tv_ticketStatus.setTextColor(_c.getResources().getColor(R.color_color));
        } catch (NullPointerException e) {
            Log.e(TAG, e.toString());
        } catch (Exception e) {
            Log.e(TAG, e.toString());
        }
        return view;
    }

    public void set_data(List<Ticket> _data) {
        this._data = _data;
    }

    static class ViewHolder {
        public TextView tv_ticketStatus, tv_ticketNo, tv_submitDate, 
        tv_updatedDate, tv_comment, tv_subject;
    }
}

0 个答案:

没有答案