将具有不同布局的项目动态添加到listview

时间:2015-01-09 23:24:27

标签: android android-listview

我有一个带有自定义CursorAdapter和2种布局类型的列表视图。在onCreate中,我只使用一种布局类型使用本地数据库中的数据填充列表视图。然后我从互联网上下载一些数据,并在列表视图的TOP中添加一个新行(使用以下方法:How to insert extra elements into a SimpleCursorAdapter or Cursor for a Spinner?),并使用不同的布局。我假设适配器重用旧布局,因为新布局不会膨胀,并且在bindView中填充行时应用程序崩溃并出现NullPointerException(我没有使用viewholder作为第二种布局类型,因为只有一行它)。我怎么解决这个问题?我是否可以强制使用适配器来重新填充"某种意见?

以下是我的代码大致如下:

碎片:

public class MyFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor>{

    private ListView mListView;
    private CustomCursorAdapter cursorAdapter;

    public MyFragment() {
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        getLoaderManager().initLoader(0, null, this);
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        mListView = (ListView)inflater.inflate(R.layout.fragment, container, false);

        cursorAdapter = new CustomCursorAdapter(getActivity(), null, 0);
        mListView.setAdapter(cursorAdapter);

        return mListView;
    }

    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        return new CursorLoader(
                getActivity(),
                CONTENT_URI,
                projection,
                selection,
                selectionArgs,
                sortOrder
        );
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
        cursorAdapter.swapCursor(data);
        new SomeTask().execute(/*params*/);
    }

    @Override
    public void onLoaderReset(Loader<Cursor> loader) {
        cursorAdapter.swapCursor(null);
    }

    private class SomeTask extends AsyncTask<String, Void, String>{

        @Override
        protected String doInBackground(String... params) {
            //Download some stuff
        }

        @Override
        protected void onPostExecute(String s) {
            //Add downloaded stuff to cursor
            cursorAdapter.swapCursor(extendedCursor);
        }
    }
}

适配器:

public class CustomCursorAdapter extends CursorAdapter {

    private static final int VIEW_TYPE_COUNT = 2;
    private static final int VIEW_TYPE_ONE = 0;
    private static final int VIEW_TYPE_TWO = 1;

    public CustomCursorAdapter(Context context, Cursor c, int flags) {
        super(context, c, flags);
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        int viewType = getItemViewType(cursor.getPosition());
        View view;
        switch (viewType) {
            case VIEW_TYPE_ONE: {
                view = LayoutInflater.from(context).inflate(R.layout.layout_one, parent, false);

                ViewHolder viewHolder = new ViewHolder(view);
                view.setTag(viewHolder);
            }
            case VIEW_TYPE_TWO: {
                view = LayoutInflater.from(context).inflate(R.layout.layout_two, parent, false);
            }
        }
        return view;
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        int viewType = getItemViewType(cursor.getPosition());
        switch (viewType){
            case VIEW_TYPE_ONE:
                // populate view using the viewholder
                break;
            case VIEW_TYPE_TWO:
                // populate newly added row, ((TextView)findViewById).setText() throws NullPointerException
                break;
        }
    }

    @Override
    public int getItemViewType(int position) {
        return /*is the new view added*/ && position == 0 ? VIEW_TYPE_TWO : VIEW_TYPE_ONE;
    }

    @Override
    public int getViewTypeCount() {
        return VIEW_TYPE_COUNT;
    }

    public static class ViewHolder {

        //cache views here

        public ViewHolder(View view) {
        }
    }
}

2 个答案:

答案 0 :(得分:1)

您在break方法中忘记了switch的{​​{1}}语句。

答案 1 :(得分:0)

一种方法是使用包含两个视图的所有元素的单个视图,并且对于每个元素,使用.setEnabled(boolean)和/或.setVisibility(int)来控制显示哪些元素。