2017-02-08 68 views
-3
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerViewHolder> { 

String [] name={ "Androidwarriors","Stackoverflow","Developer Android","AndroidHive","Slidenerd","TheNewBoston","Truiton","HmkCode","JavaTpoint","Javapeper"}; 
Context context; LayoutInflater inflater; 
public Recy 
clerAdapter(Context context) { 
    this.context=context; 
    inflater=LayoutInflater.from(context); 
} 
@Override 
public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
    View v=inflater.inflate(R.layout.item_list, parent, false); 

    RecyclerViewHolder viewHolder=new RecyclerViewHolder(v); 
    return viewHolder; 
} 

@Override 
public void onBindViewHolder(RecyclerViewHolder holder, int position) { 

    holder.tv1.setText(name[position]); 
    holder.imageView.setOnClickListener(clickListener); 
    holder.imageView.setTag(holder); 
} 

View.OnClickListener clickListener=new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     RecyclerViewHolder vholder = (RecyclerViewHolder) v.getTag(); 
     int position = vholder.getPosition(); 
     final Intent intent = new Intent(this, priya.class); 
    } 
}; 
} 

回答

2

檢查下面的代碼 取而代之的是通過上下文。

public class RecyclerAdapter extends RecyclerView.Adapter { 
String [] name={"Androidwarriors","Stackoverflow","Developer Android","AndroidHive","Slidenerd","TheNewBoston","Truiton","HmkCode","JavaTpoint","Javapeper"}; 
Context context; 
LayoutInflater inflater; 
public RecyclerAdapter(Context context) 
{ 
    this.context=context; 
    inflater=LayoutInflater.from(context); 
} 

@Override public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) 
{ 

    View v=inflater.inflate(R.layout.item_list, parent, false); 

    RecyclerViewHolder viewHolder=new RecyclerViewHolder(v); 
    return viewHolder; 
} 

@Override 
public void onBindViewHolder(RecyclerViewHolder holder, int position) { 

holder.tv1.setText(name[position]); 
holder.imageView.setOnClickListener(clickListener); 
holder.imageView.setTag(holder); 
} 

View.OnClickListener clickListener=new View.OnClickListener() { 
@Override 
    public void onClick(View v) { 
    RecyclerViewHolder vholder = (RecyclerViewHolder) v.getTag(); 
    int position = vholder.getPosition(); 
    final Intent intent = new Intent(context, priya.class); 
    context.startActivity(intent); 
    } 
    }; 
} 
0

改變這一行

final Intent intent = new Intent(this, priya.class); 

final Intent intent = new Intent(CurrentActivityName.this, priya.class); 
0

要打開新的活動
在點擊監聽

Intent mIntent = new Intent(context, NextActivity.class); 
context.startActivity(mIntent); 

語境試試這個,你可以從第一個活動或獲得在適配器本身v.getContext()中。

1

您可以從RecyclerAdapter像下面呼叫活動:

final Intent intent = new Intent(context, priya.class); 
context.startActivity(intent); 

希望這有助於

+0

final Intent intent = new Intent(context,priya.class); context.startActivity(intent); – Priya

+0

它正在工作...... – Priya

+0

但它是靜態的 – Priya

相關問題