我試圖實現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);
}
}
確保您的迭代器的索引比數組大小小:'我
Blacklight
2014-11-21 12:11:31