简体   繁体   English

如何将Intent传递给包含ListView的布局? (Android Studio)

[英]How can I pass an Intent to layout which contains ListView? (Android Studio)

I am trying to pass an Intent from my mainScreen Activity class to a new android class with a layout that contains a ListView .. 我试图将Intent从我的mainScreen Activity类传递到具有包含ListView的布局的新的android类。

MainActivityScreen Code : (TimePassed is in the format of 'HH:MM:SS') MainActivityScreen代码:(TimePassed的格式为“ HH:MM:SS”)

public void MoveToShiftsPage(View view) {

    Intent shiftsIntent=new Intent(this,Shifts.class);
    shiftsIntent.putExtra("time",TimePassed);

    startActivity(shiftsIntent);

}

"MoveToShiftsPage" in the MainActivity.xml file : MainActivity.xml文件中的“ MoveToShiftsPage”:

    <ImageButton
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:src="@mipmap/payment"
            android:id="@+id/shiftbtn"
            android:onClick="MoveToShiftsPage"

            />

Shifts.xml File( the layout which contains the ListView and the Intent sent to it) Shifts.xml文件(包含ListView和发送给它的Intent的布局)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" tools:context=".MainScreen"
>

<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:choiceMode="singleChoice"
    android:id="@+id/MyListView"
    android:clickable="true"
    style="@style/Base.Widget.AppCompat.ListView.DropDown">

</ListView>
   />

and the Shift.class which gets the Intent and it's supposed to represent the ListView with the time that passed : 以及获取了Intent的Shift.class,它应该以经过的时间表示ListView:

import android.app.Activity;
import android.content.Intent;
import android.app.ListFragment;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;

import java.util.List;

/**
 * Created by user on 19/09/2015.
 */
public class Shifts extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

      setContentView(R.layout.shifts);
Intent MyIntent=getIntent();
        String TimePassed=MyIntent.getStringExtra("time");
        String [] timepass=TimePassed.split(":");
        ListAdapter theAdapter=new ArrayAdapter<String>    
    (this,android.R.layout.simple_list_item_activated_1,timepass);
        ListView thelist=(ListView)findViewById(R.id.MyListView);
        thelist.setAdapter(theAdapter);
    }
}

Unfortunately whenever I click on the image button which is supposed to be sent to the shift activity class and represents the layout with the ListView the app crashes .. what should I do ? 不幸的是,每当我单击应该发送到轮班活动类并使用ListView表示布局的图像按钮时,应用程序崩溃..我该怎么办?

try this one hopefully it works 希望能尝试这个

Intent MyIntent=this.getIntent(); 意图MyIntent = this.getIntent();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android Studio:如何以意图传递自定义对象 - Android studio: how to pass customed objects in intent 我想在列表视图(Android Studio)中检索包含视频文件的所有文件夹 - I want to retrieve all folders which contains video files in listview ( Android Studio) 如何将Listview添加到Listview Android Studio中 - How can i add Listview into Listview Android Studio Android Studio-当满足“ if”时,如何触发意图 - Android Studio - How can I trigger an intent when an 'if' is fulfilled 如何在 Android Studio 中使用 Intent (Java) 在多个页面之间传递数据? - How do I pass data between multiple pages using intent (Java) in Android studio? 如何在 Android Studio 中使用共享首选项保存我的 listView - How can I save my listView with Shared Preferences in Android Studio 如何将 listView 放在 android 工作室的滚动视图中? - How can I put a listView inside a scroll view on android studio? 我如何在 android studio 中创建历史布局,例如最近 - How I can create history layout in android studio such as recent 如何在Android Studio中为每个设备设置标准布局 - How can I set a standard layout for every device in android studio "如何从 Android Studio 中的布局制作按钮" - How can I make a buttons from a Layout in Android Studio
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM