2017-01-04 4 views
1

したがって、XMLビューアのリストビューが表示されます。リストの行をクリックすると、インテントから詳細ページへの新しいアクティビティが開始されます。リストビューの次の行に対応する次の詳細ページを取得するには、右または左にスワイプしたいと思います。私はonSwipeRightとonSwipeLeftを設定しましたが、次の/前の詳細ページに移入するために何を入れるべきかわかりません。リストビューのインテントから開始された次のアクティビティの詳細を取得するために右または左にスワイプする方法

ここonswipeリスナー

public class RouteDetails extends AppCompatActivity { 
MyDBHandler dbHandler; 
ImageView routeImage; 
String routeName; 
CharSequence route; 
CheckBox routeCheckBox; 
int listViewPosition; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_route_details); 

    dbHandler = new MyDBHandler(this, null, null, 1); 

    //sets actionbar title 
    routeName = getIntent().getExtras().getString("routeName"); 
    getSupportActionBar().setTitle(routeName); 

    //TextView for route details 
    final TextView routeDetailsView = (TextView) findViewById(R.id.routeDetailsView); 
    route = getIntent().getExtras().getCharSequence("route"); 
    routeDetailsView.setText(route); 

    //ImageView for route details 
    routeImage = (ImageView) findViewById(R.id.routeImage); 
    final int mImageResource = getIntent().getIntExtra("imageResourceId", 0); 
    routeImage.setImageResource(mImageResource); 

    ///gets position of row that was clicked to pass into database 
    listViewPosition = getIntent().getIntExtra("listViewPosition", 0); 


    /////////////// 

    routeDetailsView.setOnTouchListener(new OnSwipeTouchListener(this) { 
     @Override 
     public void onSwipeLeft() { 
      // Whatever 
      Intent i = new Intent(RouteDetails.this,RouteDetails.class); 

      startActivity(i); 
     } 

    @Override 
    public void onSwipeRight() { 
     // Whatever 
     Intent i = new Intent(RouteDetails.this,RouteDetails.class); 

     startActivity(i); 
    } 

    }); 

    /////////////// 

} 

感謝して詳細活動

public class MainActivity extends AppCompatActivity { 
MyDBHandler dbHandler; 
int listViewPosition; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    dbHandler = new MyDBHandler(this, null, null, 1); 
    //Action Bar customization 
    final android.support.v7.app.ActionBar actionBar = getSupportActionBar(); 
    actionBar.setDisplayOptions(android.support.v7.app.ActionBar.DISPLAY_SHOW_CUSTOM); 
    actionBar.setCustomView(R.layout.actionbar); 

    setContentView(R.layout.activity_main); 

    //fill list view with xml array of routes 
    final CharSequence[] routeListViewItems = getResources().getTextArray(R.array.routeList); 
    ////fill list view with xml array of route numbers 
    final TypedArray routeNumberImages = getResources().obtainTypedArray(R.array.routeNumberImages); 
    //fills route detail text view with xml array info 
    final CharSequence[] routeDetail= getResources().getTextArray(R.array.routeDetail); 
    //fills route detail image view with xml array of images 
    final TypedArray image = getResources().obtainTypedArray(R.array.routeImages); 


    //custom adapter for list view 
    ListAdapter routeAdapter = new CustomAdapter(this, routeListViewItems, routeNumberImages); 
    final ListView routeListView = (ListView) findViewById(R.id.routeListView); 
    routeListView.setAdapter(routeAdapter); 

    //sets first visible list item 
    listViewPosition = getIntent().getIntExtra("listViewPosition", 0); 
    routeListView.setSelectionFromTop(listViewPosition, 0); 

    routeListView.setOnItemClickListener(
      new AdapterView.OnItemClickListener() { 
       @Override 
       public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 

        CharSequence route = routeListViewItems[position]; 
        int imageId = (int) image.getResourceId(position, -1); 
        if (route.equals(routeListViewItems[position])) 
        { 
         Intent intent = new Intent(view.getContext(), RouteDetails.class); 
         intent.putExtra("route", routeDetail[position]); 
         intent.putExtra("imageResourceId", imageId); 
         intent.putExtra("routeName", routeListViewItems[position]); 
         intent.putExtra("listViewPosition", position); 
         startActivity(intent); 
        } 
       } 
      } 
    ); 
} 

とHERESに私の詳細ページに関連する情報を渡すために、リストビューとのclickリスナーと私の主な活動ですあなたはどんな助けでも!エラーと

編集新しいコードを実装

01-04 12:55:51.694 25443-25443/com.example.zach.listview E/AndroidRuntime: FATAL EXCEPTION: main 
                     Process: com.example.zach.listview, PID: 25443 
                     java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.zach.listview/com.example.zach.listview.RouteDetails}: java.lang.NullPointerException: Attempt to read from null array 
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416) 
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                      at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                      at android.os.Looper.loop(Looper.java:148) 
                      at android.app.ActivityThread.main(ActivityThread.java:5417) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
                     Caused by: java.lang.NullPointerException: Attempt to read from null array 
                      at com.example.zach.listview.RouteDetails.onCreate(RouteDetails.java:56) 
                      at android.app.Activity.performCreate(Activity.java:6251) 
                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)  
                      at android.app.ActivityThread.-wrap11(ActivityThread.java)  
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)  
                      at android.os.Handler.dispatchMessage(Handler.java:102)  
                      at android.os.Looper.loop(Looper.java:148)  
                      at android.app.ActivityThread.main(ActivityThread.java:5417)  
                      at java.lang.reflect.Method.invoke(Native Method)  
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)  
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)  

答えて

1

の代わりにあなたの詳細ページに単一の項目を通過した後、あなたは配列全体だけでなく、(あなたがすでに行う)現在位置を渡す必要があります。あなたのクリックイベント(MainActivity)で

、交換してください:

intent.putExtra("route", routeDetail[position]); 
intent.putExtra("routeName", routeListViewItems[position]); 

をして:

あなたは配列や現在のオブジェクトを取得する必要がありますRouteDetailsで
intent.putExtra("routes", routeDetail); 
intent.putExtra("routeNames", routeListViewItems); 

CharSequence[] routes; 
CharSequence[] routeNames; 

// ... 

listViewPosition = getIntent().getIntExtra("listViewPosition", 0); 

routes = getIntent().getExtras().getCharSequenceArray("routes"); 
routeNames = getIntent().getExtras().getCharSequenceArray("routeNames"); 

route = routes[listViewPosition]; 
routeName = routeNames[listViewPosition]; 

あなたのスワイプイベントでは、前または次のインデックスと共に配列を渡すだけです:

routeDetailsView.setOnTouchListener(new OnSwipeTouchListener(this) { 
    @Override 
    public void onSwipeLeft() { 
     // Whatever 
     Intent i = new Intent(RouteDetails.this,RouteDetails.class); 
     i.putExtra("routes", routes); 
     i.putExtra("routeNames", routeNames); 
     i.putExtra("imageResourceId", imageId); 
     i.putExtra("listViewPosition", position + 1); 
     startActivity(i); 
    } 

    @Override 
    public void onSwipeRight() { 
     // Whatever 
     Intent i = new Intent(RouteDetails.this,RouteDetails.class); 
     i.putExtra("routes", routes); 
     i.putExtra("routeNames", routeNames); 
     i.putExtra("imageResourceId", imageId); 
     i.putExtra("listViewPosition", position - 1); 
     startActivity(i); 
    } 
}); 
+0

私はあなたが提供したコードを追加しましたが、今はnull配列に対してnullポインタ例外が発生しています。 – zsh5032

+0

私は自分の答えを編集しました。変数名が混ざっていました。それでもエラーが発生した場合は、どのラインで教えてもらえますか? –

+0

私はあなたのコードを試したときにそれに気付き、それらを変更しましたが、うまくいかなかったのです。元の投稿にエラーログを記録しました。ありがとう! – zsh5032

関連する問題