I have a custom list adapter. Here it is:
public class FilesAdapter extends ArrayAdapter<PutioFileLayout> {
Context context;
int layoutResourceId;
List<PutioFileLayout> data = null;
public FilesAdapter(Context context, int layoutResourceId, List<PutioFileLayout> data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
FileHolder holder = null;
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new FileHolder();
holder.textName = (TextView) row.findViewById(R.id.text_fileListName);
holder.textDescription = (TextView) row.findViewById(R.id.text_fileListDesc);
holder.imgIcon = (ImageView) row.findViewById(R.id.img_fileIcon);
row.setTag(holder);
} else {
holder = (FileHolder) row.getTag();
}
PutioFileLayout file = data.get(position);
holder.textName.setText(file.name);
holder.textDescription.setText(file.description);
holder.imgIcon.setImageResource(file.icon);
return row;
}
static class FileHolder {
TextView textName;
TextView textDescription;
ImageView imgIcon;
}
}
Pretty short and sweet. I have Spinners in my layout for each row, and I want the user to be able to click them, and get a contextual menu for each item. How can I implement this in my adapter?
My row.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="64dp" >
<ImageView
android:id="@+id/img_fileIcon"
android:layout_width="36dp"
android:layout_height="36dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:scaleType="fitCenter"
android:src="@drawable/ic_launcher" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toRightOf="@id/img_fileIcon"
android:orientation="vertical" >
<TextView
android:id="@+id/text_fileListName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:layout_marginTop="12dp"
android:ellipsize="end"
android:maxLines="1"
android:text="File name"
android:textAppearance="?android:attr/textAppearanceMedium" />
<FrameLayout
android:id="@+id/descriptionFrame"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:layout_marginRight="30dp" >
<TextView
android:id="@+id/text_fileListDesc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="File description" />
</FrameLayout>
</LinearLayout>
<Spinner
android:id="@+id/item_fileSpinner"
android:layout_width="44dp"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:background="@drawable/spinner_background_ab_putio" />
</RelativeLayout>
Inside your getView() method get a reference to the spinner like this:
once you have the reference you can set its items by creating a SpinnerAdapter with all of your values and set it with
I can give you a more specific example of how you’d create and fill your adapter if you can tell me what data you are hoping to fill the spinner with.
then all thats left is to set an OnItemSelectedListener, which you can do like this.
Note the use of
spnPositionin the onItemSelected callback. If you usepositionthen you will no longer have access to the getView() parameterpositionso use a different name so that you can access both/either if needbe.