Android Studio使用按钮对自定义列表视图进行排序

时间:2016-10-31 13:35:56

标签: android listview

我正在尝试使用多个值创建自定义列表视图,这些值包含用于排序的按钮。这样,listview可以按照不同的值进行排序,例如年份,名称和价格等。

我对Android Studio和编码一般都很陌生,所以如果有什么事情可以解决,那么我道歉!

问题是,当我按下任何按钮时,没有任何内容被排序。我尝试使用类似的,按数字升序排序,但这似乎不起作用(没有任何错误,没有任何反应)。

这是我尝试过的,首先显示我的自定义行,然后是自定义列表适配器:

我的自定义行布局:     

    <TextView
        android:layout_width="40dp"
        android:layout_height="35dp"
        android:layout_marginLeft="10dp"
        android:text="Time:"
        android:id="@+id/time_label_row"
        android:gravity="center"
        android:layout_weight="1"/>

    <TextView
        android:layout_width="40dp"
        android:layout_height="35dp"
        android:text="time"
        android:id="@+id/time_row"
        android:gravity="center"
        android:layout_weight="1"/>

    <TextView
        android:layout_width="40dp"
        android:layout_height="35dp"
        android:layout_marginLeft="10dp"
        android:text="Price:"
        android:id="@+id/price_label_row"
        android:gravity="center"
        android:layout_weight="1"/>

    <TextView
        android:layout_width="40dp"
        android:layout_height="35dp"
        android:text="price"
        android:id="@+id/price_row"
        android:gravity="center"
        android:layout_weight="1"/>

    <TextView
        android:layout_width="40dp"
        android:layout_height="35dp"
        android:layout_marginLeft="10dp"
        android:text="Year:"
        android:id="@+id/year_label_row"
        android:gravity="center"
        android:layout_weight="1"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="35dp"
        android:text="year"
        android:id="@+id/year_row"
        android:gravity="center"
        android:layout_weight="1"/>

</LinearLayout>

<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="@color/colorPrimaryDark"
    />

我的自定义列表视图(只有这里有兴趣的按钮):

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="50dp">
<Button
    android:text="Time"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/sort_time"
    android:layout_weight="1"
    android:onClick="TimeSortClick"/>

<Button
    android:text="Price"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/sort_price"
    android:layout_weight="1"
    android:onClick="PriceSortClick"/>

<Button
    android:text="Year"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/sort_year"
    android:layout_weight="1"
    android:onClick="YearSortClick"/>

</LinearLayout>

<ListView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/list_view"
    android:smoothScrollbar="true"/>

我的自定义适配器:

public class CustomAdapter extends BaseAdapter {
String [] result1;
String [] result2;
String [] result3;
Context context;

private static LayoutInflater inflater=null;
public CustomAdapter(ListViewActivity mainActivity, String[] priceOrderList, String [] timeOrderList, String [] yearOrderList) {
    // TODO Auto-generated constructor stub
    result1=priceOrderList;
    result2=timeOrderList;
    result3=yearOrderList;
    context=mainActivity;
    inflater = ( LayoutInflater )context.
            getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
    // TODO Auto-generated method stub
    return result1.length;
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return position;
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

public class Holder
{
    TextView tv;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    Holder holder=new Holder();
    View rowView;
    rowView = inflater.inflate(R.layout.row_layout, null);
    holder.tv=(TextView) rowView.findViewById(R.id.price_row);
    holder.tv.setText(result1[position]);
    holder.tv=(TextView) rowView.findViewById(R.id.time_row);
    holder.tv.setText(result2[position]);
    holder.tv=(TextView) rowView.findViewById(R.id.year_row);
    holder.tv.setText(result3[position]);
    rowView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Toast.makeText(context, "You Clicked "+result1[position], Toast.LENGTH_LONG).show();
        }
    });
    return rowView;
}

我的ListView Activitiy(或我为自定义列表视图充气的活动):

public class ListViewActivity extends Activity implements View.OnClickListener {

ListView view_list;
Context context;

ArrayList priceOrder;
public static String [] priceOrderList={"50$","100$","120$","40$"};
public static String [] timeOrderList={"10min","12min","5min","4min"};
public static String [] yearOrderList={"2010","2011","2012","2004"};

private Button mTimeButton;
private Button mPriceButton;
private Button mYearButton;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_list_view);

    context=this;

    mTimeButton = (Button) findViewById(R.id.sort_time);
    mPriceButton = (Button) findViewById(R.id.sort_price);
    mYearButton = (Button) findViewById(R.id.sort_year);
    view_list = (ListView) findViewById(R.id.list_view);
    view_list.setAdapter(new CustomAdapter(this, priceOrderList, timeOrderList, yearOrderList));

    mTimeButton.setOnClickListener(this);
    mPriceButton.setOnClickListener(this);
    mYearButton.setOnClickListener(this);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public void onClick(View v) {
    if (v == mPriceButton){

        String pricesort = ("Sorting for Price");

        Toast.makeText(ListViewActivity.this, pricesort, Toast.LENGTH_SHORT).show();

        Comparator<String> StringAscComparator = new Comparator<String>() {

            public int compare(String app1, String app2) {

                String stringName1 = app1;
                String stringName2 = app2;

                return stringName1.compareToIgnoreCase(stringName2);
            }
        };

        Collections.sort(Arrays.asList(priceOrderList), StringAscComparator);

    } else if (v == mTimeButton){

        String timesort = ("Sorting for Time");

        Toast.makeText(ListViewActivity.this, timesort, Toast.LENGTH_SHORT).show();

        Comparator<String> StringAscComparator = new Comparator<String>() {

            public int compare(String app1, String app2) {

                String stringName1 = app1;
                String stringName2 = app2;

                return stringName1.compareToIgnoreCase(stringName2);
            }
        };

        Collections.sort(Arrays.asList(timeOrderList), StringAscComparator);

    } else if (v == mYearButton){

        String yearsort = ("Sorting for Year");

        Toast.makeText(ListViewActivity.this, yearsort, Toast.LENGTH_SHORT).show();

        Comparator<String> StringAscComparator = new Comparator<String>() {

            public int compare(String app1, String app2) {

                String stringName1 = app1;
                String stringName2 = app2;

                return stringName1.compareToIgnoreCase(stringName2);
            }
        };

        Collections.sort(Arrays.asList(yearOrderList), StringAscComparator);

    }
}

无论如何,谢谢你的帮助!非常感谢!

2 个答案:

答案 0 :(得分:1)

@Selvin在评论中建议使用POJO来关联您的数据,这是一个好主意。最好为您的数据找到合适的数据类型:

    public class Item {

        public BigDecimal price;  // why BigDecimal? http://stackoverflow.com/a/8148773/4504191

        public Duration time;     // this is from the new java.time API in v8

        public int year;          // java.time also has a Year class

    }

一个合适的班级将有私人领域与setter和getters(留给读者练习)。

现在您的适配器中只有一个List<Item>

这些数据类型也会使您的比较逻辑更容易。

有很多不同的方法可以做到这一点。我想我要做的是在适配器上创建一个方法来对列表进行排序,指定一个比较器:

       public void sortUsing(Comparator<Item> comparator) {

           Collections.sort(list, comparator);
           notifyDataSetChanged();
       }

然后我将创建三个不同的比较器 - 每个场一个 - 并在按下相应的排序按钮时传递适当的比较器。这是时间版本:

        Comparator<Item> timeComparator = new Comparator<Item> {

            @Override
            int compare(Item lhs, Item rhs) {
                return lhs.time.compareTo(rhs.time);
            }
        }

答案 1 :(得分:-1)

编写一个方法来对数组进行排序。在你的适配器构造函数中再添加一个参数.on点击btn再次分配适配器就像 view_list.setAdapter(new CustomAdapter(this, priceOrderList, sortArray(timeOrderList), yearOrderList,2)); 基于新参数将值分配到适配器的listview中。

ps:抱歉在这里写评论,低代表:)