我在transporter.sendMail({
from: 'bot@myaddress.be',
to: 'info@myaddress.be',
subject: data.contactEmail,
text: MAILBODY
});
中显示getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
个对象MyBears,我想配置ArrayAdapter
中我的GridView
对象的排序顺序。ArrayAdapter
。
我想订购熊,例如GridView
基于最后一个参数(在本例中为48 - 方法为new myBear(27, "Hi", 48)
)。我该怎么做,我需要把这段代码放在哪里。我试图实现我在其他帖子中看到的内容,但没有一个起作用。
这是我的xml网格出现位置:
myBear.getSize()
这是我将数组适配器与网格连接的地方。
<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
答案 0 :(得分:0)
试试这个。
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);
注意强>
public class MyBear implements Comparable<MyBear>{}
compareTo
方法Collections.sort(myBearsList);
答案 1 :(得分:0)
您可以在 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());
}
});
这将首先对列表进行排序。您可以通过更改比较来反转排序。
答案 2 :(得分:0)
因为我正在使用数据库并且不断在我的ArrayAdapter中添加对象, 1。我需要在MyBear中添加一个排序Comparator,如下所示: public static Comparator MyBearCreatedDateComparator = new Comparator(){
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();