【问题标题】:How can I pass an Intent to layout which contains ListView? (Android Studio)如何将 Intent 传递给包含 ListView 的布局? (安卓工作室)
【发布时间】:2015-12-16 14:45:49
【问题描述】:

我正在尝试将一个 Intent 从我的 mainScreen Activity 类传递给一个新的 android 类,其布局包含 ListView ..

MainActivityScreen 代码: (TimePassed 的格式为 'HH:MM:SS')

public void MoveToShiftsPage(View view) {

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

    startActivity(shiftsIntent);

}

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 文件(包含 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>
   />

以及获取 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);
    }
}

不幸的是,每当我单击应该发送到 shift 活动类并使用 ListView 表示布局的图像按钮时,应用程序就会崩溃.. 我该怎么办?

【问题讨论】:

标签: java android listview android-intent android-studio


【解决方案1】:

试试这个希望它有效

意图 MyIntent=this.getIntent();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-01
    • 2017-02-09
    • 2014-07-09
    • 2021-11-21
    • 2015-06-10
    • 1970-01-01
    • 2020-07-05
    相关资源
    最近更新 更多