Android - 在数据库onItemClick中获取_id

时间:2014-12-22 12:30:09

标签: android mysql database listview

所以我现在有点困惑。我正在使用带有内部类的simpleCursorAdapter,它扩展了BaseAdapter,因此我可以在每个x项目的ListView中显示广告。现在,当用户单击ListView中的项目时,会弹出一个对话框,用户可以选择“删除”,这显然会删除该行。在我的数据库适配器中,它需要数据库中的_id删除到行,所以我传递了我从方法onItemClick获得的ID。现在我了解到,当使用自定义适配器时,onItemClick的long id返回位置,因此我需要在数据库中查找_id。

这是我的onItemClick:

myList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View viewClicked, 
                int position, long idInDB) {

Cursor cursor = (Cursor)parent.getItemAtPosition(position);
                if( cursor != null && cursor.moveToFirst() ){
                    int rowCol = cursor.getColumnIndex("_id");
                    idInDB1 = cursor.getLong(rowCol);

“idInDB1”是从我的数据库适配器传递给我的删除方法的长。

我正在设置我的SimpleCursorAdapter:

myList.setAdapter(new ListViewAdapter(getActivity(), myCursorAdapter));

以下是我必须展示广告的ListViewAdapter类:

public class ListViewAdapter extends BaseAdapter {

private Activity mContext;  
private SimpleCursorAdapter listAdapter;  
private LayoutInflater mLayoutInflater;  
private int k = 6; // interval ad every 6 items
int baseItems;
int noAds; // used for listview offset

// Constructor takes in a BaseAdapter 
public ListViewAdapter(FragmentActivity activity, SimpleCursorAdapter delegate) {
    mContext = activity;
    listAdapter = delegate; 
    baseItems = listAdapter.getCount();
    noAds  = baseItems / k;
    mLayoutInflater = (LayoutInflater) mContext  
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}



@Override
public int getCount() {
    // Total count includes list items and ads.
    return baseItems + noAds;
}

@Override
public Object getItem(int position) {
    // Return null if an item is an ad.  Otherwise return the delegate item.
    if (isItemAnAd(position)) {
        return null;
    }
    return listAdapter.getItem(getOffsetPosition(position));
}

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

@Override
public int getViewTypeCount() {
    return listAdapter.getViewTypeCount() + noAds;
}

@Override
public int getItemViewType(int position) {
    if (isItemAnAd(position)) {
        return listAdapter.getViewTypeCount();
    } else {
        return listAdapter.getItemViewType(getOffsetPosition(position));
    }
}

@Override
public boolean areAllItemsEnabled() {
    return false;
}

@Override
public boolean isEnabled(int position) {
    return (!isItemAnAd(position)) && listAdapter.isEnabled(getOffsetPosition(position));
}

private boolean isItemAnAd(int position) {
    if (position < k) return false;
    // Calculate current offset caused by ads already embedded

    if (position==k){
        return true;
    }
    else {
        return isItemAnAd(position-k);
    }

}




// Get the position that is offset by the insertion of the ads
private int getOffsetPosition(int position) {

    int currentNoAds = position / k;
    return position - currentNoAds;

}



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

    // Display every n list items
    if (isItemAnAd(position)) {
        if (convertView instanceof AdView) {
            // Don’t instantiate new AdView, reuse old one
            return convertView;
        } else {
            // Create a new AdView
            AdView adView = new AdView(mContext);
            adView.setAdSize(AdSize.SMART_BANNER);
            adView.setAdUnitId("ca-app-pub-2454167205089490/6335584363");

            // Disable focus for sub-views of the AdView to avoid problems with
            // trackpad navigation of the list.
            for (int i = 0; i < adView.getChildCount(); i++)
            {
                adView.getChildAt(i).setFocusable(false);
            }
            adView.setFocusable(false);

            // Convert the default layout parameters so that they play nice with
            // ListView.

            float density = mContext.getResources().getDisplayMetrics().density;
            int height = Math.round(AdSize.BANNER.getHeight() * density);
            AbsListView.LayoutParams params = new AbsListView.LayoutParams(
                    AbsListView.LayoutParams.MATCH_PARENT,
                    height);
            adView.setLayoutParams(params);
            AdRequest bannerIntermediateReq = new AdRequest.Builder().build();
            adView.loadAd(bannerIntermediateReq);

            return adView;
        }
    } else {

        // Offload displaying other items to the delegate 
        return listAdapter.getView(getOffsetPosition(position) ,
                convertView, parent);

    }
}
}

我的“idInDB1”始终是我ListView中顶行的_id,我似乎无法弄清楚原因。任何人吗?

1 个答案:

答案 0 :(得分:1)

更改onItemClick中的代码
Cursor cursor = (Cursor)parent.getItemAtPosition(position);
 if( cursor != null && cursor.moveToFirst() ){
     int rowCol = cursor.getColumnIndex("_id");
 idInDB1 = cursor.getLong(rowCol);

Cursor cursor = (Cursor)((YourAdapter)listView.getAdapter()).getCursor();
cursor.moveToPosition(positon);
int rowCol = cursor.getColumnIndex("_id");
idInDB1 = cursor.getLong(rowCol);

你总是得到第一个id的原因是因为你将光标移动到第一个位置然后获得id。