Reputation: 35
Please any one help how to sort images by name.Here am sharing my code.please any one help.
Here am showing images with grid and list .How to sort images by name in grid and list view.Here am using menu by selecting sort by name and sort by size.
// Main Activity
public class MainActivity extends ActionBarActivity {
public RelativeLayout mainLayout;
View gridView,listView;
CountryAdapterList customListAdapter;
CountryAdapter cutomArrayAdapter;
public int swithNo=0;
public String[] country_Names;
public RelativeLayout relativeLayout;
public int[] country_Images = {R.drawable.banglades, R.drawable.bangladesh,
R.drawable.brazi, R.drawable.brazil, R.drawable.chin,
R.drawable.china, R.drawable.indi, R.drawable.india,
R.drawable.indonesi, R.drawable.indonesia, R.drawable.japa,
R.drawable.japan, R.drawable.nigeri, R.drawable.nigeria,
R.drawable.pakista, R.drawable.pakistan, R.drawable.russi,
R.drawable.russia, R.drawable.unitedstate,
R.drawable.unitedstates };
public String[] country_Name_Sort = { "Bangladesh A", "Pakistan",
"Brazil A", "Brazil", "China A","Bangladesh", "China", "India A", "India",
"Indonesia A", "Russia","Indonesia", "Japan A", "Japan", "Nigeria A",
"Nigeria", "Pakistan A", "Russia A",
"UnitesStates A", "UnitesStates" };
public float[] country_Image_size = {1.36f , 1.36f, 4.12f, 4.12f, 1.47f,
1.47f, 1.79f, 1.79f, 0.299f, 0.299f, 1.50f, 1.50f, 0.285f, 0.285f,
1.85f, 1.85f, 0.330f, 0.330f, 3.42f, 3.42f };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Creating a new RelativeLayout
relativeLayout = new RelativeLayout(this);
customListAdapter = new CountryAdapterList(getApplicationContext(),
country_Name_Sort, country_Image_size, country_Images);
cutomArrayAdapter=new CountryAdapter(getApplicationContext(), country_Name_Sort, country_Image_size, country_Images);
// Defining the RelativeLayout layout parameters.
// In this case I want to fill its parent
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT);
relativeLayout.setLayoutParams(rlp);
gridView = getLayoutInflater().inflate(R.layout.activity_deatails_grid,
null);
listView = getLayoutInflater().inflate(R.layout.activity_main_listview,
null);
setViewUpdate(swithNo);
((AdapterView<ListAdapter>) gridView).setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent i=new Intent(getApplicationContext(),CountryDetailsScreen.class);
i.putExtra("Position", position);
i.putExtra("Country_Name", country_Name_Sort);
i.putExtra("Country_image", country_Images);
i.putExtra("Country_Image_size", country_Image_size);
startActivity(i);
}
});
((AdapterView<ListAdapter>) listView).setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
Intent i=new Intent(getApplicationContext(),CountryDetailsScreen.class);
i.putExtra("Position", position);
i.putExtra("Country_Name", country_Name_Sort);
i.putExtra("Country_image", country_Images);
i.putExtra("Country_Image_size", country_Image_size);
startActivity(i);
}
});
}
private void sortAscending () {
List<String> sortedMonthsList = Arrays.asList(country_Name_Sort);
Collections.sort(sortedMonthsList);
country_Name_Sort = (String[]) sortedMonthsList.toArray();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
menu.add(1, 1, 0, "Grid View");
menu.add(1, 2, 1, "List View");
menu.add(2, 3, 2, "Sort By Name");
menu.add(2, 4, 3, "Sort By Size");
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
switch (item.getItemId()) {
case 1:
Log.d("SwithNo", "One");
swithNo=0;
setViewUpdate(swithNo);
break;
case 2:
Log.d("SwithNo", "Two");
swithNo=1;
setViewUpdate(swithNo);
break;
case 3:
Log.d("SwithNo", "Three");
sortAscending();
for(int i=0;i<country_Name_Sort.length;i++)
{
Log.e("Assending ", " "+country_Name_Sort[i]);
}
((ListView) listView).setAdapter(customListAdapter);
listView.invalidate();
setViewUpdate(swithNo);
break;
case 4 : Log.d("Switch", "Four");
}
return true;
}
public void setViewUpdate(int k)
{
((GridView) gridView).setAdapter(cutomArrayAdapter);
((ListView) listView).setAdapter(customListAdapter);
relativeLayout.removeAllViews();
if(k==0)
{
relativeLayout.addView(gridView);
}
else
{
relativeLayout.addView(listView);
}
setContentView(relativeLayout);
}
}
class Country {
int imageId;
String countryName;
Country(int imageId, String countyName) {
this.imageId = imageId;
this.countryName = countyName;
}
}
class CountryAdapter extends BaseAdapter {
ArrayList<Country> list;
Context context;
String[] country_Name_Sort;
CountryAdapter(Context context,String[] country_Name_Sort,float[] country_Image_size,int[] country_Images) {
this.context = context;
this.country_Name_Sort=country_Name_Sort;
list = new ArrayList<Country>();
/*Resources resource = context.getResources();
String[] country_Names = resource.getStringArray(R.array.coutry_names);*/
for (int i = 0; i < country_Name_Sort.length; i++) {
Country country = new Country(country_Images[i], country_Name_Sort[i]);
list.add(country);
}
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return list.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
class ViewHolder {
ImageView county_Image;
TextView country_Name;
ViewHolder(View v) {
county_Image = (ImageView) v.findViewById(R.id.imageView);
country_Name = (TextView) v.findViewById(R.id.countryName);
}
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
View row = convertView;
if (row == null) {
LayoutInflater inflator = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflator.inflate(R.layout.single_item, parent, false);
viewHolder = new ViewHolder(row);
row.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) row.getTag();
}
Country cntry = list.get(position);
viewHolder.county_Image.setImageResource(cntry.imageId);
viewHolder.country_Name.setText(cntry.countryName);
return row;
}
}
class CountryAdapterList extends BaseAdapter {
ArrayList<Country> list;
Context context;
String[] country_Name_Sort;
int[] country_Images;
CountryAdapterList(Context context,String[] country_Name_Sort,float[] country_Image_size,int[] country_Images) {
this.context = context;
this.country_Name_Sort=country_Name_Sort;
this.country_Images=country_Images;
list = new ArrayList<Country>();
for (int i = 0; i < country_Name_Sort.length; i++) {
Country country = new Country(country_Images[i], country_Name_Sort[i]);
list.add(country);
}
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return list.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
class ViewHolder {
ImageView county_Image;
TextView country_Name;
ViewHolder(View v) {
county_Image = (ImageView) v.findViewById(R.id.imageViewList);
country_Name = (TextView) v.findViewById(R.id.countryNameList);
}
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
View row = convertView;
if (row == null) {
LayoutInflater inflator = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflator.inflate(R.layout.single_item_listview, parent, false);
viewHolder = new ViewHolder(row);
row.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) row.getTag();
}
Country cntry = list.get(position);
viewHolder.county_Image.setImageResource(cntry.imageId);
viewHolder.country_Name.setText(cntry.countryName);
return row;
}
}
Upvotes: 0
Views: 1184
Reputation: 18765
By using array sorting you can sort your image names in your application
//String array
String[] strNames = new String[]{"John", "Alex", "Chris", "Williams", "Mark", "Bob"};
sort String array using sort method
Arrays.sort(strNames);
Output of above given Java Sort String Array example would be String array sorted (case sensitive)
Alex
Bob
Chris
John
Mark
Williams
In the same way, before setting your array data to your lisyt view you can sort it by using Arrays.sort(strNames);
hope it helps
Upvotes: 1
Reputation: 907
you should implement Java's Comparator.
ListView list;
//fill list here.
Collections.sort(list, new Comparator<String>() {
public int compare(final String a, final String b) {
return a.compareTo(b));
}
});
you can compare objects too, using their properties.
public int compare(final LatLong a, final LatLong b) {
return a.latitude.compareTo(b.latitude));
}
Upvotes: 1