2014-11-21 33 views
-3

我試圖實現Android中的listview,它在列表視圖的每一行中顯示不同的圖像和文本。當我運行我的Android Studio中的代碼,它顯示錯誤:無法在列表視圖中設置不同的圖像和文本

E/AndroidRuntime﹕ FATAL EXCEPTION: main 
Process: com.vanjasrivastava.customlistview, PID: 1157 
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.vanjasrivastava.customlistview/com.vanjasrivastava.customlistview.MyActivity}: java.lang.ArrayIndexOutOfBoundsException: length=7; index=7 


Caused by: java.lang.ArrayIndexOutOfBoundsException: length=7; index=7 
     at com.vanjasrivastava.customlistview.MyActivity.onCreate(MyActivity.java:90) 

在MyActivity.java線90號是:

hm.put("txt", "Country : " + countries[i]); 

請指導。提前致謝。

下面是我的代碼:

MyActivity.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" > 

<TextView 
    android:id="@+id/textview" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    /> 

<ListView 
    android:id="@+id/listview" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    /> 
    </LinearLayout> 

mylist.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="horizontal" 
> 
<ImageView 
    android:id="@+id/flag" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 


    android:paddingTop="10dp" 
    android:paddingRight="10dp" 
    android:paddingBottom="10dp" 
    /> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    > 
    <TextView 
     android:id="@+id/txt" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textSize="15dp" 
     /> 

    <TextView 
     android:id="@+id/cur" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textSize="10dp" 
     /> 
</LinearLayout> 
</LinearLayout> 

MyActivity.java

package com.vanjasrivastava.customlistview; 

import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.List; 

import android.app.Activity; 
import android.os.Bundle; 
    import android.widget.ListView; 
import android.widget.SimpleAdapter; 

public class MyActivity extends Activity { 

// Array of strings storing country names 
String[] countries = new String[] { 

     "Mr.Vikas Raina", 
     "Mrs. Jeetu Sharma", 
     "Mr.Kuashik Ghosh", 
     "Mr.Niranjan Lal", 
     "Ms. Swati Garg", 
     "Ms. Manju", 
     "Mr. Manish K. Kakhani", 

}; 

// Array of integers points to images stored in /res/drawable-ldpi/ 
int[] flags = new int[]{ 

     R.drawable.ghosh, 

     R.drawable.jeetu, 

     R.drawable.manish, 
     R.drawable.manju, 

     R.drawable.nlal, 

     R.drawable.swati, 

     R.drawable.vikas 


}; 

// Array of strings to store currencies 
String[] currency = new String[]{ 

     "Assistant Professor", 
     "Assistant Professor", 

     "Assistant Professor", 
     "Assistant Professor", 
     "Assistant Professor", 
     "Assistant Professor", 
     "Assistant Professor", 


}; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_my); 

    // Each row in the list stores country name, currency and flag 
    List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>(); 

    for(int i=0;i<10;i++){ 
     HashMap<String, String> hm = new HashMap<String,String>(); 
     hm.put("txt", "Country : " + countries[i]); 
     hm.put("cur","Currency : " + currency[i]); 
     hm.put("flag", Integer.toString(flags[i])); 
     aList.add(hm); 
    } 

    // Keys used in Hashmap 
    String[] from = { "flag","txt","cur" }; 

    // Ids of views in listview_layout 
    int[] to = { R.id.flag,R.id.txt,R.id.cur}; 

    // Instantiating an adapter to store each items 
    // R.layout.listview_layout defines the layout of each item 
    SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), aList, R.layout.mylist, from, to); 

    // Getting a reference to listview of main.xml layout file 
    ListView listView = (ListView) findViewById(R.id.listview); 

    // Setting the adapter to the listView 
    listView.setAdapter(adapter); 
} 
} 
+0

確保您的迭代器的索引比數組大小小:'我 Blacklight 2014-11-21 12:11:31

回答

1
for(int i=0;i<10;i++){ 
    HashMap<String, String> hm = new HashMap<String,String>(); 
    hm.put("txt", "Country : " + countries[i]); 
    hm.put("cur","Currency : " + currency[i]); 
    hm.put("flag", Integer.toString(flags[i])); 
    aList.add(hm); 
} 

**在這裏,你正試圖把10個值,但你只有在你的國家和貨幣陣列7 ArrayIndexOutOfBoundsException異常是結果的那個**

String[] countries = new String[] { 

    "Mr.Vikas Raina", 
    "Mrs. Jeetu Sharma", 
    "Mr.Kuashik Ghosh", 
    "Mr.Niranjan Lal", 
    "Ms. Swati Garg", 
    "Ms. Manju", 
    "Mr. Manish K. Kakhani", 

};

for(int i=0;i<7;i++){ 
    HashMap<String, String> hm = new HashMap<String,String>(); 
    hm.put("txt", "Country : " + countries[i]); 
    hm.put("cur","Currency : " + currency[i]); 
    hm.put("flag", Integer.toString(flags[i])); 
    aList.add(hm); 
} 
+1

哎呀!我忘了檢查這個!謝謝。:-) – user2900761 2014-11-21 15:47:47

4

變化

for(int i=0;i<7;i++){ 

因爲您的currencycountries上有7個元素。

+0

好的,謝謝......笏一個愚蠢的錯誤.. – user2900761 2014-11-21 15:48:37

0

您的循環次數大於國家/貨幣/國旗列表的大小。 這就是爲什麼在i = 0到i = 6之後,它會通過ArrayIndexOutOfBoundsException。

for循環更改爲這個..

for(int i=0;i<countries.length();i++){ 
     HashMap<String, String> hm = new HashMap<String,String>(); 
     hm.put("txt", "Country : " + countries[i]); 
     hm.put("cur","Currency : " + currency[i]); 
     hm.put("flag", Integer.toString(flags[i])); 
     aList.add(hm); 
    } 
0

的Java開始於指數0,上升。大小爲7表示索引爲6. 您正在處理所有大小相同的多個數組。所以,你可以從

for(int i=0;i<10;i++){ 
. 
. 
. 
} 

改變你的循環來

for(int i=0;i<7;i++){ 
. 
. 
. 
} 
相關問題