gpt4 book ai didi

java - 数据库获取时 OnClick() 内出现 NullPointerException

转载 作者:行者123 更新时间:2023-12-01 15:28:31 26 4
gpt4 key购买 nike

在尝试刷新我的 View 中的数据时,正在努力解决我的自定义 simplecursoradapter 中的 NullPointerException 问题。使用 SimpleCursorAdapter 时,您无法使用 NotifyDataSetChanged(),因此我需要创建一个新适配器,并且在传递所需数据时遇到困难。

public class DxSimpleCursorAdapter extends SimpleCursorAdapter {
Context context;
Activity activity;
DxDbAdapter dbh;
DxSimpleCursorAdapter adapter;
ListView lv;

protected String subcategory;

public DxSimpleCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to, String param) {
super(context, layout, c, from, to);
this.context=context;
this.activity=(Activity) context;
subcategory = param;
}

public DxSimpleCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
super(context, layout, c, from, to);
this.context=context;
this.activity=(Activity) context;
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(context);
View row = inflater.inflate(R.layout.list_detail, null);
ViewHolder holder = new ViewHolder();
holder.image = (ImageView) row.findViewById(R.id.fav);
holder.diagnosis = (TextView) row.findViewById(R.id.diagnosis);
holder.code = (TextView) row.findViewById(R.id.code);
row.setTag(holder);
return row;
}

class ViewHolder {
ImageView image;
TextView diagnosis;
TextView code;
}

class Status {
int status;
Long id;
}

@Override
public void bindView(View v, Context con, Cursor cursor) {
ViewHolder holder = (ViewHolder) v.getTag();

int favstatus = cursor.getInt(cursor.getColumnIndex(DxDbAdapter.FAV));
Status state = new Status();

if (favstatus == 1) {
holder.image.setImageResource(R.drawable.btn_star_on_normal);
state.status = 1;
}
else if (favstatus == 0) {
holder.image.setImageResource(R.drawable.btn_star_off_normal);
state.status = 0;
}

long id = cursor.getLong(cursor.getColumnIndex(DxDbAdapter.DIAG_ID));
state.id = id;
holder.image.setTag(state);
holder.diagnosis.setText(cursor.getString(cursor.getColumnIndex(DxDbAdapter.DIAG)));
holder.code.setText(cursor.getString(cursor.getColumnIndex(DxDbAdapter.DIAG_CODE)));

holder.image.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
Status current_state = (Status) v.getTag();

ImageView fav = (ImageView) v.findViewById(R.id.fav);
fav.setImageResource(R.drawable.ic_fav);

if (current_state.status == 1) {
Toast toast = Toast.makeText(context,"Update fav status to 1",Toast.LENGTH_SHORT);
toast.show();
}
else if (current_state.status == 0) {
Toast toast = Toast.makeText(context,"Update fav status to 0",Toast.LENGTH_SHORT);
toast.show();
}

dbh = new DxDbAdapter(context);
dbh.open();
Cursor newCursor = dbh.fetch(1, subcategory);
String[] columns = new String[] {"diagnosis", "diagcode"};
int[] to = new int[] {R.id.diagnosis, R.id.code};
adapter = new DxSimpleCursorAdapter(context, R.layout.list_detail, newCursor, columns, to);
lv.setAdapter(adapter);
}
});
String diag = cursor.getString(cursor.getColumnIndex(DxDbAdapter.DIAG));
String code = cursor.getString(cursor.getColumnIndex(DxDbAdapter.DIAG_CODE));
holder.diagnosis.setText(diag);
holder.code.setText(code);
}

问题在于 subcategory = getIntent().getStringExtra("SUBCATEGORY");Cursor newCursor = dbh.fetch(1, subcategory); 并且我必须引用正在使用适配器的 Activity 。我想我在引用 Activity 的正确语法上遇到了问题,这样我就不会得到 NullPointerException。

最佳答案

您确实应该从父 Activity 中传递子类别,因为在使用适配器时该值不会改变(而不是每次绑定(bind)到行时都检索它)

例如:从父 Activity 中,您会执行类似的操作

String subcategory = getIntent().getStringExtra("SUBCATEGORY"); 
DxSimpleCursorAdapter adapter = DxSimpleCursorAdapter(context, layout, c, from, to, subcategory);
listView.setAdapter(adapter);

那么显然您需要修改适配器的构造函数,以便它获取该字符串,然后将其分配给适配器内的子类别变量

关于java - 数据库获取时 OnClick() 内出现 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9877085/

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