在Fragment函数中getActivity()返回null(getActivity() returns null in Fragment function)
我有一个这样的公共方法的片段(F1)
public void asd() { if (getActivity() == null) { Log.d("yes","it is null"); } }
是的,当我打电话给它(从活动),它是空的...
FragmentTransaction transaction1 = getSupportFragmentManager().beginTransaction(); F1 f1 = new F1(); transaction1.replace(R.id.upperPart, f1); transaction1.commit(); f1.asd();
它必须是我做错的事情,但我不知道是什么
I have a fragment (F1) with a public method like this
public void asd() { if (getActivity() == null) { Log.d("yes","it is null"); } }
and yes when I call it (from the Activity), it it is null...
FragmentTransaction transaction1 = getSupportFragmentManager().beginTransaction(); F1 f1 = new F1(); transaction1.replace(R.id.upperPart, f1); transaction1.commit(); f1.asd();
It must be something that I am doing very wrong, but I don't know what that is
原文:https://stackoverflow.com/questions/6215239
最满意答案
首先,我们来做一些测试数据:
create table client (client_id integer not null primary key auto_increment, name varchar(64)); create table portfolio (portfolio_id integer not null primary key auto_increment, client_id integer references client.id, cash decimal(10,2), stocks decimal(10,2)); insert into client (name) values ('John Doe'), ('Jane Doe'); insert into portfolio (client_id, cash, stocks) values (1, 11.11, 22.22), (1, 10.11, 23.22), (2, 30.30, 40.40), (2, 40.40, 50.50);
如果您不需要投资组合ID,那将很容易:
select client_id, name, max(cash + stocks) from client join portfolio using (client_id) group by client_id +-----------+----------+--------------------+ | client_id | name | max(cash + stocks) | +-----------+----------+--------------------+ | 1 | John Doe | 33.33 | | 2 | Jane Doe | 90.90 | +-----------+----------+--------------------+
由于您需要投资组合ID,事情变得更加复杂。 让我们一起做 首先,我们将编写一个返回每个客户端的最大投资组合值的子查询:
select client_id, max(cash + stocks) as maxtotal from portfolio group by client_id +-----------+----------+ | client_id | maxtotal | +-----------+----------+ | 1 | 33.33 | | 2 | 90.90 | +-----------+----------+
然后,我们将查询投资组合表,但是使用前一个子查询的连接,以便仅保留其总值为客户端最大值的投资组合:
select portfolio_id, cash + stocks from portfolio join (select client_id, max(cash + stocks) as maxtotal from portfolio group by client_id) as maxima using (client_id) where cash + stocks = maxtotal +--------------+---------------+ | portfolio_id | cash + stocks | +--------------+---------------+ | 5 | 33.33 | | 6 | 33.33 | | 8 | 90.90 | +--------------+---------------+
最后,我们可以加入客户端表(如你所做),以便包括每个客户端的名称:
select client_id, name, portfolio_id, cash + stocks from client join portfolio using (client_id) join (select client_id, max(cash + stocks) as maxtotal from portfolio group by client_id) as maxima using (client_id) where cash + stocks = maxtotal +-----------+----------+--------------+---------------+ | client_id | name | portfolio_id | cash + stocks | +-----------+----------+--------------+---------------+ | 1 | John Doe | 5 | 33.33 | | 1 | John Doe | 6 | 33.33 | | 2 | Jane Doe | 8 | 90.90 | +-----------+----------+--------------+---------------+
请注意,这将为John Doe返回两行,因为他具有两个完全相同总值的投资组合。 为了避免这种情况,并选择一个任意的顶级投资组合,在GROUP BY子句上标记:
select client_id, name, portfolio_id, cash + stocks from client join portfolio using (client_id) join (select client_id, max(cash + stocks) as maxtotal from portfolio group by client_id) as maxima using (client_id) where cash + stocks = maxtotal group by client_id, cash + stocks +-----------+----------+--------------+---------------+ | client_id | name | portfolio_id | cash + stocks | +-----------+----------+--------------+---------------+ | 1 | John Doe | 5 | 33.33 | | 2 | Jane Doe | 8 | 90.90 | +-----------+----------+--------------+---------------+
First, let's make some test data:
create table client (client_id integer not null primary key auto_increment, name varchar(64)); create table portfolio (portfolio_id integer not null primary key auto_increment, client_id integer references client.id, cash decimal(10,2), stocks decimal(10,2)); insert into client (name) values ('John Doe'), ('Jane Doe'); insert into portfolio (client_id, cash, stocks) values (1, 11.11, 22.22), (1, 10.11, 23.22), (2, 30.30, 40.40), (2, 40.40, 50.50);
If you didn't need the portfolio ID, it would be easy:
select client_id, name, max(cash + stocks) from client join portfolio using (client_id) group by client_id +-----------+----------+--------------------+ | client_id | name | max(cash + stocks) | +-----------+----------+--------------------+ | 1 | John Doe | 33.33 | | 2 | Jane Doe | 90.90 | +-----------+----------+--------------------+
Since you need the portfolio ID, things get more complicated. Let's do it in steps. First, we'll write a subquery that returns the maximal portfolio value for each client:
select client_id, max(cash + stocks) as maxtotal from portfolio group by client_id +-----------+----------+ | client_id | maxtotal | +-----------+----------+ | 1 | 33.33 | | 2 | 90.90 | +-----------+----------+
Then we'll query the portfolio table, but use a join to the previous subquery in order to keep only those portfolios the total value of which is the maximal for the client:
select portfolio_id, cash + stocks from portfolio join (select client_id, max(cash + stocks) as maxtotal from portfolio group by client_id) as maxima using (client_id) where cash + stocks = maxtotal +--------------+---------------+ | portfolio_id | cash + stocks | +--------------+---------------+ | 5 | 33.33 | | 6 | 33.33 | | 8 | 90.90 | +--------------+---------------+
Finally, we can join to the client table (as you did) in order to include the name of each client:
select client_id, name, portfolio_id, cash + stocks from client join portfolio using (client_id) join (select client_id, max(cash + stocks) as maxtotal from portfolio group by client_id) as maxima using (client_id) where cash + stocks = maxtotal +-----------+----------+--------------+---------------+ | client_id | name | portfolio_id | cash + stocks | +-----------+----------+--------------+---------------+ | 1 | John Doe | 5 | 33.33 | | 1 | John Doe | 6 | 33.33 | | 2 | Jane Doe | 8 | 90.90 | +-----------+----------+--------------+---------------+
Note that this returns two rows for John Doe because he has two portfolios with the exact same total value. To avoid this and pick an arbitrary top portfolio, tag on a GROUP BY clause:
select client_id, name, portfolio_id, cash + stocks from client join portfolio using (client_id) join (select client_id, max(cash + stocks) as maxtotal from portfolio group by client_id) as maxima using (client_id) where cash + stocks = maxtotal group by client_id, cash + stocks +-----------+----------+--------------+---------------+ | client_id | name | portfolio_id | cash + stocks | +-----------+----------+--------------+---------------+ | 1 | John Doe | 5 | 33.33 | | 2 | Jane Doe | 8 | 90.90 | +-----------+----------+--------------+---------------+
相关问答
更多-
有什么数据库专业书籍介绍?[2022-05-19]
mysql -
MySQL GROUP BY两列(MySQL GROUP BY two columns)[2024-05-07]
首先,我们来做一些测试数据: create table client (client_id integer not null primary key auto_increment, name varchar(64)); create table portfolio (portfolio_id integer not null primary key auto_increment, client_id integer r ... -
mySQL group by 3列(mySQL group by 3 columns)[2022-05-13]
尝试日期,银行名称,referenceNo上的复合索引 CREATE INDEX date_bankname_referenceNo ON table (date, bankname, referenceNo); try a compound index on date, bankname, referenceNo CREATE INDEX date_bankname_referenceNo ON table (date, bankname, referenceNo); -
尝试加入 SELECT a.`begin_date`, IF(b.`end_date` IS NOT NULL, b.`end_date`, a.`end_date`), a.`employer_id` FROM `mytable` a LEFT JOIN `mytable` b ON a.`employer_id` = b.`employer_id` AND a.`end_date` = b.`begin_date` LEFT JOIN `mytable` c ON a.`employer_id` = c ...
-
SELECT LEAST(USERID, FRIENDID) as x, GREATEST(USERID, FRIENDID) as y FROM TableName GROUP BY x, y SQLFIddle演示 SELECT LEAST(USERID, FRIENDID) as x, GREATEST(USERID, FRIENDID) as y FROM TableName GROUP BY x, y SQLFIddle Demo
-
根据您的示例表格,看起来您想要在product上进行分组,而不是在id上进行分组。 您只需将Size列添加到SELECT列表和GROUP BY $query = "SELECT product, Size, SUM(Quantity) AS TotalQuantity FROM inventory GROUP BY product, Size"; 请注意,我添加了一个列别名TotalQua ...
-
首先,我们编写一个查询,为每个类别( test_cat )获取最高(MAX) test_sn 。 喜欢这个: SELECT t.test_cat , MAX(t.test_sn) AS max_test_sn FROM car_test t GROUP BY t.test_cat 然后,我们可以将该查询用作另一个查询中的内联视图 。 (上面的查询返回一个结果集。我们可以引用它来代替表名。)作为一个简单的演示: SELECT s.test_cat , s.max_te ...
-
MySql组由两列组成(MySql group by two columns)[2022-07-11]
此表应该包含您团队的外键,您只需在团队表中选择团队名称即可。 无论如何,如果你想这样做,试试这个: SELECT DISTINCT opponent AS team FROM table UNION SELECT DISTINCT competition AS team FROM table This table should have foreign key of your teams and you would simply select teams name in teams table. Anyw ... -
您可以使用union all运算符在单个结果列中获取两列,然后按其分组: SELECT custom, COUNT(*) FROM (SELECT custom_1 AS custom FROM mytable UNION ALL SELECT custom_2 AS custom FROM mytable) t GROUP BY custom You could use the union all ope ...
-
您是在正确的轨道上...将预查询作为您想要的记录的基础,然后在符合条件的日期条件下重新加入相同的原始表格。 select PreQuery.Portal, PreQuery.LatestPostNumber, T2.User, T2.Text, T2.Date from ( select t1.Portal, max( t1.PostNumber ) as LatestPo ...