清除适配器,然后添加项目,一个错误修复



我有一个ListView。我需要在条件(true)下在listView上添加项目。我已经尝试清除适配器,然后添加真正的项目。并试图删除虚假项目。但我好像错过了什么。帮我找到我缺少的东西。

这是的主要活动

protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.favorite_tab);
    list = (ListView) findViewById(R.id.listView1);
    mAdapter = new FavoriteListAdapter(this);
    list.setAdapter(mAdapter);
    getFavoriteChapt();
    list.setOnItemClickListener(this);
}
public void getFavoriteChapt()
{
    mAdapter.clear();
    for (int i = 0; i < 17; i++)
    {
        if (DataStore.getHierarchicalChapters().getSubChapters().get(i)
                .isFavorite() == true)
            mAdapter.add(DataStore.getHierarchicalChapters()
                    .getSubChapters().get(i));
    }
    mAdapter.notifyDataSetChanged();
}

和我的CustomAdapter

    @Override
public View getView(int position, View convertView, ViewGroup parent)
{
    View row = convertView;
    if (row == null)
    {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = inflater.inflate(R.layout.favorite_tab_list_view_row, parent,
                false);
        ViewHolder viewHolder = new ViewHolder();
        viewHolder.text_view_sura_name = (TextView) row
                .findViewById(R.id.searchChapterTextViewId);
        row.setTag(viewHolder);
    }
    ViewHolder viewHolder = (ViewHolder) row.getTag();
    viewHolder.text_view_sura_name.setText(Integer.toString(position)
            + DataStore.getHierarchicalChapters().getSubChapters()
                    .get(position).getTitle());
    return row;
}

这是我的geFavoriteChapters

private ArrayList<Chapter> getFavortiveChapters()
{
    ArrayList<Chapter> chapter = new ArrayList<Chapter>();
    for (int i = 0; i < 17; i++)
    {
        if (DataStore.getHierarchicalChapters().getSubChapters().get(i)
                .isFavorite())
            chapter.add(DataStore.getHierarchicalChapters()
                    .getSubChapters().get(i));          
    }
    return chapter;
}

和View

viewHolder.chapterName.setText(getFavoriveChapters().get(position).getTitle());

并且问题解决了:)

相关内容

最新更新