按下按钮时如何进行排序

时间:2014-02-25 17:37:13

标签: android sqlite

我想按一个按钮,然后,从sqlite检索的数据将被排序并显示在ListView上。我怎么能这样做?

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_shopping);

    sdb = new ShoppingDatabaseHandler(this) ;

    List<Shops> shops = sdb.getAllShops();        
    int count = 0; 
    for ( Shops sp: shops) {

        count++;

    }

    if (count == 0 ){
    sdb.addShopping(new Shops(" Car Market ", " 27454547 ", " 7 ")); 
    sdb.addShopping(new Shops(" ABC MARKET ", " 27872514 ", " 6 ")); 
    sdb.addShopping(new Shops(" Fortress ", " 27454547 ", " 7 ")); 
    }


    Button name = (Button) findViewById(R.id.name);
    name.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            sdb.shopsOrderbyName();
            nameSorting();


        }
    });


    ArrayAdapter<Shops> adapter = new ArrayAdapter<Shops>(this, android.R.layout.simple_list_item_1, shops);
    setListAdapter(adapter);

}


  @Override
  protected void onPause() {
    sdb.close();
    super.onPause();
  }

  public void nameSorting (){

      List<Shops> shops = sdb.getAllShops();     
      ArrayAdapter<Shops> adapter = new ArrayAdapter<Shops>(this,android.R.layout.simple_list_item_1, shops);
      shoppingInfo.setAdapter(adapter);
      adapter.notifyDataSetChanged();

  }


public void shopsOrderbyName (){
    SQLiteDatabase db = this.getWritableDatabase();
    db.query(TABLE_SHOP, new String[] { KEY_NAME , KEY_NUMBER , KEY_LOCATION }, null, null, null, null, KEY_NAME+ " ASC");

}

因此,在进行排序时出错。谢谢你的帮助。

我做了一些改变。我是对的吗?

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_shopping);

    sdb = new ShoppingDatabaseHandler(this) ;

    final List<Shops> shops = sdb.getAllShops();        
    int count = 0; 
    for ( Shops sp: shops) {

        count++;

    }

    if (count == 0 ){
    sdb.addShopping(new Shops(" Car Market ", " 27454547 ", " 7 ")); 
    sdb.addShopping(new Shops(" ABC MARKET ", " 27872514 ", " 6 ")); 
    sdb.addShopping(new Shops(" Fortress ", " 27454547 ", " 7 ")); 
    }

    final ArrayAdapter<Shops> adapter = new ArrayAdapter<Shops>(this, android.R.layout.simple_list_item_1, shops);
    Button name = (Button) findViewById(R.id.name);
    name.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {


            adapter.clear();
            sdb.shopsOrderbyName();
            nameSorting();
            //List<Shops> sortedShops = sdb.shopsOrderbyName();   
            adapter.addAll(shops);

        }
    });


    //final ArrayAdapter<Shops> adapter = new ArrayAdapter<Shops>(this, android.R.layout.simple_list_item_1, shops);
    setListAdapter(adapter);

}

但是在addall(&lt;&gt;)上有一个错误,显示一条名为“Call requires API level 11”的消息。那么,我能用它做什么呢?

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

好的,我想我已经明白了。如果是这样,可以按照以下方式完成:

public class Test extends ListActivity {

    ArrayAdapter<Shops> adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shopping);
        sdb = new ShoppingDatabaseHandler(this) ;
        Button name = (Button) findViewById(R.id.name);
        name.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                ArrayList<Shops> shops = sdb.getAllShops();
                Collections.sort(shops, new Comparator<Shops>() {
                    @Override
                    public int compare(Shops lhs, Shops rhs) {
                        return <sort criteria>;
                    }
                });
                adapter = new ArrayAdapter<Shops>(this, android.R.layout.simple_list_item_1, shops);
                getListView().setAdapter(adapter);

            }
        });

    }

}