在java和android中如何在arraylist中添加视图?

时间:2014-03-22 12:16:12

标签: java android

我正在尝试创建ArrayList个观看次数。不幸的是,ArrayList值会随着视图引用更改而更改。

我有两个引用,一个在偶数位置添加,另一个在奇数位置添加。我看到当第3项出现时的行为,第1项的值也发生变化,同样也适用于偶数。

我需要在某些基础上更新视图列表。怎么解决这个问题?我试过这样的事情,但没有奏效:

  private void addItemAt(int depth,View retView){

    ArrayList<View> viewsAdded = new ArrayList<View>(viewsAtDepths.size());

    for (int i = 0; i < viewsAtDepths.size(); i++) {
        View v = viewsAtDepths.get(i);
        viewsAdded.add(v);
    }

    viewsAtDepths = viewsAdded;

    if(depth < viewsAtDepths.size()){
        Log.d(TAG,"Replacement at depth "+depth);
        viewsAtDepths.set(depth, retView);
    }
    else{
        Log.d(TAG,"Added at depth "+depth);
        viewsAtDepths.add(retView);
    }

    Log.d(TAG,"ADD item is made "+depth +" and total nu of items are      "+viewsAtDepths.size());
    for (int i = 0; i < viewsAtDepths.size(); i++) {

        if(viewsAtDepths.get(i) instanceof ListView){
            ListView lv = (ListView) viewsAtDepths.get(i);
            if(lv.getItemAtPosition(i) instanceof Node){
                Node n = (Node) lv.getItemAtPosition(0);
                Log.d(TAG,"Title Name at "+i+" selected node is "+n.title);
            }
            else{
                if(lv.getItemAtPosition(i) instanceof String){
                    Log.d(TAG,"String Title Name at "+i+" selected node is "+lv.getItemAtPosition(i));
                }
            }
        }

    }

}

0 个答案:

没有答案