- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是我的适配器。
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private static final int TYPE_HEADER = 0; // Declaring Variable to Understand which View is being worked on
// IF the viaew under inflation and population is header or Item
private static final int TYPE_ITEM = 1;
private String mNavTitles[]; // String Array to store the passed titles Value from MainActivity.java
private int mIcons[]; // Int Array to store the passed icons resource value from MainActivity.java
private String name; //String Resource for header View Name
private int profile; //int Resource for header view profile picture
private String email; //String Resource for header view email
static Context context;
// Creating a ViewHolder which extends the RecyclerView View Holder
// ViewHolder are used to to store the inflated views in order to recycle them
public static class ViewHolder extends RecyclerView.ViewHolder {
int Holderid;
TextView textView;
ImageView imageView;
ImageView profile;
TextView Name;
TextView email;
Context contxt;
Intent intent;
public ViewHolder(View itemView,int ViewType,Context c) { // Creating ViewHolder Constructor with View and viewType As a parameter
super(itemView);
contxt = c;
itemView.setClickable(true);
itemView.setOnClickListener(Click);
// Here we set the appropriate view in accordance with the the view type as passed when the holder object is created
if(ViewType == TYPE_ITEM) {
textView = (TextView) itemView.findViewById(R.id.rowText); // Creating TextView object with the id of textView from item_row.xml
imageView = (ImageView) itemView.findViewById(R.id.rowIcon);// Creating ImageView object with the id of ImageView from item_row.xml
Holderid = 1; // setting holder id as 1 as the object being populated are of type item row
}
else{
Name = (TextView) itemView.findViewById(R.id.name); // Creating Text View object from header.xml for name
email = (TextView) itemView.findViewById(R.id.email); // Creating Text View object from header.xml for email
profile = (ImageView) itemView.findViewById(R.id.circleView);// Creating Image view object from header.xml for profile pic
Holderid = 0; // Setting holder id = 0 as the object being populated are of type header view
}
}
private View.OnClickListener Click=new View.OnClickListener(){
@Override
public void onClick(View v) {
switch(getPosition())
{
case 1:
intent = new Intent(context, Me.class);
startActivity(intent);
}
}
};
}
MyAdapter(String Titles[],int Icons[],String Name,String Email, int Profile,Context passedContext){ // MyAdapter Constructor with titles and icons parameter
// titles, icons, name, email, profile pic are passed from the main activity as we
mNavTitles = Titles; //have seen earlier
mIcons = Icons;
name = Name;
email = Email;
profile = Profile; //here we assign those passed values to the values we declared here
this.context = passedContext;
//in adapter
}
//Below first we ovverride the method onCreateViewHolder which is called when the ViewHolder is
//Created, In this method we inflate the item_row.xml layout if the viewType is Type_ITEM or else we inflate header.xml
// if the viewType is TYPE_HEADER
// and pass it to the view holder
@Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == TYPE_ITEM) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_row,parent,false); //Inflating the layout
ViewHolder vhItem = new ViewHolder(v,viewType,context); //Creating ViewHolder and passing the object of type view
return vhItem; // Returning the created object
//inflate your layout and pass it to view holder
} else if (viewType == TYPE_HEADER) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.header,parent,false); //Inflating the layout
ViewHolder vhHeader = new ViewHolder(v,viewType,context); //Creating ViewHolder and passing the object of type view
return vhHeader; //returning the object created
}
return null;
}
//Next we override a method which is called when the item in a row is needed to be displayed, here the int position
// Tells us item at which position is being constructed to be displayed and the holder id of the holder object tell us
// which view type is being created 1 for item row
@Override
public void onBindViewHolder(MyAdapter.ViewHolder holder, int position) {
if(holder.Holderid ==1) { // as the list view is going to be called after the header view so we decrement the
// position by 1 and pass it to the holder while setting the text and image
holder.textView.setText(mNavTitles[position - 1]); // Setting the Text with the array of our Titles
holder.imageView.setImageResource(mIcons[position -1]);// Settimg the image with array of our icons
}
else{
holder.profile.setImageResource(profile); // Similarly we set the resources for header view
holder.Name.setText(name);
holder.email.setText(email);
}
}
// This method returns the number of items present in the list
@Override
public int getItemCount() {
return mNavTitles.length+1; // the number of items in the list will be +1 the titles including the header view.
}
// Witht the following method we check what type of view is being passed
@Override
public int getItemViewType(int position) {
if (isPositionHeader(position))
return TYPE_HEADER;
return TYPE_ITEM;
}
private boolean isPositionHeader(int position) {
return position == 0;
}
这是里面的OnClickListener
private View.OnClickListener Click=new View.OnClickListener(){
@Override
public void onClick(View v) {
switch(getPosition())
{
case 1:
intent = new Intent(context, Me.class);
startActivity(intent); // ERROR!
}
}
};
}
我正在尝试将 startActivity 放入其中,但出现错误。
我该如何处理?
最佳答案
像下面这样尝试。
private View.OnClickListener Click=new View.OnClickListener(){
@Override
public void onClick(View v) {
switch(getPosition())
{
case 1:
intent = new Intent(context, Me.class);
context.startActivity(intent); // or
v.getContext().startActivity(intent);
}
}
};
关于android - Adapter中如何将startActivity添加到OnClickListener中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29319164/
我几天前开始进行android开发。我实现了一个 recylerview,在 recyclerview 适配器的 OnBindViewHolder 方法中,我在 recyclerview 项目上使用了
我已阅读文章https://github.com/Kotlin/anko/wiki/Anko-Commons-%E2%80%93-Intents . 我可以使用 startActivity(inten
我的 BroadcastReceiver 的 onReceive 函数中有以下代码。 public void onReceive(Context context, Intent intent) {
无论我做什么,我都无法开始新 Activity 。我重新下载了所有的Android SDK。我重新下载了Eclipse并再次安装了ADT插件 错误日志 09-16 17:53:51.537: W/II
我有以下 Activity ,当调用 startactivity() 时,应用程序被强制关闭,我已经搜索了几天失败的原因,有人可以帮助我吗? 应用程序: package com.example.dec
我是 Android 开发新手,在创建新 Activity 时遇到问题。 我想在开始之前使用我的 Activity 。例如,我已经尝试过: MyActivity menu = new MyActivi
在我的应用程序菜单类中,我想要一个按钮来启动 Activity 。这不适用于 StartActivity() 方法,因为它根本不是 Activity 的子类。那我该怎么办呢? 如果你读过《Beginn
我对 Android/Java 完全陌生。我正在尝试创建一个应用程序来连接到我的 TCP 服务器以从 mySQL 获取数据。 我有以下内容:MainActivity - 这是登录屏幕。输入用户名和密码
我刚刚开始 Android 开发,我被这段代码困住了: public class Home extends Activity { /** Called when the activity is
我有一个对话框提示用户确认删除。 在用户点击删除后弹出一个简单的警告。我需要重定向到一个新 Activity ,但在我有机会在确认警报上单击“确定”之前,startActivity 已触发。我在它搞砸
为什么要在服务中使用 startActivity()?如果我需要一个 Activity 必须调用一个 Activity ,如果我需要一个“延迟 Activity ”我必须使用通知。那么,我为什么要使用
引擎盖下发生了什么?这会以某种方式传递给操作系统,并且表明操作系统会找到正确的 Activity/Activity 并启动它?是否有在 Android 中运行的服务/库来处理这个?我正在尝试修改操作系
我有一个主类和一个单独的类 (OnClickBtn.java),我只想在其中保留按钮方法。我不想从主类而是从 OnClickBtn.java 开始新的 Activity 。 我已经运行了该程序,但它在
我正在学习 android 开发。这只是示例项目。所以我在按钮点击事件上创建了相同的 Activity (见代码)。在第一个 Activity 之后,我可以看到带有“qwerty”文本的新 Activ
我已经设置了一个从菜单按钮执行的 Activity 。 Activity 启动并短暂出现然后崩溃。我已将附加 Activity 添加到 list 文件。代码如下。我最近从 Eclipse 切换到 An
我试图从 OptionsMenu 启动一个 Activity ,但它没有启动。为什么? @Override public boolean onOptionsItemSelected(MenuItem
我创建了一个 activity( Activity 编号 1),其中包含 2 个 Views、一个 button 和一个 textView这两个 View 被共享到另一个activity(activi
截至今天上午,我已经尝试整合足够的知识来制作一个非常基本的应用程序来演示一个概念。这个想法是显示谷歌地图,用户按下他们想要添加标记的位置,然后弹出一个屏幕,他们可以在其中填写更多信息,然后当有人点击该
我正在制作一个绘图应用程序并想分享图像。但是,我遇到了 startActivity 部分的错误,其中错误写道 The method startActivity(Intent) is undefined
调用 startActivity 时,我一直收到 NPE,我认为这与我提供给 Intent 的上下文有关。当我从 Internet 获取 xml 数据时,我试图让启动画面保持不变。 splash Ac
我是一名优秀的程序员,十分优秀!