我在 GridView 中使用带有列表的 ArrayAdapter。如何配置为基于我的 ArrayAdapter 中的对象变量进行排序?

I am using ArrayAdapter with a List in a GridView. How can I configure to sort based on object variables from my ArrayAdapter?

我正在 GridView 中显示 ArrayAdapter 个对象 MyBears,我想配置 ArrayAdapter 中的对象在 [=14= 中的排序顺序]. 我想订购熊,例如new myBear(27, "Hi", 48) 基于最后一个参数(在本例中为 48 - 方法是 myBear.getSize())。我该怎么做以及我需要将此代码放在哪里。我尝试实现我在其他帖子中看到的内容,但 none 有效。

这是我的 xml 出现网格的地方:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:background="@drawable/background_Bears"
    android:orientation="vertical”>
    <GridView
        android:id="@+id/my_Bears_in_my_collection_grid_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:divider="@android:color/transparent"
        android:gravity="center"
        android:numColumns="4"
        android:visibility="visible"
        tools:griditem="@layout/one_my_Bear_in_my_collection_display" />
</LinearLayout>

这是我将阵列适配器与网格连接的地方。

public class MyCollectionFragment extends Fragment {


    private GridView mMyBearGridView;

    @Override
    public View onCreateView (LayoutInflater inflater, @Nullable final ViewGroup container, @Nullable Bundle savedInstanceState){
        View v = inflater.inflate(R.layout.my_collection,container,false);

        myBearsList = new ArrayList<>();
        mMyBearsAdapter = new MyBearsMyCollectionAdapter(this, R.layout.one_my_Bear_in_my_collection_display, myBearsList);
        mMyBearsAdapter.add(new myBear(27, "Hi", 48));
        mMyBearsAdapter.add(new myBear(4, "Hi", 87));
        mMyBearsAdapter.add(new myBear(6, "Hi", 39));
        mMyBearsAdapter.add(new myBear(8, "Hallo", 90));
        mMyBearsAdapter.add(new myBear(0, "Hi", 28));
        mMyBearsAdapter.add(new myBear(8, "Bye", 54));
        //the grid view from this fragment
        mMyBearGridView = (GridView) v.findViewById(R.id.my_Bears_in_my_collection_grid_view);
        mMyBearGridView.setAdapter(mMyBearsAdapter);

        return v;
    }

} 

这是我的 ArrayAdapter:

public class MyBearsMyCollectionAdapter extends ArrayAdapter<MyBear> {
    private Context context;

    public MyBearsMyCollectionAdapter(Context context, int resource, List<MyBear> objects) {
        super(context, resource, objects);
        this.context = context;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = ((Activity) getContext()).getLayoutInflater().inflate(R.layout.one_my_Bear_in_my_collection_display, parent, false);
        }

        LinearLayout myBearsMyCollectionLinearLayout = (LinearLayout) convertView.findViewById(R.id.my_Bear_linear_layout);
        ImageView myBearImage = (ImageView) convertView.findViewById(R.id.my_Bear_image);

        final MyBear myBear = getItem(position);

        boolean isPhoto = myBear.getBearImageUrl() != null;
        if (isPhoto) {
            Glide.with(myBearImage.getContext())
                    .load(myBear.getBearImageUrl())
                    .into(myBearImage);
        }

        myBearsMyCollectionLinearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                BearPopup BearPopup = BearPopup.newInstance(myBear);
                FragmentManager manager = ((MainActivity) context).getSupportFragmentManager();
                BearPopup.show(manager, "BearPopup");
            }
        });
        return convertView;
    }

}

试试这个。

public class MyBear implements Comparable<MyBear>{  

    private int size;

    @Override  
    public int compareTo(MyBear mBear) {  
        //Edited here with your get value
        int i = this.size - mBear.getSize();

        return i;  
    }  
}

然后添加到您的java代码

    myBearsList = new ArrayList<>();
    myBearsList.add(new myBear(27, "Hi", 48));
    myBearsList.add(new myBear(4, "Hi", 87));
    myBearsList.add(new myBear(6, "Hi", 39));
    myBearsList.add(new myBear(8, "Hallo", 90));
    myBearsList.add(new myBear(0, "Hi", 28));
    myBearsList.add(new myBear(8, "Bye", 54));
    // edited here , add Collections
    Collections.sort(myBearsList);
    mMyBearsAdapter = new MyBearsMyCollectionAdapter(this, R.layout.one_my_Bear_in_my_collection_display, myBearsList);

备注

  1. public class MyBear implements Comparable<MyBear>{}
  2. 使用compareTo方法
  3. 使用Collections.sort(myBearsList);

您可以像这样在 MyBear pojo 中实现 Comparable

class MyBear implements Comparable<MyBear> {
   ... 
   int size;
   ...
   // Override compareTo method
   public int compareTo(MyBear myBear) {
     return this.size - myBear.getSize();
   }
   ...
}

然后使用Collections.sort()排序:

Collections.sort(myBearsList);  

或者你可以通过使用 Collection.sort() 像这样的东西来使用懒惰的方式:

Collections.sort(myBearsList, new Comparator<MyBear>() {
        @Override public int compare(MyBear o1, MyBear o2) {
          return NumberUtils.compare(o2.getSize(), o1.getSize());
        }
      });

这将首先对大小最大的列表进行排序。您可以通过更改比较来反转排序。

因为我正在使用数据库并不断在我的 ArrayAdapter 中添加对象,1. 我需要在 MyBear 中添加一个排序比较器,如下所示: public 静态比较器 MyBearCreatedDateComparator =新比较器(){

    public int compare(MyBear myBear1, MyBear myBear2) {

        Long MyBearCreatedDate1 = myBear1.getBearCreatedDate();
        Long MyBearCreatedDate2 = myBear2.getBearCreatedDate();

        return MyBearCreatedDate2.compareTo(MyBearCreatedDate1);
    }

};

2. 每次我在 ArrayAdapter 中添加一个新对象时,我都需要调用排序 3. 并调用notifyDataSetChanged() 以便我的 GridView/ListView 刷新。像这样:

mMyBearsAdapter.add(myBear);
                        mMyBearsAdapter.sort(MyBear.MyBearCreatedDateComparator);
                        mMyBearsAdapter.notifyDataSetChanged();