Android listview按字母顺序排列

时间:2015-06-14 09:54:59

标签: android listview

我在listview中有一个listview .item来自数据库。问题是该项目没有按字母顺序排列。帮助我解决我的问题。这是我的listview活动代码

这是datalist活动的代码。

public class DataListActivity extends Activity  {

ListView listView;
SQLiteDatabase sqLiteDatabase;
FoodDbHelper foodDbHelper;
Cursor cursor;
ListDataAdapter listDataAdapter;
private Button button1;
ListDataAdapter dataAdapter = null;
Button button;
DataProvider dataProvider;
ArrayList<HashMap<String, String>> namessList;
EditText inputSearch;
String search_name;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.data_list_layout);

    listView = (ListView) findViewById(R.id.list_View);

    listDataAdapter = new ListDataAdapter(getApplicationContext(),
            R.layout.row_layout) {

        @Override
        protected void showCheckedButton(int position, boolean value) {
            // TODO Auto-generated method stub

            DataProvider item = (DataProvider) listDataAdapter
                    .getItem(position);
            Log.i("", "");

            item.setSelected(value);

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

                @Override
                public void onClick(View v) {

                    StringBuffer responseText = new StringBuffer();
                    responseText
                           .append("The following dishes were selected...\n");

                    ArrayList<DataProvider> list = listDataAdapter
                            .getSelectedIndexes();

                    int sum = 0;
                    for (int i = 0; i < list.size(); i++) {
                        DataProvider dataProvider = list.get(i);

                        sum = sum + dataProvider.getCalorie();

                        responseText.append("\n" + dataProvider.getName()
                                + " : " + dataProvider.getCalorie()
                                + " kcal"
                                );

                    }

                    Toast.makeText(getApplicationContext(), ""+responseText+"\n"+"................................."
                                   +"\n"+"Total Calories In Your Menu Is : " +sum,
                            Toast.LENGTH_LONG).show();

                }
            });

        }               
    };
    listView.setAdapter(listDataAdapter);
    foodDbHelper = new FoodDbHelper(getApplicationContext());
    sqLiteDatabase = foodDbHelper.getReadableDatabase();
    cursor = foodDbHelper.getInformations(sqLiteDatabase);
    if (cursor.moveToFirst()) {
        do {
            String name, quantity, fat, protein, sugar, vitamins;
            boolean selected = false;
            String names = null;
            Integer calorie;

            name = cursor.getString(0);
            quantity = cursor.getString(1);
            calorie = Integer.valueOf(cursor.getString(2));
            fat = cursor.getString(3);
            protein = cursor.getString(4);
            sugar = cursor.getString(5);
            vitamins = cursor.getString(6);

            DataProvider dataProvider = new DataProvider(name, quantity,
                    calorie, fat, protein, sugar, vitamins, names, selected);

            listDataAdapter.add(dataProvider);

        } while (cursor.moveToNext());


    }

这是dbhelper类

public Cursor getInformations(SQLiteDatabase db){
    Cursor cursor;
    String[] projections = {Food.NewDishInfo.DISH_NAME,Food.NewDishInfo.DISH_QUANTITY,
            Food.NewDishInfo.DISH_CALORIE,Food.NewDishInfo.DISH_FAT,Food.NewDishInfo.DISH_PROTEIN,
            Food.NewDishInfo.DISH_SUGAR, Food.NewDishInfo.DISH_VITAMINS};
    cursor= db.query(Food.NewDishInfo.TABLE_NAME,projections,null,null,null,null,null);
    return cursor;
}

public Cursor getFood(String dish_name,SQLiteDatabase sqLiteDatabase)
{
    String[] projections = { Food.NewDishInfo.DISH_QUANTITY, Food.NewDishInfo.DISH_CALORIE, Food.NewDishInfo.DISH_FAT,
            Food.NewDishInfo.DISH_PROTEIN, Food.NewDishInfo.DISH_SUGAR, Food.NewDishInfo.DISH_VITAMINS};
    String selection = Food.NewDishInfo.DISH_NAME+" LIKE ?";
    String[] selection_args = {dish_name};
    Cursor cursor = sqLiteDatabase.query(Food.NewDishInfo.TABLE_NAME,projections,selection,selection_args,null,null,null);

    return cursor;

}

1 个答案:

答案 0 :(得分:2)

您必须在查询中添加订单:

public Cursor getInformations(SQLiteDatabase db){
    Cursor cursor;
    String[] projections = {Food.NewDishInfo.DISH_NAME,Food.NewDishInfo.DISH_QUANTITY,
            Food.NewDishInfo.DISH_CALORIE,Food.NewDishInfo.DISH_FAT,Food.NewDishInfo.DISH_PROTEIN,
            Food.NewDishInfo.DISH_SUGAR, Food.NewDishInfo.DISH_VITAMINS};
    cursor= db.query(Food.NewDishInfo.TABLE_NAME,projections,null,null,null,null, "column_name ASC");
    return cursor;
}

public Cursor getFood(String dish_name,SQLiteDatabase sqLiteDatabase)
{
    String[] projections = { Food.NewDishInfo.DISH_QUANTITY, Food.NewDishInfo.DISH_CALORIE, Food.NewDishInfo.DISH_FAT,
            Food.NewDishInfo.DISH_PROTEIN, Food.NewDishInfo.DISH_SUGAR, Food.NewDishInfo.DISH_VITAMINS};
    String selection = Food.NewDishInfo.DISH_NAME+" LIKE ?";
    String[] selection_args = {dish_name};
    Cursor cursor = sqLiteDatabase.query(Food.NewDishInfo.TABLE_NAME,projections,selection,selection_args,null,null, "column_name ASC");

    return cursor;

}

ASC表示升序,DESC表示降序。