0
我的應用程序包含所有安裝的應用程序(TextView和RatingBar)的ListView,並允許根據排名(在菜單中選擇)排序ListView的元素。 這意味着,從菜單sort-> ranking-> descending f.e.中選擇選項後,ListView中包含已安裝的應用程序的名稱和排名,應按排名降序排列。問題是ListView沒有完全排序。它的作品,但沒有幾個元素,但..後滾動屏幕是好的。我能得到一些線索嗎?通過RatingBar排序自己的適配器無法正常工作
我注意到,當適配器進行排序時,只有可見的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;
}
});
}
}
不太清楚我應該改進什麼。你能描述它到底在哪裏嗎? – user3590445