gpt4 book ai didi

android - 将具有不同布局的项目动态添加到 ListView

转载 作者:行者123 更新时间:2023-11-30 02:26:28 24 4
gpt4 key购买 nike

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

我的代码大致如下:

fragment :

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

最佳答案

您忘记了newView 方法中switchbreak 语句。

关于android - 将具有不同布局的项目动态添加到 ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27870916/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com