在活动简历

时间:2016-08-08 12:00:58

标签: android listview android-recyclerview recycler-adapter

//我的RecyclerAdpater代码在这里:

public static final class ViewHolder extends RecyclerView.ViewHolder {
    ImageView ivProperty;
    ImageView ivEditProperty;
    ImageView ivDeleteProperty;
    ImageView ivPropTransHistory;
    ImageView ivInspectReport;
    TextView tvMaintenReqCount;
    TextView tvPropName;
    TextView tvPropAddr;
    TextView tvPropStatus;
    View view;

    public ViewHolder(View v) {
        super(v);
        //Initialize the variables
        ivProperty = (ImageView) v.findViewById(R.id.ivProperty);
        tvPropName = (TextView) v.findViewById(R.id.tvPropName);
        tvPropAddr = (TextView) v.findViewById(R.id.tvPropAddr);
        tvPropStatus = (TextView) v.findViewById(R.id.tvPropStatus);
        tvMaintenReqCount = (TextView) v.findViewById(R.id.tvMaintenReqCount);
        ivEditProperty = (ImageView) v.findViewById(R.id.ivEditProperty);
        ivPropTransHistory = (ImageView) v.findViewById(R.id.ivPropTransHistory);
        ivInspectReport = (ImageView) v.findViewById(R.id.ivInspectReport);
        ivDeleteProperty = (ImageView) v.findViewById(R.id.ivDeleteProperty);
        view = v;
    }
}

// Recycler adpter 

private final class PropertyRecyclerAdapter extends RecyclerView.Adapter<PropertyListActivity.ViewHolder> {

    final Context _context;
    List<PropertyListDTO> objects;

    public PropertyRecyclerAdapter(Context context, List<PropertyListDTO> objects) {
        this._context = context;
        this.objects = objects;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // create a new view
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_property_list, parent, false);
        // set the view's size, margins, paddings and layout parameters
        ViewHolder v = new ViewHolder(view);
        return v;
    }

    public void setFilter(List<PropertyListDTO> list) {
        objects = new ArrayList<PropertyListDTO>();
        objects.addAll(list);
        notifyDataSetChanged();
    }

    // bindview holder

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        // - get element from your data set at this position
        // - replace the contents of the view with that element
        String propImgUrl = "";
        final PropertyListDTO p = objects.get(position);
        // Load a bitmap from the drawable folder
        try {
            // set data value

            holder.tvPropName.setText(p.getPropertyName());
            holder.tvPropAddr.setText(p.getAddress());
            if (p.getInspectionReports() != null) {
                if (p.getInspectionReports() != null && p.getPropStatus().equalsIgnoreCase("0")) {
                    holder.tvPropStatus.setText(_context.getResources().getString(R.string.property_status_0));
                    holder.ivInspectReport.setVisibility(View.GONE);
                } else if (p.getInspectionReports() != null && p.getPropStatus().equalsIgnoreCase("1")) {
                    holder.tvPropStatus.setText(_context.getResources().getString(R.string.property_status_1));
                    holder.ivEditProperty.setVisibility(View.VISIBLE);
                    holder.ivInspectReport.setVisibility(View.GONE);
                } else if (p.getInspectionReports() != null && p.getPropStatus().equalsIgnoreCase("2")) {
                    holder.tvPropStatus.setText(_context.getResources().getString(R.string.property_status_2));
                    holder.ivEditProperty.setVisibility(View.GONE);
                    holder.ivInspectReport.setVisibility(View.GONE); //holder.ivInspectReport.setVisibility(View.VISIBLE)
                } else if (p.getInspectionReports() != null && p.getPropStatus().equalsIgnoreCase("3")) {
                    holder.tvPropStatus.setText(_context.getResources().getString(R.string.property_status_3));
                    holder.ivEditProperty.setVisibility(View.GONE);
                    holder.ivInspectReport.setVisibility(View.VISIBLE);
                } else if (p.getInspectionReports() != null && p.getPropStatus().equalsIgnoreCase("4")) {
                    holder.tvPropStatus.setText(_context.getResources().getString(R.string.property_status_4));
                    holder.ivEditProperty.setVisibility(View.VISIBLE);
                    holder.ivInspectReport.setVisibility(View.VISIBLE);
                } else if (p.getInspectionReports() != null && p.getPropStatus().equalsIgnoreCase("5")) {
                    holder.tvPropStatus.setText(_context.getResources().getString(R.string.property_status_5));
                    holder.ivEditProperty.setVisibility(View.GONE);
                    holder.ivInspectReport.setVisibility(View.VISIBLE);
                } else if (p.getInspectionReports() != null && p.getPropStatus().equalsIgnoreCase("6")) {
                    holder.tvPropStatus.setText(_context.getResources().getString(R.string.property_status_6));
                    holder.ivEditProperty.setVisibility(View.GONE);
                    holder.ivInspectReport.setVisibility(View.VISIBLE);
                } else if (p.getInspectionReports() != null && p.getPropStatus().equalsIgnoreCase("7")) {
                    holder.tvPropStatus.setText(_context.getResources().getString(R.string.property_status_7));
                    holder.ivEditProperty.setVisibility(View.GONE);
                    holder.ivInspectReport.setVisibility(View.VISIBLE);
                } else {
                    holder.tvPropStatus.setText(_context.getResources().getString(R.string.property_status_0));
                    holder.ivInspectReport.setVisibility(View.GONE);
                }
            } else {

                if (p.getPropStatus().equalsIgnoreCase("0")) {
                    holder.tvPropStatus.setText(_context.getResources().getString(R.string.property_status_0));
                    holder.ivEditProperty.setVisibility(View.VISIBLE);
                    holder.ivInspectReport.setVisibility(View.GONE);
                } else if (p.getPropStatus().equalsIgnoreCase("1")) {
                    holder.tvPropStatus.setText(_context.getResources().getString(R.string.property_status_1));
                    holder.ivEditProperty.setVisibility(View.VISIBLE);
                    holder.ivInspectReport.setVisibility(View.GONE);
                } else if (p.getPropStatus().equalsIgnoreCase("2")) {
                    holder.tvPropStatus.setText(_context.getResources().getString(R.string.property_status_2));
                    holder.ivEditProperty.setVisibility(View.VISIBLE);
                    holder.ivInspectReport.setVisibility(View.VISIBLE);
                } else if (p.getPropStatus().equalsIgnoreCase("3")) {
                    holder.tvPropStatus.setText(_context.getResources().getString(R.string.property_status_3));
                    holder.ivEditProperty.setVisibility(View.GONE);
                    holder.ivInspectReport.setVisibility(View.VISIBLE);
                } 


            holder.tvMaintenReqCount.setText("(" + p.getMaintenance_request_count() + ")");
            if (p.getGallery() != null && p.getGallery().size() > 0) {
                propImgUrl = AppConstants.property_Images_Path + p.getGallery().get(0).toString();
            }
            if (!TextUtils.isEmpty(propImgUrl)) {
                Picasso.with(context).load(propImgUrl).error(R.mipmap.home_profile).into(holder.ivProperty);
            } else {

            }

            android.util.Log.e("ImageUrl List =>", propImgUrl.toString());


        } catch (Exception e) {
            e.printStackTrace();
        }


        // click listener  View Property Detail                  

        holder.view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(context, ViewPropDetailsFragment.class);
                intent.putExtra("propDetails", p);
                startActivity(intent);
            }
        });


        // Edit Property Detail Info

        holder.ivEditProperty.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(PropertyListActivity.this, EditPropPrimDetailsActivity.class);
                intent.putExtra("propertyListDTO", p);
                startActivity(intent);

            }
        });


        holder.ivPropTransHistory.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                PropertyTransactionFragment propertyTransactionFragment = new PropertyTransactionFragment();
                FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                Bundle bundle = new Bundle();
                bundle.putString("property_id", p.getId().get$id());
                bundle.putString("p_Id", p.getPropertyId());
                bundle.putString("property_name", p.getPropertyName());
                propertyTransactionFragment.setArguments(bundle);
                transaction.replace(R.id.container, propertyTransactionFragment);

                transaction.addToBackStack("propertyTransactionFragment");
                transaction.commit();
            }
        });

    }


    @Override
    public int getItemCount() {
        return objects.size();
    }
}  

//日志错误是:

E / AndroidRuntime:致命异常:主要                                                                          过程:com.excellenceit.propcare,PID:9927                                                                          java.lang.IndexOutOfBoundsException:检测到不一致。无效的项目位置4(偏移量:4).state:9                                                                              在android.support.v7.widget.RecyclerView $ Recycler.getViewForPosition(RecyclerView.java:4957)                                                                              在android.support.v7.widget.RecyclerView $ Recycler.getViewForPosition(RecyclerView.java:4913)                                                                              在android.support.v7.widget.LinearLayoutManager $ LayoutState.next(LinearLayoutManager.java:2029)                                                                              在android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1414)                                                                              在android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1377)                                                                              在android.support.v7.widget.LinearLayoutManager.scrollBy(LinearLayoutManager.java:1193)                                                                              在android.support.v7.widget.LinearLayoutManager.scrollVerticallyBy(LinearLayoutManager.java:1043)                                                                              在android.support.v7.widget.RecyclerView.scrollByInternal(RecyclerView.java:1552)                                                                              在android.support.v7.widget.RecyclerView.onTouchEvent(RecyclerView.java:2649)                                                                              在android.view.View.dispatchTouchEvent(View.java:8968)                                                                              在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2698)                                                                              在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2410)                                                                              在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2709)                                                                              在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2425)                                                                              在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2709)                                                                              在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2425)                                                                              在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2709)                                                                              在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2425)                                                                              在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2709)                                                                              在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2425)                                                                              在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2709)                                                                              在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2425)                                                                              在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2709)                                                                              在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2425)                                                                              在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2709)                                                                              在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2425)                                                                              在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2709)                                                                              在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2425)                                                                              在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2709)                                                                              在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2425)                                                                              在com.android.internal.policy.impl.PhoneWindow $ DecorView.superDispatchTouchEvent(PhoneWindow.java:2568)                                                                              在com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1776)                                                                              在android.app.Activity.dispatchTouchEvent(Activity.java:2866)                                                                              在android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:63)                                                                              在com.android.internal.policy.impl.PhoneWindow $ DecorView.dispatchTouchEvent(PhoneWindow.java:2529)                                                                              在android.view.View.dispatchPointerEvent(View.java:9173)                                                                              在android.view.ViewRootImpl $ ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4706)                                                                              在android.view.ViewRootImpl $ ViewPostImeInputStage.onProcess(ViewRootImpl.java:4544)                                                                              在android.view.ViewRootImpl $ InputStage.deliver(ViewRootImpl.java:4068)                                                                              在android.view.ViewRootImpl $ InputStage.onDeliverToNext(ViewRootImpl.java:4121)                                                                              在android.view.ViewRootImpl $ InputStage.forward(ViewRootImpl.java:4087)                                                                              在android.view.ViewRootImpl $ AsyncInputStage.forward(ViewRootImpl.java:4201)                                                                              在android.view.ViewRootImpl $ InputStage.apply(ViewRootImpl.java:4095)                                                                              在android.view.ViewRootImpl $ AsyncInputStage.apply(ViewRootImpl.java:4258)                                                                              在android.view.ViewRootImpl $ InputStage.deliver(ViewRootImpl.java:4068)                                                                              在android.view.ViewRootImpl $ InputStage.onDeliverToNext(ViewRootImpl.java:4121)                                                                              在android.view.ViewRootImpl $ InputStage.forward(ViewRootImpl.java:4087)                                                                              在android.view.ViewRootImpl $ InputStage.apply(ViewRootImpl.java:4095)                                                                              在android.view.ViewRootImpl $ InputStage.deliver(ViewRootImpl.java:4068)                                                                             在android.view.ViewRootImpl

2 个答案:

答案 0 :(得分:0)

onBindViewHolder中的位置与您的数据列表有关,或者数据数组可能没有该位置的任何数据。

答案 1 :(得分:0)

是的,最后我得到了上述问题的答案,并按照以下步骤操作:

1)使用你的模型类创建临时列表作为arraylist。

2)将templist分配给传递给RecyclerAdapter的arraylist。

private List<PropertyListDTO> propertyList;
List<PropertyListDTO> tempList = new ArrayList<>();

//解析Json数据

JSONArray array = obj.getJSONArray("properties");
 if (array.length() > 0) {
  Gson gson = new Gson();
  int i = 0;
  while (i < array.length()) {
  tempList.add(gson.fromJson(array.getJSONObject(i).toString(), PropertyListDTO.class));
  i++;
  }

  // assign the templist to the adpapterlist

  propertyList = tempList;
  if (propertyList.size() > 0) {
  mAdapter = new PropertyRecyclerAdapter(PropertyListActivity.this, propertyList);
  mRecyclerView.setAdapter(mAdapter);
}
}

//使用此功能,RecyclerAdapter的索引会重新排列并顺畅滚动。