2015-06-24 1 views
3

У меня есть файл CustomRecyclerViewAdapter.class, в котором я реализовал метод ниже.Как начать новую деятельность с использованием startActivity (намерение) в пользовательском адаптере RecyclerView?

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 (намерение)' для запуска Activity on click от Button –

+1

Я использовал этот код Intent intent = new Intent (v.getContext(), Group.class); v.getContext(). StartActivity (намерение); –

+0

Но это остановило приложение, к сожалению –

ответ

5

Вы можете использовать любой вид. Метод getContext() предоставляет почти методы активности, поскольку Activity расширяет контекст.

v.getContext().startActivity(intent);