【问题标题】:How to swipe right or left to get next activity detail that was started from a listview intent如何向右或向左滑动以获取从列表视图意图开始的下一个活动详细信息
【发布时间】:2017-01-04 03:13:36
【问题描述】:

所以我有一个由 xml 数组列表填充的列表视图。当我单击列表中的一行时,它会通过对详细信息页面的意图启动一个新活动。我希望能够向右或向左滑动以获取与列表视图中的下一行相对应的下一个详细信息页面。我已经设置了我的 onSwipeRight 和 onSwipeLeft ,但我不知道在它们里面放什么来填充下一个/上一个详细信息页面。

这是我的主要活动,其中包含列表视图和点击监听器,用于将相关信息传递给详细信息活动

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);
                    }
                }
            }
    );
}

这是我的带有 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);
    }

    });

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

}

感谢您的帮助!

实施新代码后出现错误编辑

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) 

【问题讨论】:

    标签: android listview android-intent


    【解决方案1】:

    您应该传递整个数组以及当前位置(您已经这样做了),而不是将单个项目传递到您的详细信息页面。

    在您的点击事件(MainActivity)中,替换:

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

    与:

    intent.putExtra("routes", routeDetail);
    intent.putExtra("routeNames", routeListViewItems);
    

    在 RouteDetails 中,您需要获取数组和当前对象:

    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);
        }
    });
    

    【讨论】:

    • 所以我添加了您提供的代码,但现在我在空数组上遇到空指针异常。
    • 我刚刚编辑了我的答案,我把变量名弄混了。如果还是有错误,能告诉我在哪一行吗?
    • 我在尝试您的代码时注意到了这一点并对其进行了更改,但这不起作用。我把错误日志放在我原来的帖子里。谢谢!
    • RouteDetails.java 的第 56 行是什么?
    • 所以我能够让它工作。我不得不改变几件事。我摆脱了 route 和 routeName 变量并设置了诸如 routeDetailsView.setText(routes[listViewPosition]); 之类的资源这似乎使它起作用了。我还必须将 putExtra 中的位置更改为 listViewPosition。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-20
    • 2013-07-05
    • 2022-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多