动态删除LinearLayout中动态添加的视图

时间:2012-09-03 10:27:47

标签: android listview scrollview android-linearlayout dynamic-data

帮助解决此类问题。我有一个包含很多项目的ScrollView。在它的底部我添加了一组来自另一个Activity的项目。我检查来自另一个Activity的输入ArrayList,并且应该使用ScrollView向我的Activity添加正确的复选计数。我添加这样的项目:

public void addFiles()
{

        LinearLayout layout = (LinearLayout) findViewById(R.id.filesList);

        if(!FileManagerActivity.finalAttachFiles.isEmpty())
        {   

            for (int i=0; i<FileManagerActivity.finalAttachFiles.size();i++)
            {   

               View line = new View(this);
               line.setLayoutParams(new LayoutParams(1, LayoutParams.MATCH_PARENT));
               line.setBackgroundColor(0xAA345556);
               informationView= new CheckBox(this);
               informationView.setTextColor(Color.BLACK);
               informationView.setTextSize(16);
               informationView.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
               informationView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.file_icon, 0, 0, 0);  
               informationView.setText(FileManagerActivity.finalAttachFiles.get(i).getName().toString());
               informationView.setId(i);
               layout.addView(informationView, 0);
               layout.addView(line, 1);
               layout.postInvalidate();
            }
        }

}

一切都添加得当。比我创建单独的ArrayList,应该保存复选框的可检查状态:

for(int b=0;b<FileManagerActivity.finalAttachFiles.size();b++){    
   checks.add(b,0);  
} 
btnClose.setOnClickListener(new View.OnClickListener() {

  public void onClick(View view) {
     moveTaskToBack(true);
  } 
});

其中FileManagerActivity.finalAttachFiles是来自另一个Activity的ArrayList。我保存了这样的可检查状态:

public void getChecked()
{  

    LinearLayout layout = (LinearLayout) findViewById(R.id.filesList);
    View v = null;
    for(int i=0; i< FileManagerActivity.finalAttachFiles.size(); i++) {

    v = layout.getChildAt(i);     
    CheckBox checkBox = (CheckBox) findViewById(i);
   boolean checked=checkBox.isChecked();
    if (checked)
    {  
     checks.set(i, 1);
    }
    else
    {
        checks.set(i, 0);
    }
  }
}

然后我想删除那些用Button检查过的项目。我这样做:

btnFilesRemove.setOnClickListener(new View.OnClickListener() {

public void onClick(View view) {

    LinearLayout layout = (LinearLayout) findViewById(R.id.filesList);
    getChecked();
    for (int i=0;i< checks.size();i++)
    {

    if (checks.get(i)==1)
    {  
        View v = layout.getChildAt(i);   
        ViewGroup parent = (ViewGroup) v.getParent();
        parent.removeView(v);
        layout.invalidate();
    }
    else
    {
        Log.i("wasn't checked",Integer.toString(checks.get(i)));
    }

  }
 }
});

但是当我这样做时,只能删除项目,而不是那些被检查的项目。当我再次尝试删除时,我得到了一个:

09-03 13:12:32.569: E/AndroidRuntime(1600): FATAL EXCEPTION: main
09-03 13:12:32.569: E/AndroidRuntime(1600): java.lang.NullPointerException
09-03 13:12:32.569: E/AndroidRuntime(1600):     at com.assignmentexpert.NewOrderActivity.getChecked(NewOrderActivity.java:307)
09-03 13:12:32.569: E/AndroidRuntime(1600):     at com.assignmentexpert.NewOrderActivity$4.onClick(NewOrderActivity.java:149)
09-03 13:12:32.569: E/AndroidRuntime(1600):     at android.view.View.performClick(View.java:2485)
09-03 13:12:32.569: E/AndroidRuntime(1600):     at android.view.View$PerformClick.run(View.java:9080)
09-03 13:12:32.569: E/AndroidRuntime(1600):     at android.os.Handler.handleCallback(Handler.java:587)
09-03 13:12:32.569: E/AndroidRuntime(1600):     at android.os.Handler.dispatchMessage(Handler.java:92)
09-03 13:12:32.569: E/AndroidRuntime(1600):     at android.os.Looper.loop(Looper.java:123)
09-03 13:12:32.569: E/AndroidRuntime(1600):     at android.app.ActivityThread.main(ActivityThread.java:3687)
09-03 13:12:32.569: E/AndroidRuntime(1600):     at java.lang.reflect.Method.invokeNative(Native Method)
09-03 13:12:32.569: E/AndroidRuntime(1600):     at java.lang.reflect.Method.invoke(Method.java:507)
09-03 13:12:32.569: E/AndroidRuntime(1600):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
09-03 13:12:32.569: E/AndroidRuntime(1600):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
09-03 13:12:32.569: E/AndroidRuntime(1600):     at dalvik.system.NativeStart.main(Native Method)

我试图通过使用ListView来实现这一点,但是由于ScrollView使用而导致可视化问题,但功能正常工作...请告诉我,如何以更好的方式实现:继续使用LinearLayout或ListView实现

1 个答案:

答案 0 :(得分:0)

在您创建的所有复选框上设置onCheckChangeListener

informationView.setTag(i);   
informationView.setOnCheckedChangeListener(new OnCheckedChangeListener{ 
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked){ 
        final Integer position=(Integer) buttonView.getTag(); 
        if(position!=null){  
            if (ischecked){  
                checks.set(i, 1);
            }else {
                checks.set(i, 0);
            }
        } 
    } 
});