#android #simplecursoradapter
#Android #simplecursoradapter
Вопрос:
Я использую адаптеры курсора для заполнения моего listview, но когда я меняю курсор, это выдает ошибку java.lang.Исключение ArrayIndexOutOfBoundsException
package me.nkkumawat.chatzzz.Adapters;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.support.v7.widget.CardView;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import me.nkkumawat.chatzzz.R;
public class ChatAdapter extends CursorAdapter {
private Cursor cursor;
public ChatAdapter(Context context, Cursor cursor) {
super(context, cursor, 0);
this.cursor = cursor;
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return LayoutInflater.from(context).inflate(R.layout.list_chat, parent, false);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView tv_message = (TextView)view.findViewById(R.id.chat_message);
TextView tv_message_from = (TextView)view.findViewById(R.id.message_from);
String body = cursor.getString(1);
tv_message_from.setText(cursor.getString(2));
tv_message.setText(body);
}
@Override
public int getViewTypeCount() {
return getCount();
}
@Override
public int getItemViewType(int position) {
if(position < getCount()) {
return position;
}else {
return 0;
}
}
}
Ошибка связана с java.lang.Исключение ArrayIndexOutOfBoundsException.
2019-03-22 11:52:40.860 10887-10887/me.nkkumawat.chatzzz E/AndroidRuntime: FATAL EXCEPTION: main
Process: me.nkkumawat.chatzzz, PID: 10887
java.lang.ArrayIndexOutOfBoundsException: length=172; index=172
at android.widget.AbsListView$RecycleBin.addScrapView(AbsListView.java:6851)
at android.widget.ListView.layoutChildren(ListView.java:1689)
at android.widget.AbsListView.onLayout(AbsListView.java:2161)
at android.view.View.layout(View.java:17535)
at android.view.ViewGroup.layout(ViewGroup.java:5616)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
at android.view.View.layout(View.java:17535)
at android.view.ViewGroup.layout(ViewGroup.java:5616)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
at android.view.View.layout(View.java:17535)
at android.view.ViewGroup.layout(ViewGroup.java:5616)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:17535)
at android.view.ViewGroup.layout(ViewGroup.java:5616)
at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:443)
at android.view.View.layout(View.java:17535)
at android.view.ViewGroup.layout(ViewGroup.java:5616)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:17535)
at android.view.ViewGroup.layout(ViewGroup.java:5616)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1494)
at android.view.View.layout(View.java:17535)
at android.view.ViewGroup.layout(ViewGroup.java:5616)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at com.android.internal.policy.DecorView.onLayout(DecorView.java:724)
at android.view.View.layout(View.java:17535)
at android.view.ViewGroup.layout(ViewGroup.java:5616)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2354)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2081)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1258)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6348)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:871)
at android.view.Choreographer.doCallbacks(Choreographer.java:683)
at android.view.Choreographer.doFrame(Choreographer.java:619)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:857)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6123)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)
Я хочу предотвратить состояние listview. Если я удаляю метод getItemViewType, то при прокрутке listview изменяется и выглядит плохо. Я хочу предотвратить состояние Listview.
Пожалуйста, помогите и заранее благодарим.
Комментарии:
1. Пожалуйста, попробуйте использовать cursor.getString(cursor.getColumnIndex(COLUMN_NAME_HERE_IN_STRING));
2. это не помогает