Android如何在网格视图和列表视图中按名称对图像进行排序

时间:2015-08-12 06:20:34

标签: android

请任何人告诉我如何在列表视图和网格视图中按名称对图像进行排序。 我有Country_images和Country_names.Am按Country_name排序,但是,只能对country_names进行排序。请任何一个帮助如何按名称排序图像。

//主要活动

        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;
                }
            }

1 个答案:

答案 0 :(得分:0)

您可以尝试使用此代码对country_Images

进行排序
TreeMap<String, Integer> names = new TreeMap<String, Integer>();
for (int i = 0; i< country_Images.length; i++) {
  names.put(getResources().getResourceEntryName(country_Images[i]), country_Images[i]);
}
int i = 0;
int[] sortedCountryImages = new int[country_Images.length]; 
for (Entry<String, Integer> entry : names.entrySet()) {
  sortedCountryImages[i] = entry.getValue();
  i++;
}
country_Images = sortedCountryImages;

但请注意,排序后country_Images的顺序可能与country_Image_size

中的订单不匹配