Reputation: 95
So I'm a little bit confused right now. I'm using a simpleCursorAdapter with a inner class which extends BaseAdapter so I can show an ad in my ListView every x items. Now when a user clicks an item in the ListView a dialog pops up and the user can select "Delete" which obviously deletes that row. In my Database Adapter it needs the _id in the database to delete to row so I pass on the ID which I get from the method onItemClick. Now I learned that when using a custom adapter the long id from the onItemClick returns the position so I need to look up the _id in the database.
This is my 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);
The "idInDB1" is the long that get's passed to my delete method from my Database Adapter.
I'm setting my SimpleCursorAdapter like this:
myList.setAdapter(new ListViewAdapter(getActivity(), myCursorAdapter));
Here is the ListViewAdapter class that I have to display ads:
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);
}
}
}
My "idInDB1" is always the _id of the top row in my ListView and I can't seem to figure out why. Anyone?
Upvotes: 0
Views: 390
Reputation: 7259
Change the code in the onItemClick from
Cursor cursor = (Cursor)parent.getItemAtPosition(position);
if( cursor != null && cursor.moveToFirst() ){
int rowCol = cursor.getColumnIndex("_id");
idInDB1 = cursor.getLong(rowCol);
to
Cursor cursor = (Cursor)((YourAdapter)listView.getAdapter()).getCursor();
cursor.moveToPosition(positon);
int rowCol = cursor.getColumnIndex("_id");
idInDB1 = cursor.getLong(rowCol);
The reason why you always get the first id, is because you are moving the cursor to the first position and then getting the id.
Upvotes: 1