Created
April 11, 2016 09:23
-
-
Save sheerazam/419be93cbb2a88c41349e048cec94091 to your computer and use it in GitHub Desktop.
Custom Cursor Adapter
This file contains hidden or 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
public class ForSaleCursorAdapter extends CursorAdapter { | |
int layout; | |
public ForSaleCursorAdapter(Context context, Cursor cursor, int layout) { | |
super(context, cursor, 0); | |
this.layout = layout; | |
} | |
// The newView method is used to inflate a new view and return it, | |
// you don't bind any data to the view at this point. | |
@Override | |
public View newView(Context context, Cursor cursor, ViewGroup parent) { | |
View rowView = LayoutInflater.from(context).inflate(layout, parent, false); | |
ViewHolder holder = new ViewHolder(); | |
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); | |
return rowView; | |
} | |
private class ViewHolder { | |
Button btnCall; | |
Button btnEmail; | |
Button btnPrice; | |
TextView tvBuildingType; | |
TextView tvLocation; | |
TextView tvArea; | |
} | |
// The bindView method is used to bind all data to a given view | |
// such as setting the text on a TextView. | |
@Override | |
public void bindView(View view, final Context context, final Cursor cursor) { | |
ViewHolder holder = (ViewHolder) view.getTag(); | |
holder.btnCall.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
Log.d("ForSaleCursorAdapter", "onClick()"); | |
call( | |
context, | |
cursor.getString(cursor.getColumnIndex(DBForSale.Columns.PHONE_NUMBER)) | |
); | |
} | |
}); | |
holder.btnEmail.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
Log.d("ForSaleCursorAdapter", "email()"); | |
email(context, | |
cursor.getString(cursor.getColumnIndexOrThrow(DBForSale.Columns.EMAIL)), | |
"Durt Beach", | |
" Text ", | |
"Send Email" | |
); | |
} | |
}); | |
// Extract properties from cursor | |
String building_type = cursor.getString( cursor.getColumnIndexOrThrow( DBForSale.Columns.BUILDING_TYPE ) ); | |
String location = cursor.getString( cursor.getColumnIndexOrThrow( DBForSale.Columns.LOCATION ) ); | |
String area = cursor.getString( cursor.getColumnIndexOrThrow( DBForSale.Columns.AREA ) ); | |
String amount = cursor.getString( cursor.getColumnIndexOrThrow( DBForSale.Columns.AMOUNT ) ); | |
holder.tvBuildingType.setText(building_type); | |
holder.tvLocation.setText(location); | |
holder.tvArea.setText(area); | |
holder.btnPrice.setText(amount); | |
} | |
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)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment