Question

I wrote android app that reads information from database and show on ListView it has ActionBar at top of the page and ButtonBar at bottom, It is worked correct till I press BackButton of my phone and my application minimized. Then I tried to run new one(without put away previous one) by click on Icon of that from list of installed application. when I try to open new one open application but not show any row of database. Just ActionBar at top of the page and the ButtonBar came up and top edge of that fix to bottom edge of ActionBar. how should i fix it? I added code of my first Activity as follow, I will be thanks full if don't minus me if my question is incorrect.

@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;
            }

        });

    }

}
Was it helpful?

Solution

just move the following code from onCreate to onResume. now even if the application got minimized, when opened again the onResume will be triggered and the listView will be populated again

@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);
}

don't forget to remove the lines from your onCreate or you will be populating the listview twice. Hope this work out with you. give me a feedback

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top