任何人都可以告诉我如何在android中排序或排列listview的项目吗? 我已经通过谷歌搜索了它,但我没有找到任何简单的程序来做到这一点。 有没有人至少有一个样本程序来实现这个概念?
建议请..
感谢您宝贵的时间!..
答案 0 :(得分:3)
使用比较器进行排序,如
Collections.sort(data, new Comparator<YourDataModel>() {
@Override
public int compare(YourDataModel data1, YourDataModel data2) {
if( data1.getDistance() < data2.getDistance() )
return 1;
else
return 0;
}
});
数据是包含内部项目的数组。
有关详情,请参阅以下链接
答案 1 :(得分:0)
假设您正在使用数组:
首先定义一个列表和适配器:
ArrayList<String> list;
ArrayAdapter<String> adapter;
然后:
list = new ArrayList<String>(Arrays.asList(your_array_name));
Collections.sort(list);
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, list);
lv.setAdapter(adapter);
lv是你的listview.Sort你的数组并将其绑定在你的适配器中。
答案 2 :(得分:0)
排序布局。
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/overlayLayout"
android:background="#66000000"
android:visibility="visible"
>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@drawable/sorting_shape"
android:layout_marginLeft="@dimen/_9sdp"
android:layout_marginRight="@dimen/_9sdp"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="@dimen/_17sdp"
android:background="@color/colorPrimary"
android:text="SORT BY"
android:gravity="center"
android:textColor="@color/gen_white"
android:id="@+id/textviewsortby"
android:padding="@dimen/_9sdp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center"
android:layout_below="@id/textviewsortby">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Price - High to Low"
android:id="@+id/highToLow"
android:textSize="@dimen/_17sdp"
android:gravity="center"
android:textColor="@color/gen_black"
android:padding="@dimen/_9sdp"/>
<View android:layout_width="match_parent"
android:layout_height="@dimen/view_width_small"
android:background="@color/darkgray"
android:layout_marginLeft="@dimen/_5sdp"
android:layout_marginRight="@dimen/_5sdp"
android:padding="@dimen/_9sdp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Price - Low to High"
android:id="@+id/lowToHigh"
android:textSize="@dimen/_17sdp"
android:gravity="center"
android:textColor="@color/gen_black"
android:padding="@dimen/_9sdp"/>
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
排序的活动代码:
TextView sorting,filter;
RelativeLayout overlayLayout;
TextView highToLow, lowToHigh;
onCreate方法:
overlayLayout = (RelativeLayout) findViewById(R.id.overlayLayout);
overlayLayout.setVisibility(View.GONE);
highToLow = (TextView) findViewById(R.id.highToLow);
lowToHigh = (TextView) findViewById(R.id.lowToHigh);
sorting = (TextView) findViewById(R.id.sorting);
overlayLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
overlayLayout.setVisibility(View.GONE);
}
});
sorting.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
overlayLayout.setVisibility(View.VISIBLE);
}
});
highToLow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.d("hightolow", "hightolow");
sortDescending();
overlayLayout.setVisibility(View.GONE);
}
});
lowToHigh.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.d("lowToHigh", "lowToHigh");
sortAscending();
overlayLayout.setVisibility(View.GONE);
}
});
上升和下降:
private void sortAscending() {
Collections.sort(productList, new AscendingComparator());
rcAdapter.notifyDataSetChanged();
Log.d("hightolow", "sortAscending" + productList);
/*for (ProductModel s : productList){
Log.d("My array list content: ", s.getPrice()+"::"+s.getName());
}*/
}
private void sortDescending() {
Collections.sort(productList, new DescendingComparator());
rcAdapter.notifyDataSetChanged();
Log.d("hightolow", "productList" + productList);
for (ProductModel s : productList) {
Log.d("My array list content: ", s.getPrice() + "::" + s.getName());
}
}
public class AscendingComparator implements Comparator<ProductModel> {
@Override
public int compare(ProductModel p1, ProductModel p2) {
int pM1 = Math.round(Float.parseFloat(p1.getPrice()));
int pM2 = Math.round(Float.parseFloat(p2.getPrice()));
if(pM1 > pM2){
return 1;
}else if((pM1 < pM2)){
return -1;
}else{
return 0;
}
}
}
public class DescendingComparator implements Comparator<ProductModel> {
@Override
public int compare(ProductModel p1, ProductModel p2) {
//Log.d("hightolow","productList"+p2.getPrice().compareTo(p1.getPrice()));
// Log.d("hightolow","productList:"+p1.getPrice());
// Log.d("hightolow","productList::"+p2.getPrice());
int pM1 = Math.round(Float.parseFloat(p1.getPrice()));
int pM2 = Math.round(Float.parseFloat(p2.getPrice()));
if(pM2 > pM1){
return 1;
}else if((pM2 < pM1)){
return -1;
}else{
return 0;
}
}
}