我有RecyclerView,我想使用Google Static地图发送URL,并通过Picasso设置此地图。现在,我只尝试在RecyclerView中的每个元素上设置任何静态映射,但是Picasso.with(x)没有上下文。怎么做?
也许可以在FragmentHistory中生成图像并将其作为参数发送吗?
FragmentHistory.xml仅具有RecyclerView;
我的FragmentHistory:
lv.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
Contact current = (Contact)list_ct.get(position);
Intent in=new Intent(Intent.ACTION_EDIT,Uri.parse("content://contacts/people/"+current.getId()));
startActivityForResult(in,2);
}
});
从BD获取数据的功能:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_history, container, false);
ButterKnife.bind(this, view);
FragmentHistorySimpleAdapter adapter = new FragmentHistorySimpleAdapter(generateSimpleListAllWorkout());
RecyclerView recyclerView = (RecyclerView) historyRecyclerView;
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.setAdapter(adapter);
return view;
}
我的fragment_history_simple_item.xml只有2倍的Text View和ImageView
FragmentHistorySimpleAdapter:
private List<FragmentHistorySimpleViewModel> generateSimpleListAllWorkout() {
List<FragmentHistorySimpleViewModel> simpleViewModelList = new ArrayList<>();
DataBaseMain bd = new DataBaseMain(getActivity());
Cursor kurs = bd.giveAllWorkout();
if (kurs.getCount() != 0) {
kurs.moveToLast();
do {
// Pobranie wartości z kolumn (0-3)
String nr = kurs.getString(0);
String time = kurs.getString(1);
String workoutTime = kurs.getString(2);
String distance = kurs.getString(3);
String avgSpeed = kurs.getString(4);
String avgPace = kurs.getString(5);
String kcal = kurs.getString(6);
String LatLng = kurs.getString(7);
String dane = "ID: " + nr + "\n"
+ "Time: " + time + "\n"
+ "Workout time: " + workoutTime + "\n"
+ "Distance: " + distance + " km" + "\n"
+ "Average speed: " + avgSpeed + " km/h" + "\n"
+ "Average pace: " + avgPace + " min/km" + "\n"
+ "Calories: " + kcal;
simpleViewModelList.add(new FragmentHistorySimpleViewModel(dane, "OK"));
// Dopóki kursor może poruszać się w dół (po wynikach)
} while (kurs.moveToPrevious());
}
return simpleViewModelList;
}
FragmentHistorySimpleViewModel:
public class FragmentHistorySimpleAdapter extends RecyclerView.Adapter
{
private List<FragmentHistorySimpleViewModel> models = new ArrayList<>();
public FragmentHistorySimpleAdapter(final List<FragmentHistorySimpleViewModel> viewModels)
{
if (viewModels != null)
{
this.models.addAll(viewModels);
}
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
{
final View view = LayoutInflater.from(parent.getContext()).inflate(viewType, parent, false);
return new FragmentHistorySimpleViewHolder(view);
}
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position)
{
((FragmentHistorySimpleViewHolder) holder).bindData(models.get(position));
}
@Override
public int getItemCount()
{
return models.size();
}
@Override
public int getItemViewType(final int position)
{
return R.layout.fragment_history_simple_item;
}
}
FragmentHistorySimpleViewHolder:
public class FragmentHistorySimpleViewModel
{
private String simpleText;
private String simpleText2;
public FragmentHistorySimpleViewModel(@NonNull final String simpleText, @NonNull final String simpleText2)
{
setSimpleText(simpleText, simpleText2);
}
@NonNull
public String getSimpleText()
{
return simpleText;
}
@NonNull
public String getSimpleText2()
{
return simpleText2;
}
public void setSimpleText(@NonNull final String simpleText, @NonNull final String simpleText2)
{
this.simpleText = simpleText;
this.simpleText2 = simpleText2;
}
}
答案 0 :(得分:1)
首先,您可以使用Glide库而不是Picasso。由于Glide易于加载任何类型的图像,因此它是位图或url等,并且比毕加索更可缩放。
现在您的问题是如何在此处获得上下文的: 在创建FragmentHistorySimpleAdapter对象时,可以专门发送片段上下文,并从FragmentHistorySimpleAdapter构造函数中获取上下文,并在任何地方使用此上下文。
我的FragmentHistory:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_history, container, false);
ButterKnife.bind(this, view);
Context context = container.context;
FragmentHistorySimpleAdapter adapter = new FragmentHistorySimpleAdapter(generateSimpleListAllWorkout(), context);
return view;
}
FragmentHistorySimpleAdapter:
private Context context;
private List<FragmentHistorySimpleViewModel> models = new ArrayList<>();
public FragmentHistorySimpleAdapter(final List<FragmentHistorySimpleViewModel> viewModels, Context context) {
if (viewModels != null) {
this.models.addAll(viewModels);
this.context = context;
}
}
}
答案 1 :(得分:0)
我怀疑这个问题在某个地方还没有答案,但是我相信您可以始终将imageView.getContext()用作毕加索的上下文。然后,您将得到类似function foo(...arguments) {
return {...arguments}
}
const argumentsToObject = foo(1, 2, [1, 2, 3], "name", {a: 10,b: 20})
console.log(argumentsToObject)
此外,您的Picasso库可能是较旧的版本之一?最新版本具有不同的API。像Picasso.with(imageView.getContext()).load(url).into(imageView);