2015-02-07 1 views
3

횟수에 따라 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()); 

} 
+0

변경 목록 또는 배열과 notifyDatasetChanged 전화에서 항목 순서() –

+0

는 칼리 감사합니다. 나는이 경우 배열 순서를 100 번 변경해야한다. 어떤 간단한 논리로도 처리 할 수있다. –

+0

ArrayList를 사용하고 있다면 add(), remove()를 직접 사용할 수있다. –

답변

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; 
     } 
    } 
} 
관련 문제