我正在尝试在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 :(得分:4)
更改
for(int i=0;i<7;i++){
因为您的currency
和countries
上有7
个元素。
答案 1 :(得分: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);
}
答案 2 :(得分:0)
您的for循环次数大于countries / currency / flag列表的大小。 这就是为什么在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);
}
答案 3 :(得分:0)
Java从索引0开始然后上升。大小为7表示指数为6。 您正在使用多个具有相同大小的阵列。所以你可以改变你的循环
for(int i=0;i<10;i++){
.
.
.
}
到
for(int i=0;i<7;i++){
.
.
.
}