构建片段时,我会根据本地数据库中的信息构建View。其他选项卡可以修改此信息,当用户选择选项卡时,我希望反映新信息。
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<Item> 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() > 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;
}
}
这是一种标签格式,几乎与Sherlock示例相同:
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"));
}
}
我只是不知道如何与RunningTotal进行通信,它需要自行更新。
编辑添加了我从数据库中提出的所有数据的来源
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<Item> 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<Item> getAllItems() {
ArrayList<Item> items = new ArrayList<Item>();
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;
}
}
答案 0 :(得分:1)
为什么不使用listfragment和loadermanager来管理游标对象,以便每当基础数据发生变化时列表会自动更新?