Java 在最后一个索引之后重复的Arraylist元素
我面临的问题是- 1.我有一个自定义网格视图。视图适配器仅为五个元素对项目进行充气,但在显示列表时,列表会在最后一个索引之后重复 例如,如果适配器中只有5个元素,当显示它时会变成10或15,然后当我单击任何项目时,它会显示outOfBoundException 2.单击子按钮时,应将项目数减少一个。有时候,事实并非如此。我想不出原因。“添加”按钮工作正常Java 在最后一个索引之后重复的Arraylist元素,java,android,android-arrayadapter,android-gridview,Java,Android,Android Arrayadapter,Android Gridview,我面临的问题是- 1.我有一个自定义网格视图。视图适配器仅为五个元素对项目进行充气,但在显示列表时,列表会在最后一个索引之后重复 例如,如果适配器中只有5个元素,当显示它时会变成10或15,然后当我单击任何项目时,它会显示outOfBoundException 2.单击子按钮时,应将项目数减少一个。有时候,事实并非如此。我想不出原因。“添加”按钮工作正常 public class MyAdaptertwo extends BaseAdapter { public static List<I
public class MyAdaptertwo extends BaseAdapter
{
public static List<Item> items = new ArrayList<Item>();
private LayoutInflater inflater;
int j;
int k=0;
public static int no[]=new int[5];
public static int co[]=new int[5];
public static int cos[]={1,2,3,4,5};
public static List<list> rowitems = new ArrayList<list>();
static int size=0;
public MyAdaptertwo(Context context)
{
inflater = LayoutInflater.from(context);
items.add(new Item("S", R.drawable.s,"1",R.drawable.v));
items.add(new Item("F", R.drawable.t,"2",R.drawable.v));
items.add(new Item("C",R.drawable.c,"3",R.drawable.n));
items.add(new Item("P", R.drawable.i,"4",R.drawable.v));
items.add(new Item("P", R.drawable.p,"5",R.drawable.n));
}
@Override
public int getCount() {
return items.size();
}
@Override
public Object getItem(int i)
{
j=i;
return items.get(i);
}
public void setCost(int i,String a)
{
items.get(i).cost=a;
}
@Override
public long getItemId(int i)
{
return items.get(i).colorId;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup)
{
View v = view;
ImageView picture;
ImageView vn;
TextView name;
TextView costs;
if(v == null)
{
v = inflater.inflate(R.layout.gridview_item, viewGroup, false);
v.setTag(R.id.picture, v.findViewById(R.id.picture));
v.setTag(R.id.vnon, v.findViewById(R.id.vnon));
v.setTag(R.id.text, v.findViewById(R.id.text));
v.setTag(R.id.cost,v.findViewById(R.id.cost));
}
picture = (ImageView)v.getTag(R.id.picture);
vn=(ImageView)v.getTag(R.id.vnon);
name = (TextView)v.getTag(R.id.text);
costs=(TextView)v.getTag(R.id.cost);
Item item = (Item)getItem(i);
picture.setImageResource(item.colorId);
name.setText(item.name);
costs.setText(item.cost);
vn.setImageResource(item.vnId);
Button add=(Button)v.findViewById(R.id.add);
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (size == 0) {
Log.e("first j-1",Integer.toString(j-1));
no[j - 1] = no[j - 1] + 1;
Log.e("first no[j-1]",Integer.toString(no[j-1]));
list item = new list(items.get(j - 1).colorId, items.get(j - 1).name, Integer.toString(co[j-1]), Integer.toString(no[j - 1]));
rowitems.add(item);
Log.e("n", Integer.toString(no[j - 1]));
size++;
Log.e("first size",Integer.toString(size));
}
else {
int y = 0, pos = 0;
for (int i = 0; i < size; i++) {
if ((rowitems.get(i).title).equals((items.get(j - 1).name))) {
y = 1;
pos = i;
}
}
if (y == 1) {
Log.e("else j-1",Integer.toString(j-1));
no[j - 1] = no[j - 1] + 1;
Log.e("else no[j-1]",Integer.toString(no[j-1]));
rowitems.get(pos).no =Integer.toString(1+Integer.parseInt(rowitems.get(pos).no));
String s= rowitems.get(pos).cost;
int c=Integer.parseInt(s);
c=c+cos[j-1];
Log.e("C", Integer.toString(c));
//co[j-1]=c;
rowitems.get(pos).cost=Integer.toString(c);
Log.e("else co[j-1]", Integer.toString(co[j - 1]));
Log.e("else c",Integer.toString(c));
Log.e("else size",Integer.toString(size));
}
else {
Log.e("else else j-1",Integer.toString(j-1));
no[j - 1] = no[j - 1] + 1;
Log.e("else else no[j-1]",Integer.toString(no[j-1]));
list item = new list(items.get(j - 1).colorId, items.get(j - 1).name, Integer.toString(co[j-1]), Integer.toString(no[j - 1]));
rowitems.add(item);
/* String s=items.get(j-1).cost.toString();
int c=Integer.parseInt(s);
c=c+(c/no[j-1]);
setCost(j-1,Integer.toString(c));
MyAdaptertwo.this.notifyDataSetChanged();*/
size++;
Log.e("else else size",Integer.toString(size));
}
}
}
});
Button sub=(Button)v.findViewById(R.id.sub);
sub.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (size > 0) {
Log.e("if size ",Integer.toString(size));
int y = 0, pos = 0;
for (int i = 0; i < size; i++) {
Log.e("if i ",Integer.toString(i));
Log.e("if j-1 ",Integer.toString(j-1));
if ((rowitems.get(i).title).equals((items.get(j - 1).name))) {
y = 1;
pos = i;
Log.e("title",rowitems.get(i).title);
Log.e("name",items.get(j-1).name);
Log.e("if pos ",Integer.toString(pos));
}
}
if (y == 1) {
no[j - 1] = no[j - 1] - 1;
Log.e("y==1 no[j-1] ",Integer.toString(no[j-1]));
rowitems.get(pos).no = Integer.toString(Integer.parseInt(rowitems.get(pos).no)-1);
Log.e("row no", rowitems.get(pos).no);
String s= rowitems.get(pos).cost;
int c=Integer.parseInt(s);
c=c-cos[j-1];
Log.e("C", Integer.toString(c));
//co[j-1]=c;
rowitems.get(pos).cost=Integer.toString(c);
Log.e("else co[j-1]", Integer.toString(co[j - 1]));
Log.e("else c",Integer.toString(c));
Log.e("else size",Integer.toString(size));
if (no[j - 1] <= 0) {
if (size == 1) {
size = 0;
rowitems.clear();
} else if (size > 1) {
rowitems.remove(pos);
}
}
}
}
}
});
return v;
}
private class Item
{
final String name;
final int colorId;
String cost;
final int vnId;
Item(String name, int drawableId,String cost,int vnId)
{
this.name = name;
this.colorId = drawableId;
this.cost=cost;
this.vnId=vnId;
}
}
您每次都在
项中添加值。在向其添加项目之前清除它
items.clear();
关于您的边界外异常
当您运行项目3次时,项目将有15个值,但您的no
、co
和cos
将没有15个值
如果静态列表不需要删除它,它将解决您的所有问题。简单地说
List<Item> items = new ArrayList<Item>();
List items=new ArrayList();
在声明数组列表时,您应该删除静态
,并且我建议您使用私有
而不是公共
public static List<Item> items = new ArrayList<Item>();
publicstaticlist items=newarraylist();
到
private List items=new ArrayList();
原因是:静态变量属于该类,而不是该类的任何特定对象。它不会释放,因此每次创建适配器时,列表将不断增加。这对性能非常不利
希望这有助于在数组中添加新值之前清除数组
arrayName.clear() 在添加到项之前,请在myadapterWO()
的构造函数中尝试items.clear()
。@RRR这有什么帮助?请先尝试,您正在将项添加到数组EVRYTIME。列表没有重复,但如果我单击最后一个项,它将引用最后第二个项。对于其他人来说,一切都很好@rrr列表没有重复,但如果我单击最后一项,它指的是最后第二项。对于其他人来说,一切都很好@rrr列表没有重复,但如果我单击最后一项,它指的是最后第二项。当我点击倒数第二项时,它指的是倒数第三项。其余的都很好,在我上传的代码中。按钮名为add和sub。您是否在按钮onClick中编写了一些日志。我在你的代码中看到它没有logcat?请写一些日志,然后给我看日志请在你的onClick
:log.e(“TAG”,“click”+i)中写这个日志代码>@TeeKay很抱歉反应太晚,如果您认为我的答案解决了您的问题,请将其标记为正确答案;)非常感谢。
List<Item> items = new ArrayList<Item>();
public static List<Item> items = new ArrayList<Item>();
private List<Item> items = new ArrayList<Item>();