如何改变listview的位置?

时间:2016-04-06 07:14:37

标签: android

目前,当用户点击任何标记为已检查的索引列表视图项时,其工作正常。

要求: 但是现在我想在列表顶部显示所有已检查的listview索引,并且在底部都未选中,有人可以帮助我吗?

提前致谢

使用class使用sharepreferance检查列表视图位置,如下所示:

public class ColorSessionManager {

public static ArrayList<Boolean> listBoolTrain = new ArrayList<Boolean>();

private int giftRemaining;

private SharedPreferences prefs;

// Editor for Shared preferences
Editor editor;

// Context
Context _context;

// Shared pref mode
int PRIVATE_MODE = 0;

// Sharedpref file name
private static final String PREF_NAME = "AndroidHivePref";

public ColorSessionManager(Context context) {
    this._context = context;

    prefs = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);

    editor = prefs.edit();
}

public void setNumberOfGits(int numberOfGifts) {
    editor.putInt("numberOfGifts", numberOfGifts);

    editor.commit();
}

public int getNumberOfGits() {
    int nog = prefs.getInt("numberOfGifts", -5);

    return nog;
}

public void initializerBooleans(int arraySiz) {
    int arraySize = prefs.getInt("arraySize", 10);

    for (int x = 0; x < arraySize; x++) {
        editor.putBoolean("Bool" + x, false);

        editor.commit();
    }
}

public void setItemVisited(int x) {
    editor.putBoolean("Bool" + x, true);

    editor.commit();
}

public boolean isItemVisited(int x) {
    return prefs.getBoolean("Bool" + x, false);
}

public int getUnVisitedItemCount() {
    int count = 0;

    int arraySize = prefs.getInt("arraySize", 10);

    for (int x = 0; x < arraySize; x++)// listBoolTrain.size(); x++)
    {
        boolean bol = prefs.getBoolean("Bool" + x, false);

        if (!bol) {
            count++;
        }
    }

    return count;
}

public void remainingGift() {
}

public void setFirstRun(boolean status) {
    editor.putBoolean("firstrun", status);

    editor.commit();
}

public boolean getFirstRun() {
    return prefs.getBoolean("firstrun", true);
}

public void removeAllPreferences() {
    prefs.edit().clear().commit();
}

public void removeKey(String keyName) {
    prefs.edit().remove(keyName).commit();
}

public void showAll() {
    Map<String, ?> keys = prefs.getAll();

    for (Map.Entry<String, ?> entry : keys.entrySet()) {
        Log.d("map values", entry.getKey() + ": "
                + entry.getValue().toString());
    }
}

public void setArraySize(int boolSize) {
    editor.putInt("arraySize", boolSize);

    editor.commit();

    initializerBooleans(boolSize);
}

public int getArraySize() {
    return prefs.getInt("arraySize", -1);
}

public boolean ItemVisited(int position) {
    return prefs.getBoolean("Bool" + position, false);

}

}

1 个答案:

答案 0 :(得分:0)

 @Override
    public void onListItemClick(ListView listview, View view, int position, long id) {
        super.onListItemClick(listview, view, position, id);
        listview.setItemChecked(position, true); 

}

选择已选中和未选中的项目

final SparseBooleanArray checked = getListView().getCheckedItemPositions();
for (int i=0; i < checked.size(); i++) {
            if (checked.valueAt(i)) {
                 .....
            } else {
                 .....
            }
}