getView()convertView仅在第一次调用getView时为null

时间:2014-09-17 08:19:15

标签: android

我得到一种非常奇怪的行为......

第一次调用getView()时,convertView为null并且没有问题 但在第二次它回来时不是空的。 我无法通过这种方式获得现金,而且名单表现不佳......

这个适配器的另一个问题是它每次调用它的getView()方法大约20次,我检查了并且listView的宽度和高度设置为match_parent。

这是代码......

public class LibraryAdapterArray extends BaseAdapter
{

    private Context                 _context;
    private ArrayList<StoryData>    _list;



    public LibraryAdapterArray(Context context)
    {
        _context = context;
        _list = new ArrayList<StoryData>();
    }

    @Override
    public int getCount()
    {

        return _list.size();
    }

    @Override
    public Object getItem(int position)
    {
        // TODO Auto-generated method stub
        return _list.get(position);
    }

    @Override
    public long getItemId(int position)
    {
        // TODO Auto-generated method stub
        return position;
    }

    public void setData(ArrayList<StoryData> data, int position)
    {

        Log.i("SET DATA","CALLED");
        if (position >= 1)
        {
            _list.addAll(_list.size(), data);
        }
        else
        {
            _list.clear();
            _list.addAll(data);
        }

        AppController.getInstance().setLibraryListData(_list, position);
        notifyDataSetChanged();

    }

    static class ViewHolder
    {
        StoryItem   item;
    }

    @Override
    public View getView(int arg0, View convertView, ViewGroup arg2)
    {
        ViewHolder view;
        Log.i("GetView Library","CALLED");
        final StoryData item = (StoryData) getItem(arg0);


            if (convertView == null)
            {
                convertView = LayoutInflater.from(_context).inflate(R.layout.expandable_list_item_2, null);
                view = new ViewHolder();
                view.item = ((StoryItem) convertView);
                view.item.initView();

                convertView.setTag(view);
            }
            else
            {
                view = (ViewHolder) convertView.getTag();
            }

            view.item.setUserId(item.getStory_creator_user_id());
            LibrarySetData(view.item, item);


        return convertView;
    }

    private void LibrarySetData(final StoryItem sItem, final StoryData item)
    {

        sItem.getProfileExtendImage().setRemoteURI(item.getCreator_image_url());
        sItem.getProfileExtendImage().setDefualImageRes(R.drawable.empty_profile);
        sItem.getProfileExtendImage().loadImage();

        sItem.getProfileImage().setRemoteURI(item.getCreator_image_url());
        sItem.getProfileImage().setDefualImageRes(R.drawable.empty_profile);
        sItem.getProfileImage().loadImage();

        if (item.getIsOpen() == true)
        {
            sItem.setOpened(true);
        }
        else
        {
            sItem.setOpened(false);
        }

        sItem.getProfileImage().setLoadingListener(new ImageLoadingListener()
        {

            @Override
            public void onFinishLoadingImage()
            {
                if (TextUtils.isEmpty(item.getCreator_image_url()) == true)
                {
                    sItem.getProfileImage().setImageResource(R.drawable.empty_profile);
                }

            }

            @Override
            public void onFailedLoading()
            {
                // TODO Auto-generated method stub

            }

        });

        sItem.getProfileExtendImage().setLoadingListener(new ImageLoadingListener()
        {

            @Override
            public void onFinishLoadingImage()
            {
                if (TextUtils.isEmpty(item.getCreator_image_url()) == true)
                {
                    sItem.getProfileExtendImage().setImageResource(R.drawable.empty_profile);
                }

            }

            @Override
            public void onFailedLoading()
            {
                // TODO Auto-generated method stub

            }

        });

        String tit = item.getStory_title();// StoryDropsUtils.getStoryTitle(item.getStory_title());
        sItem.getItemTitle().setText(tit);
        sItem.getExpandTitleTv().setText(tit);

        sItem.getStoriesParicipates().setText(item.getStory_cur_round_writers_count() + "/" + item.getStory_writers_per_round());
        sItem.getStoriesSyncUsers().setText(item.getStory_cur_round() + "/" + item.getStory_rounds());

        sItem.getItemAuthTitle().setText("By: " + item.getCreator_fullname());
        sItem.getExpandAuthTv().setText("By: " + item.getCreator_fullname());
        sItem.getTeaserTv().setText(item.getStory_teaser());

        if (item.getStory_is_private().equals("1") == true)
        {
            sItem.getPrivateImage().setVisibility(View.VISIBLE);
            sItem.getPrivateImageExpand().setVisibility(View.VISIBLE);
        }

        setImage(sItem.getStoryTypeImage(), item);

        sItem.setOnItemChoosenListener(new OnItemChoosenListener()
        {

            @Override
            public void onItemClose(Object obj)
            {

                sItem.setOpened(false);
                item.setOpened(false);

            }

            @Override
            public void onItemChoose(Object obj)
            {

                sItem.setOpened(true);
                item.setOpened(true);
            }

            @Override
            public void onItemLongClick(Object obj)
            {
                // TODO Auto-generated method stub

            }
        });

        sItem.getReadBtn().setOnClickListener(new OnClickListener()
        {

            @Override
            public void onClick(View v)
            {
                AppController.getInstance().setFirstTimeLibrary(true);
                ((MainActivity) _context).openReadStoryFragment(item.getStory_id());
            }
        });
    }

    private void setImage(final ImageView image, final StoryData item)
    {
        int storyType = Integer.parseInt(item.getStory_type());
        int id = R.drawable.list_story_icon;
        switch (storyType)
        {
            case MainActivity.STORY_TYPE__STORY:
            {
                id = R.drawable.list_story_icon;
                break;
            }
            case MainActivity.STORY_TYPE__CRAZY_TALE:
            {
                id = R.drawable.list_crazy_item;
                break;
            }
            case MainActivity.STORY_TYPE__JOURNAL:
            {
                id = R.drawable.list_journal_icon;
                break;
            }
            case MainActivity.STORY_TYPE__POEM:
            {
                id = R.drawable.list_poem_icon;
                break;
            }
            default:
            {
                break;
            }
        }
        image.setImageResource(id);
    }



}

我已经持续了几个小时,任何帮助都将是一个巨大的帮助

由于

2 个答案:

答案 0 :(得分:0)

移动

convertView.setTag(view); 

之后
view.item.setUserId(item.getStory_creator_user_id());

答案 1 :(得分:0)

这是一个小问题...... ListView是LinearLayout的孩子。 我把它改成了RelativeLayout,问题就解决了。