Question

I am try to remove the item from list.For list i am just inflating views.I am not using listview.

this is my code for inflating view :

private void addItem(){
   for (int i = 0; i < arrayList.size(); i++) {

        View myBooksListView = new View(StoriesActivity.this.getActivity());
        myBooksListView = layoutInflater.inflate(
                getResources().getLayout(R.layout.download_items), null);

        mChildLayout = (FrameLayout) myBooksListView
                .findViewById(R.id.download_book_layout);
        mChildLayout.setTag(i);
        book_image = (ImageView) myBooksListView
                .findViewById(R.id.download_image);
        progressBar = (ProgressBar) myBooksListView
                .findViewById(R.id.download_progressBar1);
        mPercentTextView = (TextView) myBooksListView
                .findViewById(R.id.percent_textView);



        System.out.println("progressbar id : " + progressBar);

        book_image.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Toast.makeText(getActivity(), "image clicked",
                        Toast.LENGTH_LONG).show();
            }
        });

                mParenLayout.addView(myBooksListView);          
    }
}

I am trying to download file from server and if it's file it's goto catch block in side of catch i am removing the item from array list and removing that entire row.

this is my code for removing the item from list :

  for (int i = 0; i < downloadingList.size(); i++) {
                String s = downloadingList.get(i).getQueFile();
                System.out.println("s-----------   "+ s);
                view1.getTag();
                String ss = view1.getTag().toString();
                System.out.println("-----------" + ss);
                FrameLayout layout = (FrameLayout) view1;
                layout.getTag();

                String sss = layout.getTag().toString();

                System.out.println("-----------" + sss);
                if (s.equals(st)) {

                    downloadingList.remove(i);
                    mParentLayout.removeAllViews();
                    int size = downloadingList.size();

                    if (size > 0) {
                        addDownloadItem();
                    }
                }
            }

In that above code Frame Layout is my inflaterLayout.And mParentLayout is the my LinerLayout it's inside of scrollview and i am adding the views into this layout.

Finally my problem was when i am trying mParentLayout.removeAllViews(); i am getting exception

Here is my Logcat :

  02-13 11:36:17.808: E/AndroidRuntime(4686): FATAL EXCEPTION: AsyncTask #4
  02-13 11:36:17.808: E/AndroidRuntime(4686): java.lang.RuntimeException: An error occured while   executing doInBackground()
02-13 11:36:17.808: E/AndroidRuntime(4686):     at   android.os.AsyncTask$3.done(AsyncTask.java:200)
02-13 11:36:17.808: E/AndroidRuntime(4686):     at   java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
  02-13 11:36:17.808: E/AndroidRuntime(4686):   at jav a.util.concurrent.FutureTask.setException(FutureTask.java:124)
  02-13 11:36:17.808: E/AndroidRuntime(4686):   at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
  02-13 11:36:17.808: E/AndroidRuntime(4686):   at java.util.concurrent.FutureTask.run(FutureTask.java:137)
  02-13 11:36:17.808: E/AndroidRuntime(4686):   at  java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
 02-13 11:36:17.808: E/AndroidRuntime(4686):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
 02-13 11:36:17.808: E/AndroidRuntime(4686):    at java.lang.Thread.run(Thread.java:1096)
      02-13 11:36:17.808: E/AndroidRuntime(4686): Caused by:     android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
   02-13 11:36:17.808: E/AndroidRuntime(4686):  at android.view.ViewRoot.checkThread(ViewRoot.java:2826)
  02-13 11:36:17.808: E/AndroidRuntime(4686):   at android.view.ViewRoot.requestLayout(ViewRoot.java:594)
 02-13 11:36:17.808: E/AndroidRuntime(4686):    at android.view.View.requestLayout(View.java:8127)
 02-13 11:36:17.808: E/AndroidRuntime(4686):    at android.view.View.requestLayout(View.java:8127)
02-13 11:36:17.808: E/AndroidRuntime(4686):     at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:254)
 02-13 11:36:17.808: E/AndroidRuntime(4686):    at    android.view.View.requestLayout(View.java:8127)
02-13 11:36:17.808: E/AndroidRuntime(4686):     at android.view.View.requestLayout(View.java:8127)
02-13 11:36:17.808: E/AndroidRuntime(4686):     at android.view.View.requestLayout(View.java:8127)
02-13 11:36:17.808: E/AndroidRuntime(4686):     at android.view.View.requestLayout(View.java:8127)
02-13 11:36:17.808: E/AndroidRuntime(4686):     at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:254)
02-13 11:36:17.808: E/AndroidRuntime(4686):     at android.view.View.requestLayout(View.java:8127)
02-13 11:36:17.808: E/AndroidRuntime(4686):     at android.widget.ScrollView.requestLayout(ScrollView.java:1200)
02-13 11:36:17.808: E/AndroidRuntime(4686):     at android.view.View.requestLayout(View.java:8127)
02-13 11:36:17.808: E/AndroidRuntime(4686):     at android.view.View.requestLayout(View.java:8127)
02-13 11:36:17.808: E/AndroidRuntime(4686):     at android.view.ViewGroup.removeAllViews(ViewGroup.java:2255)
02-13 11:36:17.808: E/AndroidRuntime(4686):     at info.storytime.activity.StoriesActivity$DownloadFileFromURL.doInBackground(StoriesActivity.java:891)
02-13 11:36:17.808: E/AndroidRuntime(4686):     at info.storytime.activity.StoriesActivity$DownloadFileFromURL.doInBackground(StoriesActivity.java:1)
02-13 11:36:17.808: E/AndroidRuntime(4686):     at android.os.AsyncTask$2.call(AsyncTask.java:185)
02-13 11:36:17.808: E/AndroidRuntime(4686):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
 02-13 11:36:17.808: E/AndroidRuntime(4686):    ... 4 more

How can i remove my items from arrayList and remove that entire row from that list?

Thanks....

Was it helpful?

Solution 2

02-13 11:36:17.808: E/AndroidRuntime(4686): Caused by: android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.`

as you are You are removing views mParentLayout.removeAllViews() in AsyncTask's doInBackground() method. As in android you can not manipulate UI in doInBackground() method directly.

you can manipulate(add, delete or set visibility of UI controls) UI in onPostExecute() or onPreEcecute() methods. As you are removing layout childs based on downloaded list.

Remove that controls on onPostExecute() method of your AsyncTask. You can also remove controls onProgressUpdate()

if still there is requirement to manipulate UI in doInBackground() mthos then you must go with runOnUiThread()

Sample code :

ActivityName.this.runOnUiThread(new Runnable() {
    public void run() {

    // Manipulate UI here

    }
});

OTHER TIPS

The exception is not because of Removing elements from ArrayList because of this line..

mParentLayout.removeAllViews();

You are removing views in the AsyncTask which runs in a another thread..View Updation only done in the UI thread.so remove it and update in the onPostExecute() or onPreEcecute() methods..

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top