如何从arraylist中删除特定项目?

时间:2014-02-13 06:59:01

标签: android layout-inflater removechild

我尝试从列表中删除。对于列表我只是在膨胀视图。我没有使用listview。

这是我的膨胀视图代码:

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

我正在尝试从服务器下载文件,如果它是文件,它在catch的一侧 goto catch block 我将从数组列表中删除该项并删除整行。

这是我从列表中删除项目的代码:

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

在上面的代码中,Frame Layout是我的inflaterLayout。而mParentLayout是我的LinerLayout,它位于scrollview中,我将视图添加到此布局中。

最后我的问题是当我尝试 mParentLayout.removeAllViews(); 我得到例外时

这是我的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

如何从arrayList中删除我的项目并从该列表中删除整行?

...谢谢

2 个答案:

答案 0 :(得分:4)

异常不是因为从ArrayList删除元素 因为这条线..

mParentLayout.removeAllViews();

您正在删除在另一个线程中运行的AsyncTask中的视图。View仅在UI线程中完成更新。删除它并在onPostExecute()或{{更新1}}方法..

答案 1 :(得分:3)

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.`

您正在使用AsyncTask的mParentLayout.removeAllViews()方法删除视图doInBackground()。在android中你不能直接在doInBackground()方法中操作UI。

您可以在onPostExecute()onPreEcecute()方法中操作(添加,删除或设置UI控件的可见性)UI。在删除基于下载列表的布局子项时。

删除onPostExecute() AsyncTask方法的控件。您还可以删除控件onProgressUpdate()

如果仍需要在doInBackground() mthos中操作用户界面,则必须使用runOnUiThread()

示例代码:

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

    // Manipulate UI here

    }
});