2015-08-08 72 views
2
int i = 0; //used for getting item position 
for (Iterator<CustomObject> iterator = mAdapter.getItemsData().iterator(); iterator.hasNext();) { 

    if (mAdapter.getItemsData().get(i).getPriceTier() != 1) { //if statement throws error 
     // Remove the current element from the iterator and the list. 
     iterator.remove(); 
     mAdapter.removeFromItemsData(i); 
    } 
    i++; 
} 

使用this計算器回答,我通過我的CustomObject的ArrayList的迭代。自定義對象只是一個帶有setter和getter的POJO類。 setter和getters具有不同的數據類型,如int,String,double等。java.lang.IndexOutOfBoundsException:無效的指數18,大小爲18

mAdapter.getItemsData從適配器類返回araylist。

//method just notifies the RecyclerView that an item was removed. 
public void removeFromItemsData(int position){ 

    notifyItemRemoved(position); 
} 

這裏是例外

java.lang.IndexOutOfBoundsException:無效索引18,尺寸爲18 在java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255) 在java的。 util.ArrayList.get(ArrayList.java:308)at com.test.TestActivity $ 5.onClick(TestActivity.java:280)at android.view.View.performClick(View.java:4780)at android.view 。查看$ PerformClick.run(View.java:19866)at android.os.Handler.handleCallback(Han droid.java:739)at android.os.Handler.dispatchMessage(Handler.java:95)at android.os.Looper.loop(Looper.java:135)at android.app.ActivityThread.main(ActivityThread。 java:5254)at java.lang.reflect.Method.invoke(Native Method)at java.lang.reflect.Method.invoke(Method.java:372)at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller .RUN(ZygoteInit.java:903) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

CustomObject類

public class CustomObject{ 
    private String name; 
    private int distance; 
    private String category; 
    private String id; 
    private double rating; 
    private String ratingColor; 
    private String prefix; 
    private String suffix; 
    private int priceTier; 
    private String currency; 
    private String phone; 
    private String twitter; 
    private String url; 
    private String menu; 
    String address; 
    String city; 
    String state; 
    String postalCode; 
    double lat; 
    double lng; 
    String review; 
    int reviewCount; 
    String firstName; 
    String lastName; 
    String userIconPrefix; 
    String userIconSuffix; 
    String secondsReview; 

    public CustomObject() { 
     this.name = ""; 
     this.distance = 0; 
     this.setCategory(""); 
     this.id = ""; 
     this.rating = 0; 
     this.ratingColor = ""; 
     this.prefix = ""; 
     this.suffix = ""; 
     this.priceTier = 0; 
     this.currency = ""; 
     this.phone = ""; 
     this.twitter = ""; 
     this.url = ""; 
     this.menu = ""; 
     this.address = ""; 
     this.city = ""; 
     this.state = ""; 
     this.postalCode = ""; 
     this.lat = 0; 
     this.lng = 0; 
     this.review = ""; 
     this.reviewCount = 0; 
     this.firstName = ""; 
     this.lastName = ""; 
     this.userIconPrefix = ""; 
     this.userIconSuffix = ""; 
     this.secondsReview = ""; 
    } 

    public void setSecondsReview(String secondsReview){ 
     this.secondsReview = secondsReview; 
    } 

    public String getSecondsReview(){ 
     return secondsReview; 
    } 

    public void setUserIconSuffix(String userIconSuffix){ 
     this.userIconSuffix = userIconSuffix; 
    } 

    public String getUserIconSuffix(){ 
     return userIconSuffix; 
    } 

    public void setUserIconPrefix(String userIconPrefix){ 
     this.userIconPrefix = userIconPrefix; 
    } 

    public String getUserIconPrefix(){ 
     return userIconPrefix; 
    } 

    public void setFirstName(String firstName){ 
     this.firstName = firstName; 
    } 

    public String getFirstName(){ 
     return firstName; 
     //todo if String firstNAme == null, then return "a foursquare user" 
    } 

    public void setLastName(String lastName){ 
     this.lastName = lastName; 
    } 

    public String getLastName(){ 
     return lastName; 
    } 

    public void setReviewCount(int reviewCount){ 
     this.reviewCount = reviewCount; 
    } 

    public int getReviewCount(){ 
     return reviewCount; 
    } 

    public void setReview(String review){ 
     this.review = review; 
    } 

    public String getReview(){ 
     return review; 
    } 

    public void setLng(double lng){ 
     this.lng = lng; 
    } 

    public double getLng(){ 
     return lng; 
    } 

    public void setLat(double lat){ 
     this.lat = lat; 
    } 

    public double getLat(){ 
     return lat; 
    } 

    public void setPostalCode(String postalCode){ 
     this.postalCode = postalCode; 
    } 

    public String getPostalCode(){ 
     return postalCode; 
    } 

    public void setState(String state){ 
     this.state = state; 
    } 

    public String getState(){ 
     return state; 
    } 

    public void setCity(String city){ 
     this.city = city; 
    } 

    public String getCity(){ 
     return city; 
    } 

    public void setAddress(String address){ 
     this.address = address; 
    } 

    public String getAddress(){ 
     return address; 
    } 

    public void setMenuUrl(String menu){ 
     this.menu = menu; 
    } 

    public String getMenuUrl(){ 
     return menu; 
    } 

    public void setUrl(String url){ 
     this.url = url; 
    } 

    public String getUrl(){ 
     return url; 
    } 

    public void setTwitter(String twitter){ 
     this.twitter = twitter; 
    } 

    public String getTwitter(){ 
     return twitter; 
    } 

    public void setPhone(String phone){ 
     this.phone = phone; 
    } 

    public String getPhone(){ 
     return phone; 
    } 

    public String getCurrency(){ 
     return currency; 
    } 

    public void setCurrency(String currency){ 
     this.currency = currency; 
    } 

    public int getPriceTier(){ 
     return priceTier; 
    } 

    public void setPriceTier(int priceTier){ 
     this.priceTier = priceTier; 
    } 

    public String getSuffix(){ 
     return suffix; 
    } 

    public void setSuffix(String suffix){ 
     this.suffix = suffix; 
    } 

    public String getPrefix(){ 
     return prefix; 
    } 

    public void setPrefix(String prefix){ 
     this.prefix = prefix; 
    } 

    public double getRating(){ 
     if(rating > 0){ 
      return rating; 
     } 
     return 0; //TODO display symbol if rating not found 
    } 

    public void setRating(double rating){ 
     this.rating = rating; 
    } 

    public Integer getDistance() { 
     if (distance >= 0) { 
      return distance; 
     } 
     return 0; //TODO display symbol if distance not found, like N/A 
    } 

    public void setDistance(int distance) { 
      this.distance = distance; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public String getName() { 
     return name; 
    } 

    public String getCategory() { 
     return category; 
    } 

    public void setCategory(String category) { 
     this.category = category; 
    } 

    public void setId(String id) { 
     this.id = id; 
    } 

    public String getId() { 
     return id; 
    } 

} 

如何添加數據

在onCreate活動中,我實例化了Recyclerview。 RecyclerView需要一個arraylist,所以我通過一個空的。

List listTitle = new ArrayList(); //empty arraylist 
mAdapter = new Fragment1Adapter(listTitle); 

RecyclerView mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView); 
mRecyclerView.setHasFixedSize(true); 
mRecyclerView.setItemAnimator(new DefaultItemAnimator()); 
mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); 
mRecyclerView.setAdapter(mAdapter); 

在稍後的AsyncTask中,我將這些項目添加到適配器。

for (int i = 0; i < jsonArray.length(); i++) { 
    CustomObject poi = new CustomObject(); 

    if (jsonArray.getJSONObject(i).has("venue")) { 
     poi.setName(jsonArray.getJSONObject(i).getJSONObject("venue").getString("name")); 
     poi.setId(jsonArray.getJSONObject(i).getJSONObject("venue").getString("id")); 
    } 
    mAdapter.addItem(poi); 
} 

這裏是的addItem類

宣佈在適配器類的頂部private final List<FoursquareVenue> itemsData;

public void addItem(FoursquareVenue data) { 
     itemsData.add(data); 
     notifyItemInserted(itemsData.size() - 1); 
    } 
+2

例外的第一行清楚地說明最新的錯誤。索引從0開始到大小 - 1 –

+0

我們知道這是一個出界異常的索引,但它爲什麼會拋出它。 –

+0

感謝您@HaloMediaz你現在可以告訴我你是如何將這個信息添加到你的列表和適配器?你如何添加customObject的細節。另外,你能看到我開始的討論,你從中獲得了什麼?我不確定聊天記錄 –

回答

1

編輯

你既然我們的聊天和編輯內容:

你需要添加和刪​​除項目到您的ArrayList和他們通知適配器的變化。

CustomObject poi = new CustomObject(); 
if (jsonArray.getJSONObject(i).has("venue")) { 
    poi.setName(jsonArray.getJSONObject(i).getJSONObject("venue").getString("name")); 
    poi.setId(jsonArray.getJSONObject(i).getJSONObject("venue").getString("id")); 
} 
listTitle.addItem(poi);// Add to ListArray 

然後通過您的ArrayList循環刪除項目。


你剛剛混淆了你自己在如何試圖訪問你的數據。

存在一個問題,即您正在創建CustomObjects的迭代器,並將其解析爲數據類型mAdapter.getItemsData()

從迭代器中刪除元素應該從列表中刪除它。

for (Iterator<CustomObject> iterator = mAdapter.iterator(); iterator.hasNext();) { 
    CustomObject itemsData = iterator.next(); 
    if (itemsData.getItemsData().get(i).getPriceTier() != 1) { //if statement throws error 
      // Remove the current element from the iterator and the list. 

      iterator.remove(); 
     } 
     i++; 
     } 
    CustomObject itemsData = iterator.next(); 

讓我知道這是否有幫助。

+1

上的重複規則,我只是想補充一點,我不確定你是否試圖通過每個對象中的某個列表循環訪問。 –

+0

'CustomObject itemsData = iterator.next();'我只能訪問制定者和獲得者。我需要訪問位於適配器類內的數組列表。感謝您嘗試ti幫助:) – HaloMediaz

+1

@HaloMediaz你能給我一些更多的信息嗎?如果我能看到你的班級,以及你想要訪問和做什麼,這將是有益的。 –

相關問題