我的应用程序包含所有instaled应用程序的ListView(TextView和RatingBar),并允许根据排名(在菜单中选择)对ListView进行排序。 这意味着,在从菜单sort-> ranking->降序f.e.中选择选项后,ListView包含已安装的应用程序和排名的名称,应根据排名按降序排序。问题是ListView没有完全排序。它工作,但没有很少的元素,但..滚动屏幕后是好的。我能得到一些线索吗?
我注意到,当适配器被排序时,只有ListView的可见项具有正确的排名,其他的则等于零。这是为什么?
一段代码:
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;
}
});
}
}
答案 0 :(得分:0)
在运行时对列表项进行排序时,需要调用 adapter.notifyDataSetChanged()方法。