2016-08-08 53 views
-4

// Mein RecyclerAdpater Code hier:Recycler Ansicht abgestürzt, während bei der Wiederaufnahme der Tätigkeit Scrollen

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

// Protokollfehler ist:

E/Android Runtime: FATAL AUSNAHME: Haupt Prozess: com.excellenceit .propcare, PID: 9927 java.lang.IndexOutOfBoundsException: Inkonsistenz festgestellt. Ungültige Artikelposition 4 (Offset: 4) .State: 9 bei android.support.v7.widget.RecyclerView $ Recycler.getViewForPosition (RecyclerView.java:4957) bei android.support.v7.widget.RecyclerView $ Recycler.getViewForPosition (RecyclerView.java:4913) bei android.support.v7.widget.LinearLayoutManager $ LayoutState.next (LinearLayoutManager.java:2029) bei android.support.v7.widget.LinearLayoutManager.layoutChunk (LinearLayoutManager.java:1414) unter android.support.v7.widget.LinearLayoutManager.fill (LinearLayoutManager.java:1377) unter android.support.v7.widget.LinearLayoutManager.scrollBy (LinearLayoutManager.java:1193) unter android.support.v7.widget.LinearLayoutManager .s crollVerticallyBy (LinearLayoutManager.java:1043) bei android.support.v7.widget.RecyclerView.scrollByInternal (RecyclerView.java:1552) bei android.support.v7.widget.RecyclerView.onTouchEvent (RecyclerView.java:2649) bei android.view.View.dispatchTouchEvent (View.java:8968) bei android.view.ViewGroup.dispatchTransformedTouchEvent (ViewGroup.java:2698) bei android.view.ViewGroup.dispatchTouchEvent (ViewGroup.java:2410) bei android. view.ViewGroup.dispatchTransformedTouchEvent (ViewGroup.java:2709) bei android.view.ViewGroup.dispatchTouchEvent (ViewGroup.java:2425) bei android.view.ViewGroup.dispatchTrans formedTouchEvent (ViewGroup.java:2709) bei android.view.ViewGroup.dispatchTouchEvent (ViewGroup.java:2425) bei android.view.ViewGroup.dispatchTransformedTouchEvent (ViewGroup.java:2709) bei android.view.ViewGroup.dispatchTouchEvent (ViewGroup.java:2425) bei android.view.ViewGroup.dispatchTransformedTouchEvent (ViewGroup.java:2709) bei android.view.ViewGroup.dispatchTouchEvent (ViewGroup.java:2425) bei android.view.ViewGroup.dispatchTransformedTouchEvent (Viewgroup. Java: 2709) bei android.view.ViewGroup.dispatchTouchEvent (ViewGroup.java:2425) bei android.view.ViewGroup.dispatchTransformedTouchEvent (Vie wGroup.java:2709) bei android.view.ViewGroup.dispatchTouchEvent (ViewGroup.java:2425) bei android.view.ViewGroup.dispatchTransformedTouchEvent (ViewGroup.java:2709) bei android.view.ViewGroup.dispatchTouchEvent (Viewgroup. Java: 2425) bei android.view.ViewGroup.dispatchTransformedTouchEvent (ViewGroup.java:2709) bei android.view.ViewGroup.dispatchTouchEvent (ViewGroup.java:2425) bei android.view.ViewGroup.dispatchTransformedTouchEvent (ViewGroup.java: 2709) bei android.view.ViewGroup.dispatchTouchEvent (ViewGroup.java:2425) bei com.android.internal.policy.impl.PhoneWindow $ DecorView.superDispatchTouchEve nt (PhoneWindow.java:2568) bei com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent (PhoneWindow.java: 1776) bei android.app.Activity.dispatchTouchEvent (Activity.java:2866) bei android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent (WindowCallbackWrapper.java:63) bei com.android.internal.policy. impl.PhoneWindow $ DecorView.dispatchTouchEvent (PhoneWindow.java:2529) bei android.view.View.dispatchPointerEvent (View.java:9173) bei android.view.ViewRootImpl $ ViewPostImeInputStage.processPointerEvent (ViewRootImpl.java:4706) bei android.view.ViewRootImpl $ ViewPostImeInputStage.onProcess (ViewRootImpl.java:4544) bei android.view.ViewRootImpl $ InputStage.deliver (ViewRootImpl.java:4068) bei Androi d.View.ViewRootImpl $ InputStage.onDeliverToNext (ViewRootImpl.java:4121) bei android.view.ViewRootImpl $ InputStage.forward (ViewRootImpl.java:4087) bei android.view.ViewRootImpl $ AsyncInputStage.forward (ViewRootImpl.java: 4201) bei android.view.ViewRootImpl $ InputStage.apply (ViewRootImpl.java:4095) bei android.view.ViewRootImpl $ AsyncInputStage.apply (ViewRootImpl.java:4258) bei android.view.ViewRootImpl $ InputStage.deliver (ViewRootImpl.java:4068) bei android.view.ViewRootImpl $ InputStage.onDeliverToNext (ViewRootImpl.java:4121) bei android.view.ViewRootImpl $ InputStage.forward (ViewRootImpl.java:4087) bei android.view.ViewRootImpl $ InputStage.apply (ViewRootImpl.java:4095) bei android.view.ViewRootImpl $ InputStage.deliver (ViewRootImpl.java:4068) bei android.view.ViewRootImpl

+1

können Sie Ihren Adaptercode auch posten –

+0

sieht aus wie Ihre Datenquelle hat nicht die gleiche Anzahl von Elementen wie Ihre Recycleransicht erwartet – akdsouza

+0

Ich habe Post-Frage mit Quellcode. – Dadaji

Antwort

0

Es ist etwas falsch mit die Position in Ihrem onBindViewHolder wie Ihrer Datenliste oder Datenarray hat möglicherweise keine Daten für diese Position.

0

Ja, schließlich habe ich die Antworten der obigen Frage und folgen Sie den Schritten:

1) Erstellen Sie die templist als Arraylist Ihre modelclass verwenden.

2) die templist zum Array zugeordnet, die an den RecyclerAdapter geben wird.

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

// Parsing Json Daten

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

// dies der Index der RecyclerAdapter Verwendung selbst neu anordnen und Scrollen glatt.