如何在降序android中对自定义数组列表适配器进行排序

时间:2018-06-21 07:53:00

标签: java android arrays sorting

我在自定义适配器类中使用了一个4数组列表。我希望它以降序形式显示,具体取决于他们的评分。我用了collection.sort(ratings.Collection.reverseOrder());它的工作原理很好,它根据评级以降序排列评级,但其他数组列表保持不变。我希望根据他们的位置来指定他们。

我正在使用此代码。

Collections.sort(mRatings,Collections.reverseOrder());
                adapterz = new SummaryAdapter(MapsActivity.this, mNames, 
mAddress, mRatings, mDistance);
                recyclerView.setAdapter(adapterz);
                adapterz.notifyDataSetChanged();

我已编辑答案,以明确清除任何人。对不起,我对Java不太熟悉。所以,嗯,你建议在可比性方面做得不好,我尝试了一下,但是效果不好,我该如何处理呢?这是我在数组列表中添加数据的地方。顺便说一句,我正在使用Google附近的地方,这是我将地方详细信息添加到相应的数组列表中。

在我的GetNearbyPlace类中

                else if (MapsActivity.x == "pStore") {
                 for (int i = 0; i < nearbyPlaceList.size(); i++) {
                MarkerOptions markerOptions = new MarkerOptions();
                HashMap<String, String> googlePlace = nearbyPlaceList.get(i);

                 placeName = googlePlace.get("place_name");
                 vicinity = googlePlace.get("vicinity");


                String rating = googlePlace.get("rating");

                double lat = Double.parseDouble(googlePlace.get("lat"));
                double lng = Double.parseDouble(googlePlace.get("lng"));


                String snippet = "Address: " + vicinity + "\n" +
                       // "Phone Number: " + formatted_phone_number + "\n" +
                       // "Website: " + url + "\n" +
                        "Place Rating: " + rating + "\n";

                LatLng latLng = new LatLng(lat, lng);
                markerOptions.position(latLng);

                markerOptions.title(placeName);
                markerOptions.snippet(snippet);








             markerOptions.icon
            (BitmapDescriptorFactory.fromResource(R.drawable.ic_pstore));

                mMap.addMarker(markerOptions);
                mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
                mMap.animateCamera(CameraUpdateFactory.zoomTo(10));

                MapsActivity.mNames.add( googlePlace.get("place_name"));
                MapsActivity.mAddress.add(googlePlace.get("vicinity"));
                int x = 0;
                try {
                    x = Integer.parseInt(googlePlace.get("rating"));
                } catch (NumberFormatException e) {
                    e.printStackTrace();
                }

                float results[] = new float[10];
                Location.distanceBetween(MapsActivity.latitude, 
                 MapsActivity.longitude,lat,lng,results);

                int rate= 0;
                try {
                    rate = new Integer(googlePlace.get("rating"));
                } catch (NumberFormatException e) {
                    e.printStackTrace();
                }

                MapsActivity.mRatings.add(rate);


                MapsActivity.mDistance.add(results[0]);

和我的MainActivity

我将arraylist声明为全局

           public static ArrayList<String> mNames = new ArrayList<>();
           public static ArrayList<String> mAddress = new ArrayList<>();
           public static ArrayList<Integer> mRatings = new ArrayList<>();
           public static ArrayList<Float> mDistance = new ArrayList<>();

//,然后将其添加到适配器中

                    Collections.sort(mDistance);
                     adapterz = new SummaryAdapter(MapsActivity.this, mNames, 
                     mAddress, mRatings, mDistance);

           recyclerView.setAdapter(adapterz);
           adapterz.notifyDataSetChanged();

//我的适配器

      public class SummaryAdapter extends 
      RecyclerView.Adapter<SummaryAdapter.ViewHolder> {

private static final String TAG = "RecyclerViewAdapter";

//vars
private ArrayList<String> mNames = new ArrayList<>();
private ArrayList<String> mAddress = new ArrayList<>();
private ArrayList<Integer> mRatings = new ArrayList<>();
private ArrayList<Float> mDistance = new ArrayList<>();

private Context mContext;

public SummaryAdapter(Context context, ArrayList<String> name, ArrayList<String> address , ArrayList<Integer> ratings , ArrayList<Float> distance  ) {
    this.mNames = name;
    this.mAddress = address;
    this.mRatings = ratings;
    this.mDistance = distance;

    mContext = context;

}



@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.summaryadapter, parent, false);
    return new ViewHolder(view);
}

@Override
public void onBindViewHolder(ViewHolder holder, final int position) {




    holder.name.setText(mNames.get(position));
    holder.address.setText(mAddress.get(position));
    holder.distance.setText("Distance: "+mDistance.get(position)+"meters");


    Toast.makeText(mContext,mImage.toString(),Toast.LENGTH_LONG).show();


    float  w = 0;
    try {
        w = new Float(mRatings.get(position));
    } catch (NumberFormatException e) {
        e.printStackTrace();
    }


    holder.rtnbar.setRating(w);




}

@Override
public int getItemCount() {
    return mNames.size();
}

public class ViewHolder extends RecyclerView.ViewHolder {


    TextView name,address,distance;
    RatingBar rtnbar;
    ImageView img;
    View mView;

    public ViewHolder(View itemView) {
        super(itemView);
        mView = itemView;
        address = itemView.findViewById(R.id.addresslist);
        name = itemView.findViewById(R.id.namelist);
        distance = itemView.findViewById(R.id.distancelist);
        img=itemView.findViewById(R.id.imagelist);
        rtnbar=itemView.findViewById(R.id.ratinglist);
    }
}

}

问题,我该如何将数组列表项切换到自定义类,该类在名称,等级等方面出现在数组列表中?

2 个答案:

答案 0 :(得分:0)

创建一个这样的类:

public class Person implements Comparable<Person> {
    private String mName;
    private String mAddress;
    private int mRating;
    private int mDistance;

    Person(String name, String address, int rating, int distance) {
        this.mName = name;
        this.mAddress = address;
        this.mRating = rating;
        this.mDistance = distance;
    }

    @Override
    public int compareTo(Person p) {
        return -Integer.valueOf(mRating).compareTo(p.mRating);
    }
}

并将所有数据存储在Person对象的1个数组persons中。 然后,对数组进行排序:Arrays.sort(persons);并在适配器中使用它。

答案 1 :(得分:0)

用一个类定义所有变量,例如rating,name等,然后将该类传递到列表中,然后将该列表传递到适配器中。  之后,如果您的评级值很长,那么perfrom降序排序如下..

        Collections.sort(indexResponsesList, new Comparator<UserData>() {
        @Override
        public int compare(UserData userData, UserData t1) {
            Long idea1 = new Long(userData.getCreatedAt());// here pass rating value.
            Long idea2 = new Long(t1.getCreatedAt());// here pass rating value.
            return idea2.compareTo(idea1);
        }
    });
    if (indexItemAdapter != null)
        indexItemAdapter.notifyDataSetChanged();

如果整数则返回ab,然后将Long数据类型替换为整数。