Java 如何在CursorAdapter中获取我的当前ID?
我有一个ListView,它由游标适配器填充。ListView中的每个项目都有一个微调器、一个按钮和一个复选框。当用户在微调器中选择某些内容、勾选复选框或按下按钮时,我需要更新数据库中的值。问题是,我不知道如何访问数据库条目的ID来更新它。每次更改任何列表项中的某些内容时,它都会更新最后一个列表项的值 我尝试对我的三个视图使用setTag()和getTag(),但在侦听器中调用getTag()时,我得到一个空值。如何获取当前列表项的IDJava 如何在CursorAdapter中获取我的当前ID?,java,android,listview,android-cursoradapter,Java,Android,Listview,Android Cursoradapter,我有一个ListView,它由游标适配器填充。ListView中的每个项目都有一个微调器、一个按钮和一个复选框。当用户在微调器中选择某些内容、勾选复选框或按下按钮时,我需要更新数据库中的值。问题是,我不知道如何访问数据库条目的ID来更新它。每次更改任何列表项中的某些内容时,它都会更新最后一个列表项的值 我尝试对我的三个视图使用setTag()和getTag(),但在侦听器中调用getTag()时,我得到一个空值。如何获取当前列表项的ID 更新:好的,我离解决这个案子越来越近了。我现在的问题是,我
更新:好的,我离解决这个案子越来越近了。我现在的问题是,我不知道如何正确设置和获取微调器的标记。我试着从家长AdapterView那里得到标签,但不是这样
public class DoorSelectorCursorAdapter extends CursorAdapter {
private ProjectsDBHelper mDbHelper;
Button bDelete;
Spinner spDoor;
TextView tvDoorName;
CheckBox cbAdded;
String doorID;
long currentID, wallID;
Intent mIntent;
public DoorSelectorCursorAdapter(Context context, Cursor cursor, Intent intent) {
super(context, cursor, 0);
final String LOG_TAG = DoorSelectorCursorAdapter.class.getSimpleName();
Log.v(LOG_TAG, "DoorSelectorCursorAdapter инициализирован");
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return LayoutInflater.from(context).inflate(R.layout.selector_list_item, parent, false);
}
// The bindView method is used to bind all data to a given view
// such as setting the text on a TextView.
@Override
public void bindView(final View view, Context context, final Cursor cursor) {
final String LOG_TAG = DoorSelectorCursorAdapter.class.getSimpleName();
Log.v(LOG_TAG, "вызван BindView");
currentID = cursor.getLong(cursor.getColumnIndex(DataContract.WallDoorEntry.COLUMN_WALLDOOR_ID));
wallID = cursor.getLong(cursor.getColumnIndex(DataContract.WallDoorEntry.COLUMN_WALLDOOR_IDWALL_FK));
spDoor = (Spinner) view.findViewById(R.id.spinner_item);
tvDoorName = (TextView) view.findViewById(R.id.selector_itemName);
cbAdded = (CheckBox) view.findViewById(R.id.selector_checkbox);
bDelete = (Button) view.findViewById(R.id.selector_button_delete);
spDoor.setTag(currentID);
cbAdded.setTag(currentID);
bDelete.setTag(currentID);
//res[0] = (cursor.getInt(cursor.getColumnIndex(DataContract.WallDoorEntry.COLUMN_WALLDOOR_IDDOOR_FK)) - 1);
// final long[] iCurrentSelection = new long[1];
// iCurrentSelection[0] = spDoor.getSelectedItemPosition();
int currentPosition = cursor.getPosition();
tvDoorName.setText("Дверь " + (currentPosition + 1));
if(cursor.getInt(cursor.getColumnIndex(DataContract.WallDoorEntry.COLUMN_WALLDOOR_EXISTS)) == 1){
cbAdded.setChecked(true);
} else if(cursor.getInt(cursor.getColumnIndex(DataContract.WallDoorEntry.COLUMN_WALLDOOR_EXISTS)) == 0) {
cbAdded.setChecked(false);
}
final Cursor doorCursor = context.getContentResolver().query(DataContract.DoorEntry.CONTENT_URI, null, null, null, null);
if (doorCursor.getCount() > 0){
DoorCursorAdapter doorAdapter = new DoorCursorAdapter(context, doorCursor);
spDoor.setAdapter(doorAdapter);
}
if(cursor.getString(cursor.getColumnIndex(DataContract.WallDoorEntry.COLUMN_WALLDOOR_IDDOOR_FK)) != null){
spDoor.setSelection(cursor.getInt(cursor.getColumnIndex(DataContract.WallDoorEntry.COLUMN_WALLDOOR_IDDOOR_FK)) - 1);
}
spDoor.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// if (res[0] == id){
// }
// else {
// spDoor.setSelection(cursor.getInt(cursor.getColumnIndex(DataContract.WallDoorEntry.COLUMN_WALLDOOR_IDDOOR_FK)) - 1);
// }
// if (iCurrentSelection[0] != id){
// // Your code here
int intID = (Integer) view.getTag();
doorID = String.valueOf(id);
updateEntry(intID);
// }
// iCurrentSelection[0] = id;
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
cbAdded.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int intID = (Integer) view.getTag();
updateEntry(intID);
}
});
// cbAdded.setOnClickListener(new CompoundButton.OnCheckedChangeListener() {
// @Override
// public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// updateEntry();
// }
// }
// );
bDelete.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
deleteEntry(cursor, arg0.getTag());
}
});
}
private void deleteEntry(Cursor cursor, Object tag) {
mContext.getContentResolver().delete(DataContract.WallDoorEntry.CONTENT_URI, DataContract.WallDoorEntry.COLUMN_WALLDOOR_ID + " LIKE " + tag, null);
// Cursor newCursor = mContext.getContentResolver().query(DataContract.WallDoorEntry.CONTENT_URI, null,DataContract.WallDoorEntry.COLUMN_WALLDOOR_IDWALL_FK + " LIKE " + wallID, null, null);
// super.swapCursor(newCursor);
}
private void updateEntry(int tagID) {
ContentValues entryValues = new ContentValues();
entryValues.put(DataContract.WallDoorEntry.COLUMN_WALLDOOR_IDDOOR_FK, doorID);
int checked = 0;
if (cbAdded.isChecked() == true){
checked = 1;
}
entryValues.put(DataContract.WallDoorEntry.COLUMN_WALLDOOR_EXISTS, checked);
int numOfUpdatedEntries = mContext.getContentResolver().update(DataContract.WallDoorEntry.CONTENT_URI, entryValues,
DataContract.WallDoorEntry.COLUMN_WALLDOOR_ID + " LIKE " + tagID, null);
}
}
公共类门选择器或游标适配器扩展游标适配器{
私人项目顾问公司;
按钮删除;
纺纱机;
TextView-tvDoorName;
增加了复选框;
门牌;
长currentID,wallID;
意向性薄荷糖;
公共门选择器或光标或适配器(上下文上下文、光标、意图){
超级(上下文,游标,0);
最终字符串LOG_TAG=DoorSelectorCursorAdapter.class.getSimpleName();
Log.v(Log_标签,“门选择器或接收器适配器”);
}
@凌驾
公共视图newView(上下文上下文、光标、视图组父对象){
返回LayoutFlater.from(上下文)。充气(R.layout.selector\u list\u项,父项,false);
}
//bindView方法用于将所有数据绑定到给定视图
//例如在文本视图上设置文本。
@凌驾
公共void bindView(最终视图、上下文上下文、最终光标){
最终字符串LOG_TAG=DoorSelectorCursorAdapter.class.getSimpleName();
Log.v(日志标签,“视图”);
currentID=cursor.getLong(cursor.getColumnIndex(DataContract.WallDoorEntry.COLUMN_WALLDOOR_ID));
wallID=cursor.getLong(cursor.getColumnIndex(DataContract.WallDoorEntry.COLUMN\u WALLDOOR\u IDWALL\u FK));
spDoor=(微调器)view.findViewById(R.id.Spinner\u项);
tvDoorName=(TextView)view.findViewById(R.id.selector\u itemName);
cbAdded=(复选框)view.findviewbyd(R.id.selector\u复选框);
b删除=(按钮)视图.findViewById(R.id.selector\u Button\u delete);
spDoor.setTag(当前ID);
cbAdded.setTag(currentID);
b删除.setTag(当前ID);
//res[0]=(cursor.getInt(cursor.getColumnIndex(DataContract.WallDoorEntry.COLUMN\u WALLDOOR\u IDDOOR\u FK))-1);
//最终长[]iCurrentSelection=新长[1];
//ICCurrentSelection[0]=spDoor.getSelectedItemPosition();
int currentPosition=cursor.getPosition();
tvDoorName.setText(“ааа”+(当前位置+1));
if(cursor.getInt(cursor.getColumnIndex(DataContract.WallDoorEntry.COLUMN\u WALLDOOR\u存在))=1{
cbAdded.setChecked(true);
}else if(cursor.getInt(cursor.getColumnIndex(DataContract.WallDoorEntry.COLUMN\u WALLDOOR\u存在))==0){
cbAdded.setChecked(false);
}
最终游标doorCursor=context.getContentResolver().query(DataContract.DoorEntry.CONTENT_URI,null,null,null);
if(doorCursor.getCount()>0){
DoorCursorAdapter doorAdapter=新的DoorCursorAdapter(上下文,doorCursor);
门设置适配器(门适配器);
}
if(cursor.getString(cursor.getColumnIndex(DataContract.WallDoorEntry.COLUMN\u WALLDOOR\u IDDOOR\u FK))!=null{
spDoor.setSelection(cursor.getInt(cursor.getColumnIndex(DataContract.WallDoorEntry.COLUMN\u WALLDOOR\u IDDOOR\u FK))-1;
}
spDoor.setOnItemSelectedListener(新的AdapterView.OnItemSelectedListener(){
@凌驾
已选择公共视图(AdapterView父视图、视图视图、整型位置、长id){
//if(res[0]==id){
// }
//否则{
//spDoor.setSelection(cursor.getInt(cursor.getColumnIndex(DataContract.WallDoorEntry.COLUMN\u WALLDOOR\u IDDOOR\u FK))-1;
// }
//如果(iCurrentSelection[0]!=id){
////您的代码在这里
int intID=(整数)view.getTag();
doorID=字符串。valueOf(id);
更新尝试(intID);
// }
//iCurrentSelection[0]=id;
}
@凌驾
未选择公共无效(AdapterView arg0){
}
});
cbAdded.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
int intID=(整数)view.getTag();
更新尝试(intID);
}
});
//cbAdded.setOnClickListener(新建CompoundButton.OnCheckedChangeListener(){
//@覆盖
//检查更改后的公共无效(复合按钮视图,布尔值已检查){
//updateEntry();
// }
// }
// );
b删除.setOnClickListener(新视图.OnClickListener(){
公共void onClick(视图arg0){
deleteEntry(游标arg0.getTag());
}
});
}
私有void deleteEntry(光标、对象标记){
mContext.getContentResolver().delete(DataContract.WallDoorEntry.CONTENT\u URI,DataContract.WallDoorEntry.COLUMN\u WALLDOOR\u ID+“LIKE”+标记,null);
//Cursor newCursor=mContext.getContentResolver().query(DataContract.Wa
final Map<Integer, Integer> mBoundItems = new HashMap<>();
final int key = someKnownKeyForItem();
mBoundItems.put(key, currentID);
final int key = someKnownKeyForThisItem();
final int id = mBoundItems.get(key);