Java tutorial
/** * Copyright 2014 Magnus Woxblom * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.teisentraeger.populationquiz; import android.content.Context; import android.content.res.Resources; import android.graphics.drawable.Drawable; import android.support.v4.util.Pair; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; import com.teisentraeger.populationquiz.model.Country; import com.woxthebox.draglistview.DragItemAdapter; import java.util.ArrayList; public class ItemAdapter extends DragItemAdapter<Pair<Long, Country>, ItemAdapter.ViewHolder> { private int mLayoutId; private int mGrabHandleId; private Context mContext; public ItemAdapter(ArrayList<Pair<Long, Country>> list, int layoutId, int grabHandleId, boolean dragOnLongPress, Context context) { super(dragOnLongPress); mContext = context; mLayoutId = layoutId; mGrabHandleId = grabHandleId; setHasStableIds(true); setItemList(list); } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(mLayoutId, parent, false); return new ViewHolder(view); } @Override public void onBindViewHolder(ViewHolder holder, int position) { super.onBindViewHolder(holder, position); String text = mItemList.get(position).second.name; holder.mText.setText(text); holder.itemView.setTag(text); Resources res = mContext.getResources(); int resID = res.getIdentifier(mItemList.get(position).second.getFilename(), "drawable", mContext.getPackageName()); holder.mImage.setImageResource(resID); } @Override public long getItemId(int position) { return mItemList.get(position).first; } public class ViewHolder extends DragItemAdapter<Pair<Long, Country>, ViewHolder>.ViewHolder { public TextView mText; public ImageView mImage; public ViewHolder(final View itemView) { super(itemView, mGrabHandleId); mText = (TextView) itemView.findViewById(R.id.text); mImage = (ImageView) itemView.findViewById(R.id.image); } @Override public void onItemClicked(View view) { //Toast.makeText(view.getContext(), "Item clicked", Toast.LENGTH_SHORT).show(); } @Override public boolean onItemLongClicked(View view) { //Toast.makeText(view.getContext(), "Item long clicked", Toast.LENGTH_SHORT).show(); return true; } } }