gpt4 book ai didi

android - 使用游标适配器和加载器时动态编辑 ListView 项

转载 作者:太空狗 更新时间:2023-10-29 13:23:33 25 4
gpt4 key购买 nike

我正在编写一个小型消息传递应用程序,我最近遇到了这个问题。

我有一个 ListFragment 子类,它实现了一个 CursorLoader 回调。内置的 ListView 绑定(bind)到 SimpleCursorAdapter。此列表中的每个行 View 都有一个级别可绘制作为背景。我想根据数据库信息(消息是否已发送)在级别之间切换。

我的问题是在这种情况下如何更改单行的背景?我必须通过适配器访问它吗?我很确定这应该在 onLoadFinished 方法中完成。但是这段代码给了我 NullPointerException。如果您能指出正确的方向,我将不胜感激。

public class MessagesFragment extends ListFragment implements 
LoaderManager.LoaderCallbacks<Cursor>{

private static MessagesFragment fragment;
private static MainActivity activity;
private static SimpleCursorAdapter adapter;

public static MessagesFragment getInstance() {
if (fragment == null)
fragment = new MessagesFragment();

return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

activity = (MainActivity) getActivity();

adapter = new SimpleCursorAdapter(activity, R.layout.list_item, null,
new String[] {DataProvider.KEY_NUMBER, DataProvider.KEY_MESSAGE, DataProvider.KEY_TIME},
new int [] {R.id.item_field_number, R.id.item_field_message, R.id.item_field_time}, 0);
setListAdapter(adapter);

getLoaderManager().initLoader(0, null, this);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_messages, null);
}

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);

activity.showDetails(Long.valueOf(id).intValue());
}

@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
return new CursorLoader(activity, DataProvider.CONTENT_URI, null, null, null, null);
}

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
adapter.swapCursor(data);

while (data.moveToNext()) {
int id = data.getInt(data.getColumnIndex(DataProvider.KEY_ID));
int sent = data.getInt(data.getColumnIndex(DataProvider.KEY_SENT));

this.getListView().getChildAt(id).getBackground().setLevel(sent);
}
}

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

最佳答案

这应该有帮助!

public class SampleAdapter extends SimpleCursorAdapter {

private LayoutInflater inflater;
private Cursor cursor;
private Context context;

public SampleAdapter(Context context, int layout, Cursor c, String[] from, int[] to, int flags) {
super(context, layout, c, from, to, flags);
this.context = context;
cursor = c;
inflater = LayoutInflater.from(context);
}

@Override
public int getCount() {
return cursor == null ? 0 : cursor.getCount();
}

@Override
public Object getItem(int position) {
return null;
}

@Override
public long getItemId(int position) {
return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

if (cursor != null) {
ViewHolder holder;
cursor.moveToPosition(position);

if (convertView == null) {
convertView = inflater.inflate(R.layout.list_item, parent, false);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
convertView.getBackground().setLevel(cursor.getInt(cursor.getColumnIndex(DataProvider.KEY_SENT)));
holder.number.setText(cursor.getString(cursor.getColumnIndex(DataProvider.KEY_NUMBER)));
holder.message.setText(cursor.getColumnIndex(DataProvider.KEY_MESSAGE)));
holder.time.setText(cursor.getColumnIndex(DataProvider.KEY_TIME)));
}

return convertView;
}

@Override
public Cursor swapCursor(Cursor c) {
cursor = c;
return super.swapCursor(c);
}

private class ViewHolder {

TextView number;
TextView message;
TextView time;

public ViewHolder(View view) {
number = (TextView) view.findViewById(R.id.item_field_number);
message = (TextView) view.findViewById(R.id.item_field_message);
time = (TextView) view.findViewById(R.id.item_countries_time);
}
}
}

关于android - 使用游标适配器和加载器时动态编辑 ListView 项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24956124/

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