Android 正在尝试创建自定义适配器
我开始学习Android开发课程,同时也在学习定制适配器。 执行此代码后,应用程序就会崩溃 这是我的全部密码 customlistview.javaAndroid 正在尝试创建自定义适配器,android,listview,indexoutofboundsexception,Android,Listview,Indexoutofboundsexception,我开始学习Android开发课程,同时也在学习定制适配器。 执行此代码后,应用程序就会崩溃 这是我的全部密码 customlistview.java package com.example.vamsi.customadapter; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import a
package com.example.vamsi.customadapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.HashMap;
/**
* Created by vamsi on 3/12/2017.
*/
public class customlistviewadadapter extends BaseAdapter {
private Context mcontext;
private ArrayList<HashMap<String,String>> books;
private static LayoutInflater inflater=null;
public customlistviewadadapter(Context context, ArrayList<HashMap<String,String>> data){
mcontext=context;
books=data;
inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return books.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view =convertView;
if(convertView==null){
view=inflater.inflate(R.layout.list_row,null);
TextView title = (TextView)view.findViewById(R.id.title);
TextView author=(TextView)view.findViewById(R.id.author);
TextView pages=(TextView)view.findViewById(R.id.pages);
ImageView image=(ImageView)view.findViewById(R.id.listimg);
HashMap<String,String> mbook=new HashMap<>();
mbook = books.get(position);
title.setText(mbook.get("title"));
author.setText(mbook.get("author"));
pages.setText(mbook.get("pages"));
}
return view;
}
}
我使用的是API级别25
任何帮助都将不胜感激页面有5个值,而不像其他页面有6个值
除此之外,您还硬编码了超过数组长度的一个
for (int i=0; i < 6; i++){ // Here
// Code removed
}
做一个数组列表
请参阅:非常感谢!!我忽略了这一点,以前完全吓坏了。;欢迎我得好好读一读。您可以通过以下方式表达您的感谢。哦,对不起,matebam是stackoverflow的新成员,您做到了吗,还有更多有关androidstudio的资源请如果您有任何其他问题,请创建新的帖子-询问教程是离题的
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:background="@drawable/listselector"
android:orientation="horizontal"
android:layout_height="wrap_content"
>
<LinearLayout
android:id="@+id/thumbnail"
android:padding="3dp"
android:layout_alignParentLeft="true"
android:background="#ffeeeade"
android:layout_marginRight="5dp"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/listimg"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@mipmap/ic_launcher"/>
</LinearLayout>
<TextView
android:id="@+id/title"
android:layout_alignTop="@+id/thumbnail"
android:layout_width="wrap_content"
android:text="the alchemist"
android:textSize="14sp"
android:textColor="#040404"
android:layout_toRightOf="@+id/thumbnail"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/author"
android:layout_below="@+id/title"
android:text="paulo"
android:layout_toRightOf="@+id/thumbnail"
android:textColor="#ffde3a3d"
android:layout_marginTop="2dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="234 pages"
android:id="@+id/pages"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/title"
android:layout_marginRight="10dp"
android:textColor="#10bcc9"
android:gravity="right"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@android:drawable/ic_media_play"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"/>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.vamsi.customadapter.MainActivity">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="@+id/listview1"
android:listSelector="@drawable/listselector"
android:divider="@color/colorAccent"
android:dividerHeight="1dp" />
</RelativeLayout>
V/BoostFramework: mIOPStart method = public int com.qualcomm.qti.Performance.perfIOPrefetchStart(int,java.lang.String)
V/BoostFramework: mIOPStop method = public int com.qualcomm.qti.Performance.perfIOPrefetchStop()
V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@28f08ea
V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@a9a47db
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.vamsi.customadapter, PID: 6371
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.vamsi.customadapter/com.example.vamsi.customadapter.MainActivity}: java.lang.ArrayIndexOutOfBoundsException: length=5; index=5
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2728)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2814)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1527)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6290)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.ArrayIndexOutOfBoundsException: length=5; index=5
at com.example.vamsi.customadapter.MainActivity.onCreate(MainActivity.java:51)
at android.app.Activity.performCreate(Activity.java:6760)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1134)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2681)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2814)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1527)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6290)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
D/AppTracker: App Event: crash
I/Process: Sending signal. PID: 6371 SIG: 9
Disconnected from the target VM, address: 'localhost:8612', transport: 'socket'
for (int i=0; i < 6; i++){ // Here
// Code removed
}
public static class BookData {
public String title, author, pages;
}