我正在尝试从非活动类中更改ImageView的src并同时显示错误对话框消息,对话框消息出现但在ImageView的src上没有任何更改这是我的代码使用inflater,我不知道是什么问题。
with recursive r as (
-- start with "required_people" as "missing_required_people" in the whole range
select 0 iteration,
id group_id,
array[]::int[] used_assignment_ids,
-- build a json map, where keys are the time ranges
-- and values are the number of missing people for that range
jsonb_build_object(tsrange(start_time, end_time), required_people) required_people_per_time_range
from groups
where required_people > 0
and id = 1 -- query parameter
union all
select r.iteration + 1,
r.group_id,
r.used_assignment_ids || a.assignment_id,
d.required_people_per_time_range
from r
-- join a single assignment to the previous iteration, where
-- the assigment's time range overlaps with (at least one) time range,
-- where there is still missing people. when there are no such time range is
-- found in assignments, the "recursion" (which is really just a loop) stops
cross join lateral (
select a.id assignment_id, tsrange(start_time, end_time) time_range
from people_assignments a
cross join (select key::tsrange time_range from jsonb_each(r.required_people_per_time_range)) j
where a.group_id = r.group_id
and a.id <> ALL (r.used_assignment_ids)
and tsrange(start_time, end_time) && j.time_range
limit 1
) a
-- "partition" && accumulate all remaining time ranges with
-- the one found in the previous step
cross join lateral (
-- accumulate "partition" results
select jsonb_object_agg(u.time_range, u.required_people) required_people_per_time_range
from (select key::tsrange time_range, value::int required_people
from jsonb_each_text(r.required_people_per_time_range)) j
cross join lateral (
select u time_range, j.required_people - case when u && a.time_range then 1 else 0 end required_people
-- "partition" the found time range with all existing ones, one-by-one
from unnest(case
when j.time_range @> a.time_range
then array[tsrange(lower(j.time_range), lower(a.time_range)), a.time_range, tsrange(upper(a.time_range), upper(j.time_range))]
when j.time_range && a.time_range
then array[j.time_range * a.time_range, j.time_range - a.time_range]
else array[j.time_range]
end) u
where not isempty(u)
) u
) d
),
-- select only the last iteration
l as (
select group_id, required_people_per_time_range
from r
order by iteration desc
limit 1
)
-- unwind the accumulated json map
select l.group_id, lower(time_range) start_time, upper(time_range) end_time, missing_required_people
from l
cross join lateral (
select key::tsrange time_range, value::int missing_required_people
from jsonb_each_text(l.required_people_per_time_range)
) j
-- select only where there is still some missing people
-- this is optional, if you omit it you'll also see row(s) for sub-ranges where
-- there is enough people in the group (these rows will have zero,
-- or negative amount of "missing_required_people")
where j.missing_required_people > 0
更新:完成课程
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService( Context.LAYOUT_INFLATER_SERVICE );
View view = inflater.inflate( R.layout.control_fragment, null );
ImageView imgView = (ImageView)view.findViewById(R.id.okimg);
imgView.setImageResource(R.drawable.clear);
答案 0 :(得分:1)
runOnUiThread(new Runnable() {
@Override
public void run() {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService( Context.LAYOUT_INFLATER_SERVICE );
View view = inflater.inflate( R.layout.control_address_fragment, null );
ImageView okaddress = (ImageView)view.findViewById(R.id.address_ok);
okaddress.setImageResource(R.drawable.clear);
}
});
将您的UI更改移至MainThread。只会反映这些
利用functino runOnUIThread(),这会将您的指令传递给mainthread,从而更新UI。您还可以在此api中移动与UI相关的逻辑
答案 1 :(得分:0)
您可以在ImageView
Fragment
静态内容
public class YourFragment extends Fragment{
public static ImageView okaddress;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.control_address_fragment, container, false);
okaddress = (ImageView)view.findViewById(R.id.address_ok);
}
return view;
}
当结果为0时,如你所说,你可以在AddressControl
YourFragment.okaddress.setImageResource(R.drawable.clear);
答案 2 :(得分:0)
我通过Fragment中的ImageView更改源而不是非活动类来解决我的问题,但是直到现在需要知道为什么我们不能通过以前的方式(从普通类)更改ImageView?