我正在使用一个ListView并将一个适配器设置为两个Arraylist。 List和List1是两个ArrayList,它与SqliteDatabase的GetAllItems不同。
我的问题是,List有假设10个项目,List1有7个项目然后当我滚动我的Listview并成功显示项目到7但是每当第8个项目然后给出错误ArrayOutOfBoundExcecption。因为ArrayList的大小不同。列表总是比List1大。
如何解决此错误?
Plesae指导我。
My code is,
Adapter1 adapter = new Adapter1(this,
List, List1);
listView1.setAdapter(Adapter1);
dbAdapter.openForRead();
list = dbAdapter.getAllMyData();
dbAdapter.close();
dbAdapter.openForRead();
list1 = dbAdapter.getDisplayLike();
dbAdapter.close();
/** Adapter Class */
public class Adapter1 extends BaseAdapter {
Context context = null;
LayoutInflater layoutInflater = null;
List<List> list = new ArrayList<List>();
List<List1> list1 = new ArrayList<List1>();
public Adapter1(Context context, List<List> list ,
List<List1> list1 ) {
this.context = context;
this.list = list ;
this.list1 = list1;
layoutInflater = LayoutInflater.from(context);
viewHolder = new ViewHolder();
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@SuppressLint("InflateParams")
@Override
public View getView(final int position, View convertView,
ViewGroup parent) {
View vi = convertView;
if (vi == null) {
vi = layoutInflater.inflate(R.layout.list_item,
parent, false);
viewHolder = new ViewHolder();
/** Initialize Widgets */
/** Imageview */
viewHolder.imgUserIcon = (ImageView) vi
.findViewById(R.id.imgUserIcon);
viewHolder.imgImage = (ImageView) vi
.findViewById(R.id.imgImage);
viewHolder.imgUnlike = (ImageView) vi
.findViewById(R.id.imgUnlike);
viewHolder.imgUnlike.setTag(position);
viewHolder.imgLike = (ImageView) vi
.findViewById(R.id.imgLike);
viewHolder.imgLike.setTag(position);
/** TextView */
viewHolder.txtPostId = (TextView) vi
.findViewById(R.id.txtPostId);
viewHolder.txtUserName = (TextView) vi
.findViewById(R.id.txtUserName);
viewHolder.txtDateTime = (TextView) vi
.findViewById(R.id.txtDateTime);
viewHolder.txtLikeUnlike = (TextView) vi
.findViewById(R.id.txtLikeUnlike);
viewHolder.txtLikeUnlike.setTag(position);
vi.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) vi.getTag();
}
final List listBean= list .get(position);
final List1 list1Bean= list1.get(position);
viewHolder.txtPostId.setText(listBean.getiPostId()
+ "");
viewHolder.txtUserName.setText(listBean.getName());
viewHolder.txtDateTime.setText(listBean.getStrCreated());
viewHolder.txtLikeUnlike.setText(listBean.getiLike()
+ "");
int iDisplayLike = list1Bean.getiDisplayLike();
if (iDisplayLike == 1) {
viewHolder.imgUnlike.setVisibility(View.GONE);
viewHolder.imgLike.setVisibility(View.VISIBLE);
}
if (iDisplayLike == 0) {
viewHolder.imgLike.setVisibility(View.GONE);
viewHolder.imgUnlike.setVisibility(View.VISIBLE);
}
});
return vi;
}
class ViewHolder {
ImageView imgIcon = null, imgImage = null,
imgUnlike = null;
TextView txtPostId = null, txtUserName = null,
txtDateTime = null,
txtLikeUnlike = null;
}
答案 0 :(得分:0)
如果您从两个表中获取数据,则首先填充ArrayList
,然后在适配器中传递单个ArrayList
以显示数据而不是两个List。
在Adapter1
类构造函数中,两个都是ArrayList:
private ArrayList<DataType> finalList;
public Adapter1(Context mContext,ArrayList list1,ArrayList list2){
finalList = new ArrayList<DataType>(list1);
finalList.addAll(list2);
}
现在使用finalList
适用于您使用getView
和getCount
的适配器的List
和List1
方法