Skip to content

Instantly share code, notes, and snippets.

@sheerazam
Created April 11, 2016 09:23
Show Gist options
  • Save sheerazam/5cc6d3e93e59c6b65019a1fb3abbc1ad to your computer and use it in GitHub Desktop.
Save sheerazam/5cc6d3e93e59c6b65019a1fb3abbc1ad to your computer and use it in GitHub Desktop.
Custom Array Adapter
public class ForSaleArrayAdapter extends ArrayAdapter<ForSale> {
Context context;
int layout;
public ForSaleArrayAdapter(Context context, List<ForSale> sales , int layout) {
super(context, 0, sales);
this.layout = layout;
this.context = context;
}
private class ViewHolder {
Button btnCall;
Button btnEmail;
Button btnPrice;
TextView tvBuildingType;
TextView tvLocation;
TextView tvArea;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ForSale sale = getItem(position);
View rowView;
ViewHolder holder;
if( convertView == null){
holder = new ViewHolder();
rowView = LayoutInflater.from(context).inflate(layout, parent, false);
holder.btnCall = (Button) rowView.findViewById(R.id.btnCall);
holder.btnPrice = (Button) rowView.findViewById(R.id.btnPrice);
holder.btnEmail = (Button) rowView.findViewById(R.id.btnEmail);
holder.tvBuildingType = (TextView) rowView.findViewById(R.id.tvBuildingType);
holder.tvLocation = (TextView) rowView.findViewById(R.id.tvLocation);
holder.tvArea = (TextView) rowView.findViewById(R.id.tvArea);
rowView.setTag(holder);
} else {
rowView = convertView;
holder = (ViewHolder) convertView.getTag();
}
holder.btnCall.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("ForSaleCursorAdapter", "onClick()");
call(
context,
sale.phoneNumber
);
}
});
holder.btnEmail.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("ForSaleCursorAdapter", "email()");
email(context,
sale.email,
"Durt Beach",
" Text ",
"Send Email"
);
}
});
// Extract properties from cursor
String building_type = sale.buildingType;
String location = sale.location;
String area = sale.area;
String amount = sale.amount;
holder.tvBuildingType.setText(building_type);
holder.tvLocation.setText(location);
holder.tvArea.setText(area);
holder.btnPrice.setText(amount);
return rowView;
}
void call( Context context, String number ){
Log.d("ForSaleAdapter", number);
Intent callIntent = new Intent( Intent.ACTION_CALL, Uri.parse( "tel:" + number ) );
context.startActivity(callIntent);
}
void email( Context context, String email, String subject, String text, String title ){
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_EMAIL, email);
intent.putExtra(Intent.EXTRA_SUBJECT, subject );
intent.putExtra(Intent.EXTRA_TEXT, text );
context.startActivity(Intent.createChooser(intent, title));
}
public void setData(List<ForSale> data) {
clear();
if (data != null) {
for (ForSale aData : data) {
add(aData);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment