0

Help with such an issue. I have a ScrollView which contains a lot of items. In the bottom of it I add a set of items from another Activity. I check the input ArrayList from another Activity and should to add proper count of checkbox to my Activity with ScrollView. I add items like that:

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();
            }
        }

}

Everything adds properly. Than I create separate ArrayList with should save the checkable state of the checkboxes:

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);
  } 
});

where FileManagerActivity.finalAttachFiles is the ArrayList from another Activity. I save the checkable state like this:

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);
    }
  }
}

Then I want to delete those items which were checked with the Button. I do it like that:

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)));
    }

  }
 }
});

But when I do it only item can be deleted and not those which was checked. And when I try to delete one more time I get an :

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)

I tried to implement this by using the ListView, but it cause problems with visualization in reason of ScrollView using, but functional worked properly...Tell me please, how can I implement in better way: continue with LinearLayout or ListView implementing.

Haresh Chaudhary
  • 4,390
  • 1
  • 34
  • 57
Rikki Tikki Tavi
  • 3,089
  • 5
  • 43
  • 81

1 Answers1

0

Set onCheckChangeListener on all check box that you have created

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);
            }
        } 
    } 
});
Dipak Keshariya
  • 22,193
  • 18
  • 76
  • 128
Jaiprakash Soni
  • 4,100
  • 5
  • 36
  • 67