Last active
August 29, 2015 14:16
-
-
Save valokafor/69b4b8c007c82700b824 to your computer and use it in GitHub Desktop.
Custom Listview Adapter Android
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.okason.serviceinvoice.Adapters; | |
import android.app.Activity; | |
import android.content.Context; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.ArrayAdapter; | |
import android.widget.CheckBox; | |
import android.widget.CompoundButton; | |
import android.widget.ImageButton; | |
import android.widget.TextView; | |
import android.widget.Toast; | |
import com.okason.serviceinvoice.Activities.ServiceListActivity; | |
import com.okason.serviceinvoice.Comparer.ServiceComparer; | |
import com.okason.serviceinvoice.Data.DatabaseManager; | |
import com.okason.serviceinvoice.Model.ProfessionalService; | |
import com.okason.serviceinvoice.R; | |
import com.okason.serviceinvoice.Utilities.CurrencyFormatter; | |
import com.okason.serviceinvoice.Utilities.ServiceUtils; | |
import java.util.Collections; | |
import java.util.List; | |
/** | |
* Created by Valentine on 2/5/2015. | |
*/ | |
public class ServiceAdapter extends ArrayAdapter<ProfessionalService> { | |
private List<ProfessionalService> mListOfServices; | |
private int layoutResourceId; | |
private Activity mActivity; | |
//Current sort method | |
private static ServiceUtils.SortMethod sSortMethod; | |
//Database reference | |
private DatabaseManager mDatabaseManager; | |
/*** | |
* Constructor | |
* @param activity = context | |
*/ | |
public ServiceAdapter(Activity activity, int resource, List<ProfessionalService> services) { | |
super(activity, resource, services); | |
this.mActivity = activity; | |
this.layoutResourceId = resource; | |
this.mListOfServices = services; | |
//If there is no current sort method, use Name | |
if (sSortMethod == null){ | |
sSortMethod = ServiceUtils.SortMethod.NAME; | |
} | |
mDatabaseManager = DatabaseManager.getInstance(mActivity); | |
} | |
@Override | |
public View getView(final int position, View convertView, ViewGroup parent) { | |
View view = convertView; | |
ServiceHolder holder = null; | |
if (view == null){ | |
LayoutInflater inflater = (LayoutInflater) mActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); | |
view = inflater.inflate(R.layout.list_row_service, null); | |
holder = new ServiceHolder(); | |
holder.textName = (TextView) view.findViewById(R.id.tvServiceName); | |
holder.textPrice = (TextView) view.findViewById(R.id.tvServicePrice); | |
holder.textCategory = (TextView) view.findViewById(R.id.tvServiceCategory); | |
holder.checkBoxAdd = (CheckBox) view.findViewById(R.id.checkboxAdd); | |
holder.buttonDeleteService = (ImageButton) view.findViewById(R.id.buttonDeleteService); | |
holder.buttonEditService = (ImageButton) view.findViewById(R.id.buttonEditService); | |
view.setTag(holder); | |
} else { | |
holder = (ServiceHolder) view.getTag(); | |
} | |
final ProfessionalService serviceItem = mListOfServices.get(position); | |
holder.textName.setText(serviceItem.getName()); | |
holder.textPrice.setText(CurrencyFormatter.FormatCurrency(serviceItem.getPrice())); | |
holder.textCategory.setText(serviceItem.getCategory()); | |
holder.buttonEditService.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
Toast.makeText(mActivity, "Edit Button Clicked", Toast.LENGTH_SHORT).show(); | |
} | |
}); | |
holder.buttonDeleteService.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
if (mDatabaseManager.deleteServiceById(serviceItem.getId())){ | |
mListOfServices.remove(position); | |
notifyDataSetChanged(); | |
} else {{ | |
Toast.makeText(mActivity, "Unable to delete Delete", | |
Toast.LENGTH_SHORT).show(); | |
}} | |
} | |
}); | |
holder.checkBoxAdd.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { | |
@Override | |
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { | |
if (mActivity instanceof ServiceListActivity){ | |
((ServiceListActivity)mActivity).onServiceSelected(serviceItem, isChecked); | |
} | |
} | |
}); | |
return view; | |
} | |
//This adds a ServiceItem to the List property of this adapter | |
public void add(ProfessionalService service){ | |
mListOfServices.add(service); | |
notifyDataSetChanged(); | |
} | |
public static class ServiceHolder{ | |
TextView textPrice; | |
TextView textName; | |
TextView textCategory; | |
CheckBox checkBoxAdd; | |
ImageButton buttonEditService; | |
ImageButton buttonDeleteService; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment