使用回收器视图中的按钮打开地图

时间:2015-04-25 22:00:46

标签: android

我正在尝试为回收器视图中的卡片上显示的按钮设置一个意图,但是我在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;
}


}

1 个答案:

答案 0 :(得分:1)

您必须使用context来呼叫startActivity()

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

这里是ViewHolder Class

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;
}


}