I want to implement the two cells type in my app Image and text so i'm just testing this, my app works fine and load perfectly but tis crashes when i start scrolling with the following error
java.lang.ArrayIndexOutOfBoundsException: length=2; index=2130968624
this is my adapter
public class myadapter extends ArrayAdapter<myobject> {
Context context;
List<myobject> objectlist;
public myadapter(Context context, int resource, List<myobject> objects) {
super(context, resource, objects);
this.context = context;
this.objectlist = objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder2 tvholder;
ViewHolder ivholder;
LayoutInflater inflater =(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (getItemViewType(position) == R.layout.textcell){
if(convertView==null){
convertView= inflater.inflate(getItemViewType(position),parent,false);
tvholder= new ViewHolder2(convertView);
convertView.setTag(tvholder);
}else{
tvholder = (ViewHolder2)convertView.getTag();
}
tvholder.TV.setText(objectlist.get(position).getText());
}else{
if(convertView==null){
convertView= inflater.inflate(getItemViewType(position),parent,false);
ivholder = new ViewHolder(convertView);
convertView.setTag(ivholder);
}else{
ivholder=(ViewHolder) convertView.getTag();
}
ivholder.IV.setImageResource(objectlist.get(position).getImage());
}
return convertView;
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public int getItemViewType(int position) {
if(objectlist.get(position).getType()==1){
return R.layout.textcell;
}else{
return R.layout.imagecell;
}
}
}
class ViewHolder{
ImageView IV;
public ViewHolder(View view){
IV = (ImageView)view.findViewById(R.id.IV);
}
}
class ViewHolder2{
TextView TV;
public ViewHolder2(View view){
TV = (TextView)view.findViewById(R.id.TV);
}
}
my object :
public class myobject {
int type;
String Text;
int Image;
public myobject(){
this.type = 0;
this.Text = null;
this.Image = 0;
}
public myobject(int type,String Text,int Image){
this.type=type;
this.Image = Image;
this.Text = Text;
}
public int getType(){ return type;}
public String getText (){return Text;}
public int getImage (){return Image;}
}
Use in Main Activity :
public class MainActivity extends AppCompatActivity {
ListView LV;
List<myobject> myobjectList = new ArrayList<>();
myadapter myadapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LV = (ListView) findViewById(R.id.LV);
myobject myobject1 = new myobject(1,"teeeeeexxxxt",0);
myobject myobject2 = new myobject(2,null,R.mipmap.ic_launcher);
myobject myobject3 = new myobject(1,"teeeeeexxxxt",0);
myobject myobject4 = new myobject(1,"teeeeeexxxxt",0);
myobject myobject5 = new myobject(2,null,R.mipmap.ic_launcher);
myobject myobject6 = new myobject(2,null,R.mipmap.ic_launcher);
myobject myobject7 = new myobject(2,null,R.mipmap.ic_launcher);
myobject myobject8 = new myobject(1,"teeeeeexxxxt",0);
myobject myobject9 = new myobject(1,"teeeeeexxxxt",0);
myobjectList.add(myobject1);
myobjectList.add(myobject2);
myobjectList.add(myobject3);
myobjectList.add(myobject4);
myobjectList.add(myobject5);
myobjectList.add(myobject6);
myobjectList.add(myobject7);
myobjectList.add(myobject8);
myobjectList.add(myobject9);
myadapter = new myadapter(this,0,myobjectList);
LV.setAdapter(myadapter);
}
I really don't know what is causing this fatal error + is my implementation consider to be the most optimize approach for the 2 different cells type ?
any ideas would be much appreciated
See Question&Answers more detail:
os