Android 无法在列表视图中设置不同的图像和文本

Android 无法在列表视图中设置不同的图像和文本,android,Android,我正试图在android中实现一个列表视图,在列表视图的每一行中显示不同的图像和文本。当我在Android studio中运行代码时,它显示错误: E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: com.vanjasrivastava.customlistview, PID: 1157 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.vanjasr

我正试图在android中实现一个列表视图,在列表视图的每一行中显示不同的图像和文本。当我在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);
}
}
改变

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

因为您的货币和国家有7个元素。

您的for循环计数大于国家/货币/标志列表的大小。 这就是为什么在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);
    }

Java从索引0开始,然后上升。大小为7表示索引为6。 您正在使用多个大小相同的阵列。因此,您可以从

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

})


确保迭代器索引小于数组大小:ifor(int i=0;i<7;i++){ . . . }
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);
}
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);
}