如何在 Activity 中接收 Intent
How to receive Intent in Activity
老实说,我在询问之前对此进行了一些搜索。如何接收从 activity 到 fragmentactivity
的意图 我正在尝试使用 viewpager。我在获取参数时遇到错误Bundle extras = getArguments();
public class SingleViewActivity extends FragmentActivity {
ImageView imageView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_page_view);
ArrayList<Listitem> personArrayList = new ArrayList<Listitem>();
// Listitem item = new Listitem("1", "http://developer.android.com/assets/images/android_logo.png");//I used to add it staticly
Bundle extras = getArguments();
if (extras != null) {
extras = extras.getParcelableArrayList(ARG_PERSON_LIST);
personArrayList.add(item); }
// DemoObjectFragment f = DemoObjectFragment.newInstance(personArrayList);
ViewPager mViewPager = (ViewPager) findViewById(R.id.pager);
Log.d("s", "singleview");
DemoCollectionPagerAdapter mDemoCollectionPagerAdapter = new DemoCollectionPagerAdapter( getSupportFragmentManager(),personArrayList);
mViewPager.setAdapter(mDemoCollectionPagerAdapter);
您正在将 ParcelableList 对象分配给 Bundle 变量。
extras = extras.getParcelableArrayList(ARG_PERSON_LIST); //change it
将其分配给 ParcelableList
而不是 extras
。
编辑:
试试这个,
ArrayList<Listitem> personArrayList;
// Listitem item = new Listitem("1", "http://developer.android.com/assets/images/android_logo.png");//I used to add it staticly
Bundle extras = getArguments();
if (extras != null) {
personArrayList = (ArrayList<Listitem>) extras.getParcelableArrayList("jh");
personArrayList.add(item);
....
}
当您创建片段对象时,您也已经在创建的片段对象中设置了参数。
像这样...
Fragment f = new Fragment();
Bundle args = new Bundle();
args.putInt("someNum", 0);
f.setArguments(args);
然后在fragment的onCreate()或onCreateView方法中获取参数。
getArguments().getInt("someNum");
在您的情况下,它将是可打包或可序列化的对象。
您将需要两个活动。由于您没有 post Activity
启动 posted Activity
,我做了一个简单的,只包含一个 Button
启动显示Activity
。
记下注释的步骤。总共有六个步骤。
第一个 Activity
获取 ArrayList
数据,然后将其传递给第二个 Activity
,后者又让步给 DemoCollectionAdapter
。
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Step 1: Build an ArrayList to pass to the next Activity
ArrayList<Listitem> items = new ArrayList<Listitem>();
items.add(new Listitem("1", "http://developer.android.com/assets/images/android_logo.png"));
items.add(new Listitem("2", "https://i.stack.imgur.com/B28Ca.jpg?s=328&g=1"));
// Step 2: Create an Intent to start the next Activity
Intent intent = new Intent(getApplicationContext(), SingleViewActivity.class);
// Step 3: Put the ArrayList into the Intent
intent.putParcelableArrayListExtra(SingleViewActivity.ARG_PERSON_LIST, items);
// Step 4: Start the next Activity
startActivity(intent);
}
});
}
}
public class SingleViewActivity extends FragmentActivity {
public static final String ARG_PERSON_LIST = "ARG_PERSON_LIST";
private ArrayList<Listitem> items;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_page_view);
// Step 5: Get the Bundle from the Intent that started this Activity
Bundle extras = getIntent().getExtras();
if (extras != null) {
// Step 6: Get the data out of the Bundle
items = extras.getParcelableArrayList(ARG_PERSON_LIST);
} else {
items = new ArrayList<Listitem>();
}
ViewPager pager = (ViewPager) findViewById(R.id.pager);
DemoCollectionPagerAdapter adapter = new DemoCollectionPagerAdapter(getSupportFragmentManager(), items);
pager.setAdapter(adapter);
}
}
老实说,我在询问之前对此进行了一些搜索。如何接收从 activity 到 fragmentactivity
的意图 我正在尝试使用 viewpager。我在获取参数时遇到错误Bundle extras = getArguments();
public class SingleViewActivity extends FragmentActivity {
ImageView imageView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_page_view);
ArrayList<Listitem> personArrayList = new ArrayList<Listitem>();
// Listitem item = new Listitem("1", "http://developer.android.com/assets/images/android_logo.png");//I used to add it staticly
Bundle extras = getArguments();
if (extras != null) {
extras = extras.getParcelableArrayList(ARG_PERSON_LIST);
personArrayList.add(item); }
// DemoObjectFragment f = DemoObjectFragment.newInstance(personArrayList);
ViewPager mViewPager = (ViewPager) findViewById(R.id.pager);
Log.d("s", "singleview");
DemoCollectionPagerAdapter mDemoCollectionPagerAdapter = new DemoCollectionPagerAdapter( getSupportFragmentManager(),personArrayList);
mViewPager.setAdapter(mDemoCollectionPagerAdapter);
您正在将 ParcelableList 对象分配给 Bundle 变量。
extras = extras.getParcelableArrayList(ARG_PERSON_LIST); //change it
将其分配给 ParcelableList
而不是 extras
。
编辑:
试试这个,
ArrayList<Listitem> personArrayList;
// Listitem item = new Listitem("1", "http://developer.android.com/assets/images/android_logo.png");//I used to add it staticly
Bundle extras = getArguments();
if (extras != null) {
personArrayList = (ArrayList<Listitem>) extras.getParcelableArrayList("jh");
personArrayList.add(item);
....
}
当您创建片段对象时,您也已经在创建的片段对象中设置了参数。
像这样...
Fragment f = new Fragment();
Bundle args = new Bundle();
args.putInt("someNum", 0);
f.setArguments(args);
然后在fragment的onCreate()或onCreateView方法中获取参数。
getArguments().getInt("someNum");
在您的情况下,它将是可打包或可序列化的对象。
您将需要两个活动。由于您没有 post Activity
启动 posted Activity
,我做了一个简单的,只包含一个 Button
启动显示Activity
。
记下注释的步骤。总共有六个步骤。
第一个 Activity
获取 ArrayList
数据,然后将其传递给第二个 Activity
,后者又让步给 DemoCollectionAdapter
。
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Step 1: Build an ArrayList to pass to the next Activity
ArrayList<Listitem> items = new ArrayList<Listitem>();
items.add(new Listitem("1", "http://developer.android.com/assets/images/android_logo.png"));
items.add(new Listitem("2", "https://i.stack.imgur.com/B28Ca.jpg?s=328&g=1"));
// Step 2: Create an Intent to start the next Activity
Intent intent = new Intent(getApplicationContext(), SingleViewActivity.class);
// Step 3: Put the ArrayList into the Intent
intent.putParcelableArrayListExtra(SingleViewActivity.ARG_PERSON_LIST, items);
// Step 4: Start the next Activity
startActivity(intent);
}
});
}
}
public class SingleViewActivity extends FragmentActivity {
public static final String ARG_PERSON_LIST = "ARG_PERSON_LIST";
private ArrayList<Listitem> items;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_page_view);
// Step 5: Get the Bundle from the Intent that started this Activity
Bundle extras = getIntent().getExtras();
if (extras != null) {
// Step 6: Get the data out of the Bundle
items = extras.getParcelableArrayList(ARG_PERSON_LIST);
} else {
items = new ArrayList<Listitem>();
}
ViewPager pager = (ViewPager) findViewById(R.id.pager);
DemoCollectionPagerAdapter adapter = new DemoCollectionPagerAdapter(getSupportFragmentManager(), items);
pager.setAdapter(adapter);
}
}