I have a custom SimpleCursorAdapter in a DialogFragment, and I am having trouble understanding the use of setTag and getTag. From my LogCat output, it seems that I am setting tag on a LinearLayout and trying to retrieve the tag from a Button. How can I target the right component to access the tag in the ClickListener?
@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
if (mCursor.moveToPosition(position)) {
ViewHolder holder;
final String label;
final int label_index = mCursor.getColumnIndex(ProfilesColumns.USERNAME);
label = mCursor.getString(label_index);
if (convertView == null) {
convertView = mInflater.inflate(layout, null);
holder = new ViewHolder();
holder.name = (Button) convertView.findViewById(R.id.title);
holder.logout = (Button) convertView.findViewById(R.id.logout);
holder.id = getItemId(position);
convertView.setTag(holder);
Log.d(DEBUG_TAG, "getView view " + convertView);//Returns LinearLayout
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.logout.setOnClickListener(new OnClickListener() {
@Override
public void onClick(final View v) {
ViewHolder holder;
if (v == null) {
Log.d(DEBUG_TAG, "logout view null ");
} else {
Log.d(DEBUG_TAG, "logout view " + v);//Returns Button
holder = (ViewHolder) v.getTag();
if (holder == null) {
Log.d(DEBUG_TAG, "logout holder null ");
} else {
Log.d(DEBUG_TAG, "logout holder.id " + holder.id);
String[] argument = { "" + holder.id };
ContentResolver cr = getActivity().getContentResolver();
int count = cr.delete(ProfileProvider.URI_LOADEDPROFILETABLE, CommonDatabaseHelper._ID
+ "=?", argument);
Log.d(DEBUG_TAG, "logout count " + count);
}
}
}
});
}
return convertView;
}
Here is the the layout, profileselect_list_item:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="0dp" >
<Button
android:id="@+id/title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button
android:id="@+id/logout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/logout" />
</LinearLayout>
The short answer is that you have not used
setTag()on the Buttons, you only used it on its parent the LinearLayout. So inside your OnClickListener change this:to:
Also this line:
changes for each row, while the OnClickListener doesn’t. You should move the above line outside of
if(convertView == null)and move the OnClickListener inside.The longer answer
You are extending a CursorAdapter, they have three great methods
newView(),bindView(), andgetView().newView()creates the new Views. Override this method and move all of yourif(convertView == null) { ... }code into here.bindView()has direct access to the Cursor. It seems the SimpleCursorAdapter’s default method takes care of this for you…Overriding
getView()is not always necessary because of the awesomeness ofnewView()andbindView().