当我有一个最小化时,我应该如何处理我的应用程序?

时间:2014-05-11 09:45:18

标签: android database android-listview back-button back-button-control

我编写的android应用程序从数据库中读取信息并在ListView上显示,页面顶部有ActionBar,底部有ButtonBar,直到我按BackButton为止我的手机和我的应用程序最小化然后我尝试通过单击已安装应用程序列表中的图标来运行新的(不放弃前一个)。当我尝试打开新的一个打开的应用程序但不显示任何数据库行。只需ActionBar位于页面顶部,ButtonBar就会出现,并且该修复的上边缘会移至ActionBar的下边缘。我应该怎么解决? 我添加了我的第一个Activity的代码如下,如果我的问题不正确,我会感谢你们,如果不是我的话,我会感激不尽。

@SuppressLint({ "NewApi", "ServiceCast" })
public class Index extends Activity implements OnItemClickListener {

    private final static String EXTRA_MASSAGE = "";
    private final static String MESSAGE_TO_SETTING = null;

    // add for data base
    private static final String DB_NAME = "amam-khmini.gdb";

    // database
    private static final String TABLE_NAME = "cat";
    private static final String CAT_ID = "_id";
    private static final String CAT_TEXT = "text";

    private static final String TABLE_NAME2 = "font";
    private static final String FONT_ID = "_id";
    private static final String FONT_TYPE = "font_type";
    private static final String FONT_SIZE = "font_size";

    private SQLiteDatabase database;
    static ListView listView;
    List<RowItem> rowItems;
    private ArrayList<String> poemtype;
    private ArrayList<Integer> ids;

    private String fontName;
    private int fontSize;
    // end of database

    static CustomListViewAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ActionBar actionBar = getActionBar();
        actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);

        // set custom view
        View actionBarView = getLayoutInflater().inflate(R.layout.actionbar,
                null);

        ActionBar.LayoutParams params = new ActionBar.LayoutParams(
                LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        actionBar.setCustomView(actionBarView, params);

        // Hide the home icon
        actionBar.setIcon(android.R.color.transparent);
        actionBar.setLogo(android.R.color.transparent);

        TextView title = (TextView) findViewById(R.id.title);

        setContentView(R.layout.activity_index);

        // add for database
        ExternalDbOpenHelper dbOpenHelper = new ExternalDbOpenHelper(this,
                DB_NAME);

        database = dbOpenHelper.openDataBase();
        fillIndex();

        getFont();

        rowItems = new ArrayList<RowItem>();
        for (int i = 0; i < poemtype.size(); i++) {
            RowItem item = new RowItem(poemtype.get(i));
            rowItems.add(item);
        }

        listView = (ListView) findViewById(R.id.list);
        adapter = new CustomListViewAdapter(this, R.layout.indexitem_row,
                rowItems);

        adapter.setFontType(fontName);
        adapter.setFontize(fontSize);

        listView.setAdapter(adapter);
        listView.setOnItemClickListener(this);

    }

    @Override
    public void onDestroy() {
        listView.setAdapter(null);
        super.onDestroy();
    }

    private void getFont() {
        // TODO Auto-generated method stub
        String[] tableColumns = new String[] { FONT_TYPE, FONT_SIZE };
        String whereClause = FONT_ID + " LIKE ?";
        String[] whereArgs = new String[] { String.valueOf(1) };
        Cursor fontCursor = database.query(TABLE_NAME2, tableColumns,
                whereClause, whereArgs, null, null, FONT_ID);
        fontCursor.moveToFirst();
        if (!fontCursor.isAfterLast()) {
            fontName = fontCursor.getString(0);
            fontSize = fontCursor.getInt(1);
        }
        Log.d("type of font in getFont Index", fontName);
        Log.d("size of font in getFont Index", Integer.toString(fontSize));
        fontCursor.close();
    }

    private void fillIndex() {
        poemtype = new ArrayList<String>();
        ids = new ArrayList<Integer>();

        String[] tableColumns = new String[] { CAT_ID, CAT_TEXT };
        String whereClause = "PARENT_ID = ?";
        String[] whereArgs = new String[] { "6001" };
        Cursor poetCursor = database.query(TABLE_NAME, tableColumns,
                whereClause, whereArgs, null, null, CAT_ID);

        poetCursor.moveToFirst();
        if (!poetCursor.isAfterLast()) {
            do {
                Integer id = poetCursor.getInt(0);
                String name = poetCursor.getString(1);
                poemtype.add(name);
                ids.add(id);
            } while (poetCursor.moveToNext());
        }
        poetCursor.close();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {

        // TODO Auto-generated method stub

        Toast toast = Toast.makeText(getApplicationContext(), "Item "
                + (position + 1) + ": " + rowItems.get(position),
                Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
        toast.show();

        Intent intent = new Intent(this, PoemType.class);
        String[] extra = { ids.get(position).toString(),
                poemtype.get(position).toString() };

        intent.putExtra(EXTRA_MASSAGE, extra);
        startActivity(intent);
    }

    public void showSettings(View view) {
        Intent intent = new Intent(this, Options.class);
        String message = "Index";
        intent.putExtra(MESSAGE_TO_SETTING, message);
        startActivity(intent);
    }

    public static void setFont(String fontName1, int fontSize1) {
        // TODO Auto-generated method stub
        adapter.setFontType(fontName1);
        adapter.setFontize(fontSize1);
        listView.setAdapter(adapter);
    }

    public void showFontSetting(final View view) {
        PopupMenu popup = new PopupMenu(this, view);
        MenuInflater inflater = popup.getMenuInflater();
        inflater.inflate(R.menu.menu, popup.getMenu());
        popup.show();

        popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {

            @Override
            public boolean onMenuItemClick(MenuItem item) {
                // TODO Auto-generated method stub
                String message = "Index";
                switch (item.getItemId()) {
                case R.id.item1:
                    Toast.makeText(Index.this,
                            "You Clicked : " + item.getTitle(),
                            Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(view.getContext(),
                            IndexFont.class);

                    intent.putExtra(MESSAGE_TO_SETTING, message);
                    startActivity(intent);
                    break;
                case R.id.item2:
                    Intent intent2 = new Intent(view.getContext(),
                            HemistichFont.class);
                    intent2.putExtra(MESSAGE_TO_SETTING, message);
                    startActivity(intent2);
                    break;
                }
                return true;
            }

        });

    }

}

1 个答案:

答案 0 :(得分:1)

将以下代码从onCreate移至onResume。现在即使应用程序被最小化,再次打开时,onResume将被触发,listView将再次填充

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();


ExternalDbOpenHelper dbOpenHelper = new ExternalDbOpenHelper(this,
            DB_NAME);

    database = dbOpenHelper.openDataBase();
    fillIndex();

    getFont();

    rowItems = new ArrayList<RowItem>();
    for (int i = 0; i < poemtype.size(); i++) {
        RowItem item = new RowItem(poemtype.get(i));
        rowItems.add(item);
    }

    listView = (ListView) findViewById(R.id.list);
    adapter = new CustomListViewAdapter(this, R.layout.indexitem_row,
            rowItems);

    adapter.setFontType(fontName);
    adapter.setFontize(fontSize);
    listView.setAdapter(adapter);
    listView.setOnItemClickListener(this);
}

不要忘记删除onCreate中的行,否则您将填充列表视图两次。 希望这项工作与您合作。给我一个反馈