Note that there are some explanatory texts on larger screens.

plurals
  1. PORefreshing a Fragment after the tab has been selected
    primarykey
    data
    text
    <p>When my fragment is built, I build the View from information in my local Database. Other tabs can modify this information, and when the user selects the tab, I would like the new information to be reflected.</p> <pre><code>public class RunningTotal extends SherlockFragment { public static final String TAG = "Running Total"; private LinearLayout lv; public View onCreateView(LayoutInflater li, ViewGroup vg, Bundle savedInstanceState) { SalesDataSource sds = BarcodeSaleTracker.SDS; ArrayList&lt;Item&gt; items = sds.getAllItems(); String[] totals = sds.getPersonValues(); int totalsPosition = 0; Log.v(TAG, "Items Length: " + items.size()); lv = new LinearLayout(this.getActivity()); String lastPerson = ""; if (items.size() &gt; 0) { for (Item i : items) { if (lastPerson.equalsIgnoreCase(i.getPerson()) == false) { lastPerson = i.getPerson(); TextView tv = (TextView) li.inflate(R.layout.list_title, lv, false); tv.setText(totals[totalsPosition]); totalsPosition++; lv.addView(tv); } TextView listItem = (TextView) li.inflate(R.layout.list_item, lv, false); listItem.setText(i.toString()); lv.addView(listItem); } } else { TextView noItems = (TextView) li.inflate(R.layout.list_title, lv, false); noItems.setText(R.string.no_items); lv.addView(noItems); } return lv; } } </code></pre> <p>It's a tabbed format, nearly identical to the Sherlock example:</p> <pre><code>public class BarcodeSaleTracker extends SherlockFragmentActivity { TabHost mTabHost; ViewPager mViewPager; TabsAdapter mTabsAdapter; public static SalesDataSource SDS; public BarcodeSaleTracker() { } protected void onCreate(Bundle savedInstanceState) { setTheme(R.style.Sherlock___Theme); super.onCreate(savedInstanceState); SDS = new SalesDataSource(this); SDS.open(); if (savedInstanceState == null) { setContentView(R.layout.fragment_tabs_pager); mTabHost = (TabHost) findViewById(android.R.id.tabhost); mTabHost.setup(); mViewPager = (ViewPager) findViewById(R.id.pager); mTabsAdapter = new TabsAdapter(this, mTabHost, mViewPager); mTabsAdapter.addTab(mTabHost.newTabSpec("current_sale") .setIndicator("Current Sale"), Current_Sale.class, null); mTabsAdapter.addTab(mTabHost.newTabSpec("running_total") .setIndicator("Running Total"), RunningTotal.class, null); mTabsAdapter.addTab( mTabHost.newTabSpec("stats").setIndicator("Stats"), CountingFragment.class, null); } else { mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab")); } } </code></pre> <p>I just don't know <em>how</em> to communicate to RunningTotal that it needs to update itself.</p> <p><strong>EDIT</strong> Added the source of all the data I ask from the database</p> <pre><code>public class SalesDataSource { private SQLiteDatabase database; private DatabaseHelper dbHelper; private String[] allColumns = { DatabaseHelper.COLUMN_ID, DatabaseHelper.COLUMN_PERSON, DatabaseHelper.COLUMN_COST, DatabaseHelper.COLUMN_ITEM }; public SalesDataSource(Context context) { dbHelper = new DatabaseHelper(context); } public void open() throws SQLException { database = dbHelper.getWritableDatabase(); } public void close() { dbHelper.close(); } /** * Creates an entry in the Database. Both a person and a cost are required. * Item is not required. If one is not needed, simply pass null. * * @param person * Who this sale belongs to. * @param cost * The amount (in pennies) that the sale was. * @param item * An optional description of the sold item * @return The newly created Item. */ public Item addItem(String person, int cost, String item) { ContentValues values = new ContentValues(); values.put(DatabaseHelper.COLUMN_PERSON, person); values.put(DatabaseHelper.COLUMN_COST, cost); values.put(DatabaseHelper.COLUMN_ITEM, item); long insertId = database.insert(DatabaseHelper.TABLE_SALES, null, values); Cursor cursor = database.query(DatabaseHelper.TABLE_SALES, allColumns, DatabaseHelper.COLUMN_ID + " = " + insertId, null, null, null, null); cursor.moveToFirst(); Item rv = cursorToItem(cursor); cursor.close(); return rv; } public void addItems(List&lt;Item&gt; items) { for (Item i : items) { ContentValues values = new ContentValues(); values.put(DatabaseHelper.COLUMN_PERSON, i.getPerson()); values.put(DatabaseHelper.COLUMN_COST, i.getAmount()); values.put(DatabaseHelper.COLUMN_ITEM, i.getItem()); database.insert(DatabaseHelper.TABLE_SALES, null, values); } } public ArrayList&lt;Item&gt; getAllItems() { ArrayList&lt;Item&gt; items = new ArrayList&lt;Item&gt;(); Cursor cursor = database.query(DatabaseHelper.TABLE_SALES, allColumns, null, null, null, null, DatabaseHelper.COLUMN_PERSON); cursor.moveToFirst(); while (!cursor.isAfterLast()) { Item item = cursorToItem(cursor); items.add(item); cursor.moveToNext(); } cursor.close(); return items; } public String[] getPersonValues() { Cursor cursor = database.query(DatabaseHelper.TABLE_SALES, new String[] { "SUM(" + DatabaseHelper.COLUMN_COST + ") AS " + DatabaseHelper.PERSON_SUM, DatabaseHelper.COLUMN_PERSON }, null, null, DatabaseHelper.COLUMN_PERSON, null, null); String[] rv = new String[cursor.getCount()]; cursor.moveToFirst(); int pos = 0; while (!cursor.isAfterLast()) { String person = cursor.getString(1); String money = Item.format(cursor.getInt(0)); rv[pos++] = person + ": " + money; cursor.moveToNext(); } cursor.close(); return rv; } public Item getMaxSale() { Cursor cursor = database.query(DatabaseHelper.TABLE_SALES, new String[] { "MAX(" + DatabaseHelper.COLUMN_COST + ") AS " + DatabaseHelper.MAX_SALE, DatabaseHelper.COLUMN_PERSON }, null, null, null, null, null); cursor.moveToFirst(); Item rv = cursorToItem(cursor); cursor.close(); return rv; } public Item getMinSale() { Cursor cursor = database.query(DatabaseHelper.TABLE_SALES, new String[] { "MIN(" + DatabaseHelper.COLUMN_COST + ") AS " + DatabaseHelper.MIN_SALE, DatabaseHelper.COLUMN_PERSON }, null, null, null, null, null); cursor.moveToFirst(); Item rv = cursorToItem(cursor); cursor.close(); return rv; } private Item cursorToItem(Cursor cursor) { Item item = new Item(); item.setId(cursor.getLong(0)); item.setPerson(cursor.getString(1)); item.setAmount(cursor.getInt(2)); item.setItem(cursor.getString(3)); return item; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload