部分排序自己的适配器。为什么ListView没有完全排序?

时间:2014-05-04 18:33:15

标签: android listview sorting

我的应用程序包含所有instaled应用程序的ListView(TextView和RatingBar),并允许根据排名(在菜单中选择)对ListView进行排序。 这意味着,在从菜单sort-> ranking->降序f.e.中选择选项后,ListView包含已安装的应用程序和排名的名称,应根据排名按降序排序。问题是ListView没有完全排序。它工作,但没有很少的元素,但..滚动屏幕后是好的。 我认为这个问题可能出现在我的onRatingChanged上,但我还没有想到如何改变它。我能得到一些线索吗?

一段代码:

public class MainActivity extends Activity {
    private static final String PREFERENCES_NAME = "myPreferences3";
    public SharedPreferences pref;
    public SharedPreferences.Editor prefEditor;
    public ListView listView;
    public List<ApplicationInfo> packages;
    public PackageManager pm;
    public App app_packages[];
    public AppAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        pref=getSharedPreferences(PREFERENCES_NAME, MainActivity.MODE_PRIVATE);
        prefEditor = pref.edit(); 
        setContentView(R.layout.main_activity);

        pm=getPackageManager();
        packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);
        app_packages=new App[packages.size()];

        int i=0; 
        for (ApplicationInfo packageInfo : packages) 
        {
            app_packages[i]=new App(pm.getApplicationLabel(packageInfo).toString());
            i++;
        }

         adapter = new AppAdapter(this, R.layout.app, app_packages);
         listView=(ListView) findViewById(R.id.listView1);
             listView.setAdapter(adapter); 
         }


    //------------------------------------------------------------------------

    public class AppAdapter extends ArrayAdapter<App> 
    {
        private static final String PREFERENCES_NAME = "myPreferences3";
        public SharedPreferences pref=getSharedPreferences(PREFERENCES_NAME, MainActivity.MODE_PRIVATE);
        public SharedPreferences.Editor prefEditor=pref.edit();

       Context context; 
       App applications_array[]=null;
       int layoutResourceId;

       public AppAdapter(Context context, int layoutResourceId, App[] applications_array)
       {
           super(context, layoutResourceId, applications_array);  
           this.layoutResourceId=layoutResourceId;
           this.context=context;
           this.applications_array=applications_array;
       }

       @Override
       public View getView(final int position, View convertView, ViewGroup parent) 
       {           
           View app=convertView;  
           AppHolder holder=null;

           if(app==null) 
           {
               LayoutInflater inflater=((Activity)context).getLayoutInflater();     
               app=inflater.inflate(layoutResourceId, parent, false);

               holder=new AppHolder();
               holder.textView=(TextView)app.findViewById(R.id.appTitle);
               holder.ratingBar=(RatingBar)app.findViewById(R.id.appScore);
               app.setTag(holder);
           }
           else
           {
               holder=(AppHolder) app.getTag();
           }



        holder.ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener(){

            @Override
           public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
                prefEditor.putFloat("rating"+applications_array[position].title, rating).commit();

                            applications_array[position].rating=rating;
                            switch(pref.getString("sort", ""))
                            { 
                                case "r_up": Sort_r_up(); 
                                    adapter.notifyDataSetChanged(); 
                                    listView.smoothScrollToPosition(adapter.getPosition(applications_array[position]));
                                    break;
                                case "r_down": Sort_r_down();
                                    adapter.notifyDataSetChanged();
                                    listView.smoothScrollToPosition(adapter.getPosition(applications_array[position]));
                                    break;
                            }
                } 
                }); 
                holder.textView.setText(applications_array[position].title);
                holder.ratingBar.setRating(pref.getFloat("rating"+applications_array[position].title, 0.0f));

            return app;
       } 
    }


    static class AppHolder
    { 
        TextView textView; 
        RatingBar ratingBar;
    }

    public class App
    {
        public String title;
        public float rating;


        public App(){}

        public App(String title)
        {
            this.title=title;
            this.rating=0.0f;
        }

    }
    //---------------------------------------------------------------

    @Override
     public boolean onCreateOptionsMenu(Menu menu) {
         getMenuInflater().inflate(R.layout.menu, menu);
         return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        switch (item.getItemId())
        {

                 case R.id.r_up:
                prefEditor.putString("sort", "r_up").commit();
            Sort_r_up(); 
                adapter.notifyDataSetChanged();
                return true;

            case R.id.r_down:
                prefEditor.putString("sort", "r_down").commit();
                Sort_r_down();
                adapter.notifyDataSetChanged();
                return true;

            default:
            return super.onOptionsItemSelected(item);
        }
    }


    public void Sort_r_up()
    {
         adapter.sort(new Comparator<App>()
         {
             public int compare(App arg0, App arg1) {
                    if(arg0.rating>arg1.rating)
                        return 1;
                    else 
                        return -1;
                }
         });
    }

    public void Sort_r_down() 
    {
         adapter.sort(new Comparator<App>()
         {  
             public int compare(App arg0, App arg1) {
                    if(arg0.rating>=arg1.rating)
                        return -1;
                    else 
                        return 1; 
             }    
         });

    }
}

2 个答案:

答案 0 :(得分:0)

您没有正确描述您的排序方式。您不仅可以根据评级进行排序,还可以在评级相同时按字母顺序排序。先拿出来。

你忘记了switch()休息; listView.smoothScrollToPosition();

之后的语句

而不是

object.rating=rating; 

我也会写

data[position].rating = rating;

您的变量名称有多糟糕:

public App app_packages[];
App data[]=null;
View app=convertView; 
final App object=data[position];   

尝试让您的代码更具可读性。

答案 1 :(得分:0)

更改

prefEditor.putFloat("rating"+object.title, rb.getRating()).commit();

prefEditor.putFloat("rating"+object.title, rating).commit();

进一步在onRatingChanged中你使用了我认为错误的对象。你不应该采取最后一个,但使用像

这样的东西
object= ratingBar.getTag();

在使用之前

setTag(object);

未经测试。

请改变

static class AppHolder
{ 
    TextView appTitle; 
    RatingBar appScore;
}

static class AppHolder
{ 
    TextView textView; 
    RatingBar ratingBar;
}

因为使用这些不断变化的描述难以阅读您的代码。