如何根据项目点击次数更改网格项的位置

时间:2015-02-07 07:33:30

标签: android

根据项目点击的次数我有100个项目的网格我想要将项目重新排列到第一个位置哪个项目的点击次数按降序排列更高

HashMap<String, Integer> map = new HashMap<String, Integer>();
ValueComparator bvc = new ValueComparator(map);
TreeMap<String, Integer> sorted_map = new TreeMap<String, Integer>(bvc);

map.put("Windows", sharedPref.getInt("Windows", 0));
map.put("iOS", sharedPref.getInt("iOS", 0));
map.put("Android", sharedPref.getInt("Android", 0));
map.put("Blackberry", sharedPref.getInt("Blackberry", 0));
map.put("Java", sharedPref.getInt("Java", 0));
map.put("JQuery", sharedPref.getInt("JQuery", 0));
map.put("Phonegap", sharedPref.getInt("Phonegap", 0));
map.put("SQLite", sharedPref.getInt("SQLite", 0));
map.put("Thread", sharedPref.getInt("Thread", 0));
map.put("Video", sharedPref.getInt("Video", 0));
sorted_map.putAll(map);

iconList = new ArrayList<Integer>();
Map<String, Integer> treeMap = new TreeMap<String, Integer>(sorted_map);

for (Map.Entry<String, Integer> entry : treeMap.entrySet()) {
System.out.println("Key : " + entry.getKey() 
                                  + " Value : " + entry.getValue());


sortedList.add(entry.getKey());

}

1 个答案:

答案 0 :(得分:1)

我找到了解决问题的方法      包com.androidexample.gridview;

 import java.io.File;
 import java.util.ArrayList;
 import java.util.Comparator;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.TreeMap;

 import android.app.Activity;
 import android.content.SharedPreferences;
 import android.content.SharedPreferences.Editor;
 import android.os.Bundle;
 import android.util.Log;
 import android.view.View;
 import android.widget.AdapterView;
 import android.widget.AdapterView.OnItemClickListener;
 import android.widget.GridView;
 import android.widget.TextView;
 import android.widget.Toast;

 public class GridViewExample extends Activity {

    GridView gridView;
    private Map<String, Integer> iconMap = new HashMap<String, Integer>();
    int count0 = 0, count1 = 0, count2 = 0, count3 = 0, count4 = 0, count5 = 0,
            count6 = 0, count7 = 0, count8 = 0;
    static final String[] GRID_DATA = new String[] { "Windows", "iOS",
            "Android", "Blackberry", "Java", "JQuery", "Phonegap", "SQLite",
            "Thread", "Video" };

    SharedPreferences sharedPref;
    Editor editor;
    ArrayList<Integer> iconList;
    ArrayList<String> sortedList;
    String[] sorted_data;
    public GridViewExample() {
        // TODO Auto-generated constructor stub

        iconMap.put("Windows", R.drawable.windows_logo);
        iconMap.put("iOS", R.drawable.ios_logo);
        iconMap.put("Android", R.drawable.ic_launcher);
        iconMap.put("Blackberry", R.drawable.blackberry_logo);
        iconMap.put("Java", R.drawable.ic_launcher);
        iconMap.put("JQuery", R.drawable.ic_launcher);
        iconMap.put("Phonegap", R.drawable.ic_launcher);
        iconMap.put("SQLite", R.drawable.ic_launcher);
        iconMap.put("Thread", R.drawable.ic_launcher);
        iconMap.put("Video", R.drawable.ic_launcher);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.grid_view_android_example);

        sharedPref = getSharedPreferences("payism", MODE_APPEND);
        sortedList=new ArrayList<String>();

        File f = new File("/data/data/" + getPackageName()
                + "/shared_prefs/payism.xml");

        if (f.exists()) {
            Toast.makeText(getApplicationContext(), "file exist", 100).show();
        } else {
            editor = sharedPref.edit();
            editor.putInt("Windows", 0);
            editor.putInt("iOS", 0);
            editor.putInt("Android", 0);
            editor.putInt("Blackberry", 0);
            editor.putInt("Java", 0);
            editor.putInt("JQuery", 0);
            editor.putInt("Phonegap", 0);
            editor.putInt("SQLite", 0);
            editor.putInt("Thread", 0);
            editor.putInt("Video", 0);
            editor.commit();
        }

        // Get gridview object from xml file
        gridView = (GridView) findViewById(R.id.gridView1);

        // Set custom adapter (GridAdapter) to gridview
        gridView.setAdapter(new CustomGridAdapter(this, GRID_DATA));



        gridView.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v,
                    int position, long id) {

                if (((TextView) v.findViewById(R.id.grid_item_label)).getText()
                        .toString() == "Windows") {

                    count0 = sharedPref.getInt("Windows", 0);
                    count0 = count0 + 1;
                    editor = sharedPref.edit();
                    editor.putInt("Windows", count0);
                    Toast.makeText(getApplicationContext(),
                            "hi" + sharedPref.getInt("Windows", 0), 100).show();
                    editor.commit();

                } else if (((TextView) v.findViewById(R.id.grid_item_label))
                        .getText().toString() == "iOS") {

                    int count1 = sharedPref.getInt("iOS", 0);
                    count1 = count1 + 1;
                    editor = sharedPref.edit();
                    editor.putInt("iOS", count1);
                    Toast.makeText(getApplicationContext(),
                            "hi" + sharedPref.getInt("iOS", 0), 100).show();
                    editor.commit();

                } else if (((TextView) v.findViewById(R.id.grid_item_label))
                        .getText().toString() == "Android") {

                    int count2 = sharedPref.getInt("Android", 0);
                    count2 = count2 + 1;
                    editor = sharedPref.edit();
                    editor.putInt("Android", count2);
                    Toast.makeText(getApplicationContext(),
                            "hi" + sharedPref.getInt("Android", 0), 100).show();
                    editor.commit();

                } else if (((TextView) v.findViewById(R.id.grid_item_label))
                        .getText().toString() == "Blackberry") {

                    int count3 = sharedPref.getInt("Blackberry", 0);
                    count3 = count3 + 1;
                    editor = sharedPref.edit();
                    editor.putInt("Blackberry", count3);
                    Toast.makeText(getApplicationContext(),
                            "hi" + sharedPref.getInt("Blackberry", 0), 100)
                            .show();
                    editor.commit();

                } else if (((TextView) v.findViewById(R.id.grid_item_label))
                        .getText().toString() == "Java") {

                    int count4 = sharedPref.getInt("Java", 0);
                    count4 = count4 + 1;
                    editor = sharedPref.edit();
                    editor.putInt("Java", count4);
                    Toast.makeText(getApplicationContext(),
                            "hi" + sharedPref.getInt("Java", 0), 100).show();
                    editor.commit();

                } else if (((TextView) v.findViewById(R.id.grid_item_label))
                        .getText().toString() == "JQuery") {

                    int count5 = sharedPref.getInt("JQuery", 0);
                    count5 = count5 + 1;
                    editor = sharedPref.edit();
                    editor.putInt("JQuery", count5);
                    Toast.makeText(getApplicationContext(),
                            "hi" + sharedPref.getInt("JQuery", 0), 100).show();
                    editor.commit();

                } else if (((TextView) v.findViewById(R.id.grid_item_label))
                        .getText().toString() == "Phonegap") {

                    int count6 = sharedPref.getInt("Phonegap", 0);
                    count6 = count6 + 1;
                    editor = sharedPref.edit();
                    editor.putInt("Phonegap", count6);
                    Toast.makeText(getApplicationContext(),
                            "hi" + sharedPref.getInt("Phonegap", 0), 100)
                            .show();
                    editor.commit();

                } else if (((TextView) v.findViewById(R.id.grid_item_label))
                        .getText().toString() == "SQLite") {

                    int count7 = sharedPref.getInt("SQLite", 0);
                    count7 = count7 + 1;
                    editor = sharedPref.edit();
                    editor.putInt("SQLite", count7);
                    Toast.makeText(getApplicationContext(),
                            "hi" + sharedPref.getInt("SQLite", 0), 100).show();
                    editor.commit();

                } else if (((TextView) v.findViewById(R.id.grid_item_label))
                        .getText().toString() == "Thread") {

                    int count8 = sharedPref.getInt("Thread", 0);
                    count8 = count8 + 1;
                    editor = sharedPref.edit();
                    editor.putInt("Thread", count8);
                    Toast.makeText(getApplicationContext(),
                            "hi" + sharedPref.getInt("Thread", 0), 100).show();
                    editor.commit();

                }

                else if (((TextView) v.findViewById(R.id.grid_item_label))
                        .getText().toString() == "Video") {

                    int count9 = sharedPref.getInt("Video", 0);
                    count9 = count9 + 1;
                    editor = sharedPref.edit();
                    editor.putInt("Video", count9);
                    Toast.makeText(getApplicationContext(),
                            "hi" + sharedPref.getInt("Video", 0), 100).show();
                    editor.commit();

                }
            }
        });
        HashMap<String, Integer> map = new HashMap<String, Integer>();
        ValueComparator bvc = new ValueComparator(map);
        TreeMap<String, Integer> sorted_map = new TreeMap<String, Integer>(bvc);

        map.put("Windows", sharedPref.getInt("Windows", 0));
        map.put("iOS", sharedPref.getInt("iOS", 0));
        map.put("Android", sharedPref.getInt("Android", 0));
        map.put("Blackberry", sharedPref.getInt("Blackberry", 0));
        map.put("Java", sharedPref.getInt("Java", 0));
        map.put("JQuery", sharedPref.getInt("JQuery", 0));
        map.put("Phonegap", sharedPref.getInt("Phonegap", 0));
        map.put("SQLite", sharedPref.getInt("SQLite", 0));
        map.put("Thread", sharedPref.getInt("Thread", 0));
        map.put("Video", sharedPref.getInt("Video", 0));
        sorted_map.putAll(map);

        iconList = new ArrayList<Integer>();
        Map<String, Integer> treeMap = new TreeMap<String, Integer>(sorted_map);

        for (Map.Entry<String, Integer> entry : treeMap.entrySet()) {
            System.out.println("Key : " + entry.getKey() 
                                       + " Value : " + entry.getValue());


            sortedList.add(entry.getKey());


        }

        for(int i=0;i<sortedList.size();i++)
        {

            GRID_DATA[i]=sortedList.get(i);
            Log.d("Completed Sorted List Items are", GRID_DATA[i]);
        }

    }
    public static void printMap(Map<String, Integer> map) {
        for (Map.Entry<String, Integer> entry : map.entrySet()) {
            System.out.println("Key : " + entry.getKey() 
                                       + " Value : " + entry.getValue());
            entry.getKey();

        }
    }
 }

 class ValueComparator implements Comparator<String> {

    Map<String, Integer> base;

    public ValueComparator(Map<String, Integer> base) {
        this.base = base;
    }

    public int compare(String a, String b) {
        if (base.get(a) >= base.get(b)) {
            return -1;
        } else {
            return 1;
        }
    }
 }