gpt4 book ai didi

android - 使用回收站 View 中的按钮打开 map

转载 作者:行者123 更新时间:2023-11-30 02:12:57 26 4
gpt4 key购买 nike

我正在尝试为出现在回收站 View 中的卡片上的按钮设置一个 Intent ,但是我收到 startActivity(intent) 部分的错误消息“无法解析方法‘startActivity(android.Content.intent)’” "我是新手,我不确定我哪里出错了,因为代码在我的应用程序的其他部分工作。

这是我的适配器显示我的代码:

public class EventCalenderAdapter extends RecyclerView.Adapter<EventCalenderAdapter.ViewHolder> {


String[] title;
String[] time_start;
String[] time_finish;
String[] date;
String[] description;
String[] loc_lat;
String[] loc_long;

static class ViewHolder extends RecyclerView.ViewHolder {
CardView cardView;
TextView titleView;
TextView auxView1;
TextView auxView2;
TextView auxView3;
Button time_date;
Button location;

public ViewHolder(CardView card) {
super(card);
cardView = card;
titleView = (TextView) card.findViewById(R.id.text1);
auxView1 = (TextView) card.findViewById(R.id.text2);
auxView2 = (TextView) card.findViewById(R.id.text3);
auxView3 = (TextView) card.findViewById(R.id.text4);
time_date = (Button) card.findViewById(R.id.time_date);
location = (Button) card.findViewById(R.id.location);
}
}

public EventCalenderAdapter (Context context) {

title = context.getResources().getStringArray(R.array.title);
time_start = context.getResources().getStringArray(R.array.time_start);
time_finish = context.getResources().getStringArray(R.array.time_finish);
date = context.getResources().getStringArray(R.array.date);
description = context.getResources().getStringArray(R.array.description);
loc_lat = context.getResources().getStringArray(R.array.loc_lat);
loc_long = context.getResources().getStringArray(R.array.loc_long);

}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int i) {
CardView v = (CardView) LayoutInflater.from(parent.getContext()).inflate(R.layout.event_task, parent, false);
return new ViewHolder(v);
}

@Override
public void onBindViewHolder(ViewHolder viewHolder, final int i) {
viewHolder.titleView.setText(title[i]);
viewHolder.auxView1.setText("Date: "+ date[i]);
viewHolder.auxView2.setText("Time: " + time_start[i] + " - " + time_finish[i]);
viewHolder.auxView3.setText("Details: " + description[i]);

viewHolder.time_date.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

}
});

viewHolder.location.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String uri = String.format(Locale.ENGLISH, "geo:%f,%f", loc_lat[i], loc_long[i]);
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
startActivity(intent);
}
});

}
@Override
public int getItemCount() {
return title.length;
}


}

最佳答案

你必须使用 context 来调用 startActivity()

ViewHolder vh=new ViewHolder(card,MainActivity.this);//use it like this

这里是 ViewHolder 类

static class ViewHolder extends RecyclerView.ViewHolder {
CardView cardView;
TextView titleView;
TextView auxView1;
TextView auxView2;
TextView auxView3;
Button time_date;
Button location;
Context context;

public ViewHolder(CardView card,Context context) {
super(card);
cardView = card;
titleView = (TextView) card.findViewById(R.id.text1);
auxView1 = (TextView) card.findViewById(R.id.text2);
auxView2 = (TextView) card.findViewById(R.id.text3);
auxView3 = (TextView) card.findViewById(R.id.text4);
time_date = (Button) card.findViewById(R.id.time_date);
location = (Button) card.findViewById(R.id.location);
this.context=context;
}
}

public EventCalenderAdapter (Context context) {

title = context.getResources().getStringArray(R.array.title);
time_start = context.getResources().getStringArray(R.array.time_start);
time_finish = context.getResources().getStringArray(R.array.time_finish);
date = context.getResources().getStringArray(R.array.date);
description = context.getResources().getStringArray(R.array.description);
loc_lat = context.getResources().getStringArray(R.array.loc_lat);
loc_long = context.getResources().getStringArray(R.array.loc_long);

}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int i) {
CardView v = (CardView) LayoutInflater.from(parent.getContext()).inflate(R.layout.event_task, parent, false);
return new ViewHolder(v);
}

@Override
public void onBindViewHolder(ViewHolder viewHolder, final int i) {
viewHolder.titleView.setText(title[i]);
viewHolder.auxView1.setText("Date: "+ date[i]);
viewHolder.auxView2.setText("Time: " + time_start[i] + " - " + time_finish[i]);
viewHolder.auxView3.setText("Details: " + description[i]);

viewHolder.time_date.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

}
});

viewHolder.location.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String uri = String.format(Locale.ENGLISH, "geo:%f,%f", loc_lat[i], loc_long[i]);
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
context.startActivity(intent);
}
});

}
@Override
public int getItemCount() {
return title.length;
}


}

关于android - 使用回收站 View 中的按钮打开 map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29871224/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com