如何在 CursorAdapter 中获取我当前的 ID?

How to get my current ID in CursorAdapter?

我有一个 ListView,它由 CursorAdapter 填充。 ListView 中的每个项目都有一个微调器、一个按钮和一个复选框。当用户在微调器中选择某些内容、勾选复选框或按下按钮时,我需要更新数据库中的值。问题是,我不知道如何访问数据库条目的 ID 来更新它。每次更改任何列表项中的内容时,它都会更新最后一个列表项的值。

我已经尝试对我的三个视图使用 setTag()getTag(),但是当调用 getTag() 时在侦听器中,我得到一个 null 值。如何获取当前列表项的 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);
    }
}

您可以在Adapter中的某些Collection中保存绑定物品的状态。

final Map<Integer, Integer> mBoundItems = new HashMap<>();

保存状态,一次物品绑定。您可以使用任何对象作为键来访问您的 ID。在此示例中,您可以为项目使用您已知的密钥。

final int key = someKnownKeyForItem();
mBoundItems.put(key, currentID);

然后,每当您需要来自 Views 的点击并已知此位置键或任何其他条件的数据时。

final int key = someKnownKeyForThisItem();
final int id = mBoundItems.get(key);