开发者

Exception adding View in ViewFlipper

I have a ViewFlipper in which I want to add Views (Relative Layout with Children). I am trying to do this in an AsyncTask.

Here is the code I am using:

class LoadData extends AsyncTask<Object, Void, String>
{
    RelativeLayout rl_main;

    @Override
    protected void onPreExecute()
    {
        super.onPreExecute();   开发者_高级运维        
    }

    @Override
    protected String doInBackground(Object... parametros)
    {   
        Cursor cur_channel = db_Helper.sqlDB.query(DatabaseHelper.Channels_TableName, null, null, null, null, null, null);
        startManagingCursor(cur_channel);

        int index = 0;
        while(cur_channel.moveToNext())
        {

            LayoutInflater inflater = getLayoutInflater();
            rl_main = (RelativeLayout) inflater.inflate(R.layout.newslisting,null); 

            ListView lv_Listing = (ListView) rl_main.findViewById(R.id.id_lv_news_listing);
            LazyAdapter newsAdpater = new LazyAdapter(NewsListing.this, channel_id, db_Helper);
            lv_Listing.setAdapter(newsAdpater);
            lv_Listing.setDividerHeight(0);

            TextView tv_channelNumber = (TextView)rl_main.findViewById(R.id.id_tv_ChannelNumber);
            if(tv_channelNumber != null)
            {
                tv_channelNumber.setText("Some Text");
            }

            TextView tv_channelName = (TextView)rl_main.findViewById(R.id.id_tv_ChannelName);
            if(tv_channelName != null)
            {
                tv_channelName.setText("Some Text");
            }

            lv_Listing.setOnItemClickListener(new OnItemClickListener()
            {

                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
                {
                    // something on ListItem Click
                }
            });         

            publishProgress();
            index++;
        }       
        return null;
    }

    @Override
    protected void onProgressUpdate(Void... v) 
    {

        super.onProgressUpdate(v);
        try
        {
            viewFlipper.addView(rl_main);  // Here I get the exception, but not on all the views
        } 
        catch (Exception e)
        {
             // Exception
        }
    }

    @Override
    protected void onPostExecute(String result)
    {
        super.onPostExecute(result);
        progressDialog.dismiss();
    }
}

In protected void onProgressUpdate(Void... v), I get the exception when I add the view to ViewFlipper, but I don't see this Exception on every addition of View. The Exception is:

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. in viewflipper


You shoud probably use the overloaded version of LayoutInflater.inflate. In your code :

LayoutInflater inflater = getLayoutInflater();
rl_main = (RelativeLayout) inflater.inflate(R.layout.newslisting,null); 

Use this method instead : Android Developpers LayoutInflater.inflate

The boolean attachToRoot is for attach the inflated view to his root (parent) or not. Try to pass a false boolean like that :

rl_main = (RelativeLayout) inflater.inflate(R.layout.newslisting,null, false); 


Replacing AsyncTask with a Thread and Handler did the work. Its working perfect now. I don't know what was the problem with AsyncTask.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜