2015-06-24 39 views
3

我有一个CustomRecyclerViewAdapter.class文件,我在其中实现了下面的方法。如何在自定义RecyclerView适配器中使用startActivity(intent)开始新活动?

public void onBindViewHolder(RecyclerViewHolder viewHolder, int position) 
{ 
    viewHolder.title.setText(mData.get(position).text); 
    //viewHolder.icon.setBackgroundColor(Color.parseColor(mData.get(position).color)); 

    viewHolder.setClickListener(new RecyclerViewHolder.ClickListener(){ 

     @Override 
     public void onClick(View v, int position, boolean isLongClick) { 

      if (isLongClick) { 
       // View v at position pos is long-clicked. 
       Toast.makeText(v.getContext(), "Hey you just hit item" + position, Toast.LENGTH_SHORT).show(); 


      }else { 
       // View v at position pos is clicked. 
       //how to start a new activity here 
       Toast.makeText(v.getContext(),"Hey you just hit item" + position,Toast.LENGTH_SHORT).show(); 
      } 


     } 
    }); 

} 

所以我怎么能在上面的else块中启动新的活动。

Group.class

public class Group extends ActionBarActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState);//line no. 16 which is indicated in logcat 
     setContentView(R.layout.group); 

     getSupportActionBar().setHomeButtonEnabled(true); 
     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

} 

这是我的错误的logcat .....

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.trueblueoperator.samplerecyclerview/com.trueblueoperator.samplerecyclerview.Group}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity. 
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298) 
     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) 
     at android.app.ActivityThread.access$800(ActivityThread.java:144) 
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) 
     at android.os.Handler.dispatchMessage(Handler.java:102) 
     at android.os.Looper.loop(Looper.java:135) 
     at android.app.ActivityThread.main(ActivityThread.java:5221) 
     at java.lang.reflect.Method.invoke(Native Method) 
     at java.lang.reflect.Method.invoke(Method.java:372) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 
Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity. 
     at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:151) 
     at android.support.v7.app.ActionBarActivityDelegateBase.onCreate(ActionBarActivityDelegateBase.java:138) 
     at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:123) 
     at com.trueblueoperator.samplerecyclerview.Group.onCreate(Group.java:16) 
     at android.app.Activity.performCreate(Activity.java:5933) 
     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105) 
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251) 
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) 
            at android.app.ActivityThread.access$800(ActivityThread.java:144) 
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) 
            at android.os.Handler.dispatchMessage(Handler.java:102) 
            at android.os.Looper.loop(Looper.java:135) 
            at android.app.ActivityThread.main(ActivityThread.java:5221) 
            at java.lang.reflect.Method.invoke(Native Method) 
            at java.lang.reflect.Method.invoke(Method.java:372) 
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 

感谢您的时间。

+1

那么目前面临的问题是什么?只需使用'v.getContext()。startActivity(intent)'启动Activity就点击按钮 –

+1

我用这个代码Intent intent = new Intent(v.getContext(),Group.class); v.getContext()。startActivity(intent); –

+0

但是它不幸地停止了应用 –

回答

5

您可以使用任何查看。 getContext()方法提供了几乎活动的方法,因为Activity扩展了Context。

v.getContext().startActivity(intent); 
相关问题