Created
December 26, 2012 13:44
-
-
Save anonymous/4380422 to your computer and use it in GitHub Desktop.
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.m7.nomad; | |
import java.util.ArrayList; | |
import java.util.List; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.os.Bundle; | |
import android.support.v4.app.DialogFragment; | |
import android.text.TextUtils; | |
import android.util.Log; | |
import android.view.View; | |
import android.widget.AdapterView; | |
import android.widget.AdapterView.OnItemClickListener; | |
import android.widget.AutoCompleteTextView; | |
import android.widget.ImageView; | |
import android.widget.ListView; | |
import com.actionbarsherlock.app.SherlockFragmentActivity; | |
import com.actionbarsherlock.view.Menu; | |
import com.actionbarsherlock.view.MenuInflater; | |
import com.actionbarsherlock.view.MenuItem; | |
import com.actionbarsherlock.widget.SearchView; | |
public class MainActivity extends SherlockFragmentActivity implements | |
SearchView.OnQueryTextListener { | |
ListView placesListView; | |
static Context context; | |
PlacesListAdapter adapter; | |
private Menu mainMenu = null; | |
public SearchView mSearchView; | |
private final String[] places = new String[] { "Mysore", "Bangalore", | |
"Mangalore", "Wayanad", "Bandipur National Park National Park", | |
"Chickmaglur", "Bandipura", "Coorg", "Kodaikanal", "Hampi", | |
"Ghati Subramanya", "Mekedatu", "Muththathhi", "Shivasamudram", | |
"Talakadu", "Savana Durga" }; | |
private final String[] distances = new String[] { "126", "365", "956", | |
"621", "65", "98", "654", "23", "75", "542", "58", "26", "95", | |
"64", "542", "69" }; | |
private final String[] icons = new String[] { "wildlife", "waterfall", | |
"hillstation", "hillstation", "wildlife", "waterfall", | |
"hillstation", "wildlife", "waterfall", "beach", "hillstation", | |
"wildlife", "waterfall", "hillstation", "hillstation", | |
"hillstation" }; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
context = this; | |
Log.i("Nomad", "onCreate"); | |
mSearchView = (SearchView) findViewById(R.id.action_search); | |
placesListView = (ListView) findViewById(R.id.list_view_places); | |
placesListView.setEmptyView(findViewById(R.id.empty)); | |
List<Place> thePlaces = new ArrayList<Place>(); | |
for (int i = 0; i < places.length; i++) { | |
Place pl = new Place(places[i], distances[i], icons[i]); | |
thePlaces.add(pl); | |
} | |
adapter = new PlacesListAdapter(MainActivity.this, | |
R.layout.list_item_places, thePlaces); | |
placesListView.setAdapter(adapter); | |
placesListView.setTextFilterEnabled(true); | |
placesListView.setOnItemClickListener(new OnItemClickListener() { | |
@Override | |
public void onItemClick(AdapterView<?> a, View view, int position, | |
long id) { | |
startActivity(new Intent(MainActivity.this, PlaceActivity.class)); | |
} | |
}); | |
} | |
@Override | |
public boolean onCreateOptionsMenu(Menu menu) { | |
Log.i("Nomad", "onCreateOptionsMenu"); | |
super.onCreateOptionsMenu(menu); | |
MenuInflater inflater = getSupportMenuInflater(); | |
inflater.inflate(R.menu.activity_main, menu); | |
mainMenu = menu; | |
MenuItem searchItem = menu.findItem(R.id.action_search); | |
// Search View | |
mSearchView = (SearchView) searchItem.getActionView(); | |
styleSearchView(mSearchView, MainActivity.this); | |
setupSearchView(searchItem); | |
mSearchView.setOnSearchClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
// hide action item | |
if (mainMenu != null) { | |
mainMenu.findItem(R.id.action_category).setVisible(false); | |
mainMenu.findItem(R.id.action_sort).setVisible(false); | |
} | |
} | |
}); | |
mSearchView.setOnCloseListener(new SearchView.OnCloseListener() { | |
@Override | |
public boolean onClose() { | |
adapter.getFilter().filter(""); | |
// re-show the action button | |
if (mainMenu != null) { | |
mainMenu.findItem(R.id.action_category).setVisible(true); | |
mainMenu.findItem(R.id.action_sort).setVisible(true); | |
} | |
return false; | |
} | |
}); | |
Log.i("Nomad", "after setupSearchView()"); | |
return true; | |
} | |
private void setupSearchView(MenuItem searchItem) { | |
mSearchView.setIconifiedByDefault(true); | |
mSearchView.setOnQueryTextListener(this); | |
mSearchView.setSubmitButtonEnabled(false); | |
searchItem.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_ALWAYS); | |
} | |
public static void styleSearchView(SearchView searchView, Context context) { | |
View searchPlate = searchView.findViewById(R.id.abs__search_plate); | |
searchPlate.setBackgroundResource(R.drawable.abs__search_plate_white); | |
ImageView searchCloseIcon = (ImageView) searchView | |
.findViewById(R.id.abs__search_close_btn); | |
searchCloseIcon | |
.setBackgroundResource(R.drawable.actionbar_search_close_bg_pressed); | |
ImageView searchButtonIcon = (ImageView) searchView | |
.findViewById(R.id.abs__search_button); | |
searchButtonIcon.setImageResource(R.drawable.ic_menu_search); | |
ImageView searchMagIcon = (ImageView) searchView | |
.findViewById(R.id.abs__search_mag_icon); | |
searchMagIcon.setImageResource(R.drawable.ic_menu_search); | |
AutoCompleteTextView searchText = (AutoCompleteTextView) searchView | |
.findViewById(R.id.abs__search_src_text); | |
searchText.setTextColor(context.getResources().getColor(R.color.white)); | |
searchText.setHintTextColor(context.getResources().getColor( | |
R.color.white)); | |
} | |
public boolean onOptionsItemSelected(MenuItem item) { | |
switch (item.getItemId()) { | |
case R.id.action_category: | |
showDialog(); | |
break; | |
case R.id.action_about: | |
startActivity(new Intent(MainActivity.this, AboutActivity.class)); | |
break; | |
default: | |
// return super.onOptionsItemSelected(item); | |
break; | |
} | |
return true; | |
} | |
/* Dialog Fragment */ | |
void showDialog() { | |
DialogFragment newFragment = CategoriesDialogFragment | |
.newInstance(R.string.place_about); | |
newFragment.show(getSupportFragmentManager(), "dialog"); | |
} | |
/* Search Filtering */ | |
public boolean onQueryTextChange(String newText) { | |
Log.i("Nomad", "onQueryTextChange"); | |
if (TextUtils.isEmpty(newText)) { | |
adapter.getFilter().filter(""); | |
Log.i("Nomad", "onQueryTextChange Empty String"); | |
placesListView.clearTextFilter(); | |
} else { | |
Log.i("Nomad", "onQueryTextChange " + newText.toString()); | |
adapter.getFilter().filter(newText.toString()); | |
} | |
return true; | |
} | |
public boolean onQueryTextSubmit(String query) { | |
Log.i("Nomad", "onQueryTextSubmit"); | |
return false; | |
} | |
public boolean onClose() { | |
Log.i("Nomad", "onClose"); | |
return false; | |
} | |
protected boolean isAlwaysExpanded() { | |
return false; | |
} | |
} |
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.m7.nomad; | |
import java.util.ArrayList; | |
import java.util.List; | |
import android.content.Context; | |
import android.util.Log; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.ArrayAdapter; | |
import android.widget.Filter; | |
import android.widget.Filterable; | |
import android.widget.ImageView; | |
import android.widget.TextView; | |
public class PlacesListAdapter extends ArrayAdapter<Place> implements | |
Filterable { | |
public Context mContext; | |
List<Place> mPlaces, orig; | |
public PlacesListAdapter(Context context, int resource, List<Place> places) { | |
super(context, resource, places); | |
// TODO Auto-generated constructor stub | |
this.mPlaces = places; | |
this.mContext = context; | |
orig = new ArrayList<Place>(this.mPlaces); | |
} | |
@Override | |
public Place getItem(int position) { | |
// TODO Auto-generated method stub | |
return mPlaces.get(position); | |
} | |
@Override | |
public long getItemId(int position) { | |
return position; | |
} | |
@Override | |
public View getView(int position, View convertView, ViewGroup parent) { | |
ViewHolder holder; | |
if (convertView == null) { | |
LayoutInflater viewInflater; | |
viewInflater = LayoutInflater.from(getContext()); | |
convertView = viewInflater.inflate(R.layout.list_item_places, null); | |
holder = new ViewHolder(); | |
holder.placeTitle = (TextView) convertView | |
.findViewById(R.id.place_title); | |
holder.placeDistance = (TextView) convertView | |
.findViewById(R.id.place_distance); | |
holder.placeCategoryIcon = (ImageView) convertView | |
.findViewById(R.id.place_category_icon); | |
convertView.setTag(holder); | |
} else { | |
holder = (ViewHolder) convertView.getTag(); | |
} | |
Log.i("Nomad", mPlaces.get(position).getPlaceName()); | |
holder.placeTitle.setText(mPlaces.get(position).getPlaceName()); | |
holder.placeDistance.setText(mPlaces.get(position).getPlaceDistance()); | |
holder.placeCategoryIcon | |
.setImageResource(R.drawable.icon_category_hillstation); | |
// // Setting Alternative Row Colors | |
if (position % 2 == 0) { | |
convertView | |
.setBackgroundResource(R.drawable.list_view_places_row_1); | |
} else { | |
convertView | |
.setBackgroundResource(R.drawable.list_view_places_row_2); | |
} | |
return convertView; | |
} | |
static class ViewHolder { | |
TextView placeId; | |
TextView placeTitle; | |
TextView placeDistance; | |
ImageView placeCategoryIcon; | |
} | |
@Override | |
public Filter getFilter() { | |
// TODO Auto-generated method stub | |
return new PlaceFilter(); | |
} | |
private class PlaceFilter extends Filter { | |
@Override | |
protected FilterResults performFiltering(CharSequence constraint) { | |
constraint = constraint.toString().toLowerCase(); | |
FilterResults result = new FilterResults(); | |
if (constraint != null && constraint.toString().length() > 0) { | |
List<Place> founded = new ArrayList<Place>(); | |
for (Place item : mPlaces) { | |
if (item.toString().toLowerCase().contains(constraint)) { | |
founded.add(item); | |
} | |
} | |
result.values = founded; | |
result.count = founded.size(); | |
} else { | |
result.values = orig; | |
result.count = orig.size(); | |
} | |
return result; | |
} | |
@Override | |
protected void publishResults(CharSequence constraint, | |
FilterResults results) { | |
clear(); | |
for (Place item : (List<Place>) results.values) { | |
add(item); | |
} | |
notifyDataSetChanged(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment