请任何人帮助如何按名称对图像进行排序。我正在分享我的代码。请任何帮助。
这里显示带有网格和列表的图像。如何按网格和列表视图中的名称对图像进行排序。通过选择按名称排序和按大小排序,使用菜单。
// 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;
}
}
答案 0 :(得分:1)
通过使用数组排序,您可以在应用程序中对图像名称进行排序
//String array
String[] strNames = new String[]{"John", "Alex", "Chris", "Williams", "Mark", "Bob"};
使用排序方法
排序字符串数组 Arrays.sort(strNames);
以上给出的Java Sort String Array示例的输出将是 字符串数组已排序(区分大小写)
Alex
Bob
Chris
John
Mark
Williams
以同样的方式,在将数组数据设置为lisyt视图之前,您可以使用Arrays.sort(strNames)对其进行排序。
希望有所帮助
答案 1 :(得分:1)
你应该实现Java的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));
}
});
您也可以使用它们的属性来比较对象。
public int compare(final LatLong a, final LatLong b) {
return a.latitude.compareTo(b.latitude));
}