2012-01-27 11 views
1

長さが異なる5つのArrayListsがありますが、すべての配列リストを連結すると、すべての配列リストの項目が同じ。私は、項目が見つからないときにnullを入れるか、同じサイズの配列リストを作成したい。アンドロイドでこれを達成するためにどのように..アンドロイドで同じサイズのすべてのarraylistsを作成する方法

ArrayListの1 = 25の項目 attaylist 2 = 35の項目 arraylits 3 = 5つの項目 ArrayListを4 = 13の項目 ArrayListを事前にいくつかの例と感謝で私を助けてください5 = 40アイテム。

1つのビュー(eg.webビュー)にすべてのアイテムを表示すると、6番目のアイテムにスクロールすると5個までスクロールできます。これを克服する方法。助けてください

私はここで私のパースし、リストに追加します。

+2

あなたのコードを共有してください..以前に何を試しましたか? – user370305

+0

あなたは 'ArrayList'sをどのように正確に連結していますか? – Vladimir

答えて

2

コードが掲載されている場合に問題がどのようになっているかを確認するのは簡単です。あなたは境界例外のうちを取得するときには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 
} 

Hereさん、あなたがそれをチェックアウトしたい場合には例外処理のいくつかの入門資料。

3

私は知っているが、このことができます場合は表示されません。

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

}

2

この は技術的には、ArrayListには、可変配列の一種で、その純粋なJavaの問題..アンドロイドれていません... 呼び出すインデックスがarrayListのサイズより小さいかどうかを確認してください。連結する場合は連結してください。そうでない場合は例外が発生します。 またはarraylistをこのような配列に変換しようとします(データ型String):

String mArray[]=mArrayList.toArray(new String[SIZE]); 

これは、あなたがその例外がTHSTのArrayListにでない十分なスペースがある可能性がありました< MARRAY

幸運

+0

助けてくれてありがとう、私はコードを追加してください私のコードを確認してください私を助けてください – cavallo

1

ケースmArrayListサイズの要素を空にnullを代入します。 サイズ10のbのような3人のarraylistと、サイズ10のbと、サイズ10のcがあります。

これで、すべてのarraylistをサイズ10のArrayList zに追加しますが、索引したがって、IndexOutofBount例外が発生します。

ソリューション

ですから、その3つのすべてのArrayListのサイズの合計のような大きさで新しいのArrayList zを取らなければならないことを解決します。 を作成し、3つのarrayListデータをそのarrayListに追加します。

以下のコードを使用して、SizeをArrayListに与えることができます。

ArrayList<Integer>[size] arrayOfLists = new ArrayList<Integer>[size](); 

希望します。

私に知らせてください。

お楽しみください。 :))

+0

私はコードを追加してください私のコードを確認してくださいしてください – cavallo

2

は、私は別のデータ構造を持つことをお勧めします、そしてメインブック構造にその項目のリストを追加します

public class BOOK { 
String BId=""; 
String BTitle=""; 
String prelimTitle=""; 
String prelimIntro=""; 
String prelimInsight=""; 
} 
2

ようなあなたのクラス にいくつかのデフォルト値を確認し、新しい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; 
関連する問題