整数ArrayList不附加值但替换它们

时间:2016-12-06 03:52:14

标签: android arrays list listview arraylist

所以我有一个listview,当你点击一个单独的行时,它会启动一个新的活动。在新活动中,有一个复选框,在选中时会在最初单击的列表视图行旁边放置一个复选标记。我使用整数数组列表来存储列表视图行的位置,这些行应该在它们旁边有复选标记。

我正在尝试添加到ArrayList的值只是替换以前的值而不是将其附加到结尾。

这是我的适配器中的getView方法,我试图这样做

@Override
    public View getView(int position, View convertView, ViewGroup parent) {

        LayoutInflater routeInflater = LayoutInflater.from(getContext());
        View customView = convertView;
        if(customView == null){customView = routeInflater.inflate(R.layout.custom_row, parent, false);}

        CharSequence singleRoute = getItem(position);
        TextView routeText = (TextView) customView.findViewById(R.id.routeText);
        routeText.setText(singleRoute);

       ///check mark imageview///
        ImageView checkImageView = (ImageView) customView.findViewById(R.id.checkImageView);

    ///image id for checkmark
        int checkImageResourceId = ((Activity) getContext()).getIntent().getIntExtra("checkImageResource",0);

    ///listview item position added to list array    
    int listViewItemPosition = ((Activity) getContext()).getIntent().getIntExtra("listViewItemPosition",0);

        ////list where I want to store equivalent checked routes////////

        List<Integer> checkedRoutePosition = new ArrayList<Integer>();

        checkedRoutePosition.add(listViewItemPosition);

  for(int i=0; i<checkedRoutePosition.size(); i++)
    if(position  ==     checkedRoutePosition.get(i)) {
         checkImageView.setImageResource(checkImageResourceId);
       }
        return customView;

感谢您的任何意见!

2 个答案:

答案 0 :(得分:0)

正如Nic Robertson所说,为路线创建Bean类

public class Route{
int a,b;

public void set(int x,int y){
    a=x;
    b=y;
}

public boolean isChecked(){
    return a==b;
}
}

创建 ArrayList

基于arrayList

更新getView()

答案 1 :(得分:0)

替换你的代码:

if( each checkedRoutes position in array == each checkedRoutePosition in array){
    checkImageView.setImageResource(checkImageResourceId);
}

使用:

boolean state = false;
if(checkedRoutes.size() == checkedRoutePosition.size()) {
  state = true;
  for(int i=0; i<checkedRoutes.size(); i++)
    if(checkedRoutes.get(i) != checkedRoutePosition.get(i)) {
      state = false;
      break;
    }
}
if(state)
  checkImageView.setImageResource(checkImageResourceId);