[英]How to make all arraylists of same size in android
我得到了五個具有不同長度的ArrayList,但是當我連接所有數組列表時,由於所有數組列表中的項都不相同,所以我得到了數組索引超出范圍的異常。 我想在找不到項目時包括null或使數組列表大小相同。 如何在android ..中實現這一點。請幫助我提供一些示例,並在此先感謝
arraylist 1 = 25項attaylist 2 = 35項arraylits 3 = 5項arraylist 4 = 13項arraylist 5 = 40項。
當我在一個視圖(例如Web視圖)中顯示所有項目時,當滾動到第6個項目時,我最多可以滾動5個項目。 如何克服這一點。 請幫忙
這是我解析並添加到列表的代碼。
我不知道,但是看看是否有幫助:
package your.package
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class TestProjectActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
setContentView(tv);
ArrayList<Integer> a = new ArrayList<Integer>();
a.add(1);
a.add(2);
a.add(3);
a.add(4);
ArrayList<Integer> b = new ArrayList<Integer>();
b.add(5);
b.add(6);
b.add(7);
a.addAll(b);
String p = "";
for (int i = 0; i < a.size(); i++)
{
p = String.format("%s, %d", p, a.get(i));
}
tv.setText(p);
}
}
如果發布代碼,則更容易發現問題所在。 但是,如果要在超出范圍的異常時返回null,則應執行以下操作:
try {
//your code that's throwing index out of bounds
//this block should include as little of your code as possible
//for example, don't put a for loop in here --
// put this inside the for loop if you can.
} catch (IndexOutOfBoundsException e) {
return null;
// or do what you need to do with the null
}
這是一些有關異常處理的介紹性材料,以防您想檢出它。
從技術上講,這不是android。它是純Java問題,arraylist是一種可變數組...只要檢查要調用的索引的位置是否小於arrayList的大小(如果是,請進行級聯),否則請檢查它是否在外綁定異常..或者您可以嘗試將您的arraylist轉換成這樣的數組(假設數據類型為String):
String mArray[]=mArrayList.toArray(new String[SIZE]);
如果mArrayList size <mArray,這將為空元素分配null
祝好運
在類中設置一些默認值,例如
public class BOOK {
String BId="";
String BTitle="";
String prelimTitle="";
String prelimIntro="";
String prelimInsight="";
}
我建議使用其他數據結構,並將該項目的列表添加到主要的Book Structure中,新的Datastructre可能是:
case XmlPullParser.END_TAG:
if (xmlpullparser.getName().toString().equals("BOOK")) {
book.setInsightList(insightList);
book.setcasestudyList(casestudyList);
book.setquotesList(quoteList);
book.setoneminwonderList(oneminwonderList);
book.setsecretList(secretList);
bookList.add(book);
Intro.book = book;
} else if (xmlpullparser.getName().toString().equals("chapter")) {
if(objInsight==null)
objInsight=new INSIGHT();
listInsights.add(objInsight);
objInsight=null;
} else if (xmlpullparser.getName().toString().equals("secret")) {
secretList.add(secret);
} else if (xmlpullparser.getName().toString().equals("prelim")) {
secretList.add(secret);
this.prelim = false;
} else if (xmlpullparser.getName().toString().equals("prelimTitle")) {
this.prelimTitle = false;
} else if (xmlpullparser.getName().toString().equals("prelimIntro")) {
this.prelimIntro = false;
} else if (xmlpullparser.getName().toString()
.equals("prelimInsight")) {
this.prelimInsight = false;
} else if (xmlpullparser.getName().toString()
.equals("prelimContent")) {
this.prelimContent = false;
} else if (xmlpullparser.getName().toString()
.equals("chapterIntro")) {
secretList.add(secret);
this.chapterIntro = false;
} else if (xmlpullparser.getName().toString().equals("bold")) {
this.chapterIntrobold = false;
} else if (xmlpullparser.getName().toString().equals("secretIntro")) {
this.secretIntro = false;
} else if (xmlpullparser.getName().toString().equals("insight")) {
} else if (xmlpullparser.getName().toString().equals("caseStudy")) {
this.caseStudy = false;
casestudyList.add(casestudy);
} else if (xmlpullparser.getName().toString().equals("oneMinute")) {
this.oneMinute = false;
oneminwonderList.add(oneminwonder);
} else if (xmlpullparser.getName().toString().equals("quote")) {
quoteList.add(quotes);
} else if (xmlpullparser.getName().toString()
.equals("quoteContent")) {
this.quoteContent = false;
} else if (xmlpullparser.getName().toString()
.equals("quoteCitation")) {
this.quoteCitation = false;
} else if (xmlpullparser.getName().toString()
.equals("secretContent")) {
this.secretContent = false;
}
break;
您得到的例外可能是數組列表中沒有足夠的空間。 假設存在三個數組列表,例如a的大小為10,b的大小為10,c的大小為10。
現在,您將所有arraylist添加到大小為10的ArrayList z中,則總共有30個數據,但索引只有10個。因此,這將導致IndexOutofBount異常。
解
因此,要解決此問題,必須采用新的ArrayList z,其大小應類似於所有三個arraylist大小的和。 然后將三個arrayList數據添加到該arrayList。
您可以使用以下代碼將Size賦予ArrayList。
ArrayList<Integer>[size] arrayOfLists = new ArrayList<Integer>[size]();
希望對您有幫助。
如果沒有,請告訴我。
請享用。 :))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.