我有一个问题,我有一个活动与tablayout viewpager从数据库获取对象,并通过bundle传递到tab片段,问题是bundle在tab片段中获得null
这里是活动
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import java.util.ArrayList;
import java.util.List;
public class Categorys extends DrawerActivity implements TabLayout.OnTabSelectedListener {
String url,result;
//This is our tablayout
private TabLayout tabLayout;
//This is our viewPager
private ViewPager viewPager;
ProgressDialog progress;
DBhelper db;
Bundle bundle;
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_categorys);
//Adding toolbar to the activity
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// getSupportActionBar().setDisplayShowHomeEnabled(true);
set();
db = new DBhelper(Categorys.this);
db.open();
List<ProductObj> productslistfromDB = db.getAllProductsUp();
db.close();
ProductObj product = productslistfromDB.get(0);
String namee = product.getName();
String details = product.getDetails();
String category = product.getcategory();
String ownerr = product.getOwner();
String weight = product.getWeight();
String size = product.getSize();
String price = product.getPrice();
String imageurl = product.getimageurl();
String owner_address = product.getOwner_address();
String owner_phone = product.getOwner_phone();
String UID = product.getUID();
productpojo productpoj = new productpojo();
productpoj.setName(namee);
productpoj.setDetails(details);
productpoj.setcategory(category);
productpoj.setOwner(ownerr);
productpoj.setWeight(weight);
productpoj.setSize(size);
productpoj.setPrice(price);
productpoj.setimageurl(imageurl);
productpoj.setOwner_address(owner_address);
productpoj.setOwner_phone(owner_phone);
productpoj.setUID(UID);
ArrayList<productpojo> productlisttosend = new ArrayList<>();
productlisttosend.add(productpoj);
Tab1 fragmentGet1 = new Tab1();
Tab2 fragmentGet2 = new Tab2();
bundle = new Bundle();
bundle.putParcelableArrayList("arraylist", productlisttosend);
fragmentGet1.setArguments(bundle);
fragmentGet2.setArguments(bundle);
//Initializing the tablayout
tabLayout = (TabLayout) findViewById(R.id.tabLayout);
//Adding the tabs using addTab() method
tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.ic_clothes));
tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.ic_electrical));
tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.ic_hammer));
tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.ic_cars));
tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.ic_interior_accesories));
tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.ic_buid));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
//Initializing viewPager
viewPager = (ViewPager) findViewById(R.id.pager);
//Creating our pager adapter
Pager adapter = new Pager(getSupportFragmentManager(), tabLayout.getTabCount());
//Adding adapter to pager
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
//Adding onTabSelectedListener to swipe views
tabLayout.setOnTabSelectedListener(this);
}
@Override
public void onTabSelected(TabLayout.Tab tab)
{
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
@Override
public void onBackPressed() {
}
}
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import java.util.ArrayList;
/**
* Created by toshiba c660-12T on 16/08/2016.
*/
public class Tab1 extends Fragment {
GridView gv;
int[] clicknums;
ArrayList<productpojo> productarraylist = null;
Bundle bundle;
//Overriden method onCreateView
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.tab1, container, false);
bundle=this.getArguments();
if (bundle != null) {
productarraylist = bundle.getParcelableArrayList("arraylist");
}
go();
return view;
}
public void go(){
productpojo propoj; = productarraylist.get(0);
String namee = propoj.getName();
String details = propoj.getDetails();
String category = propoj.getcategory();
String ownerr = propoj.getOwner();
String weight = propoj.getWeight();
String size = propoj.getSize();
String price = propoj.getPrice();
String imageurl = propoj.getimageurl();
String owner_address = propoj.getOwner_address();
String owner_phone = propoj.getOwner_phone();
String UID = propoj.getUID();
ProductObj product = new ProductObj();
product.setName(namee);
product.setDetails(details);
product.setcategory(category);
product.setOwner(ownerr);
product.setWeight(weight);
product.setSize(size);
product.setPrice(price);
product.setimageurl(imageurl);
product.setOwner_address(owner_address);
product.setOwner_phone(owner_phone);
product.setUID(UID);
final ArrayList<ProductObj> productlist = new ArrayList<>();
productlist.add(product);
gv = (GridView) getView().findViewById(R.id.grid);
clicknums = new int[productlist.size()];
gv.setAdapter(new CustomGrid(getActivity(), productlist));
gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
int clicknum;
clicknums[position] = clicknums[position] + 1;
clicknum = clicknums[position];
String image = productlist.get(position).imageurl;
String text = productlist.get(position).name;
String owenr = productlist.get(position).owner;
Intent intent = new Intent(getActivity(), Product.class);
intent.putExtra("numofclick", clicknum);
intent.putExtra("title", text);
intent.putExtra("owner", owenr);
intent.putExtra("image", image);
//Start details activity
startActivity(intent);
}
});
}
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import java.util.ArrayList;
/**
* Created by toshiba c660-12T on 16/08/2016.
*/
public class Tab2 extends Fragment {
GridView gv;
int[] clicknums;
ArrayList<productpojo> productarraylist = null;
Bundle bundle;
//Overriden method onCreateView
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.tab2, container, false);
bundle=this.getArguments();
if (bundle != null) {
productarraylist = bundle.getParcelableArrayList("arraylist");
}
go();
return view;
}
public void go(){
productpojo propoj; = productarraylist.get(0);
String namee = propoj.getName();
String details = propoj.getDetails();
String category = propoj.getcategory();
String ownerr = propoj.getOwner();
String weight = propoj.getWeight();
String size = propoj.getSize();
String price = propoj.getPrice();
String imageurl = propoj.getimageurl();
String owner_address = propoj.getOwner_address();
String owner_phone = propoj.getOwner_phone();
String UID = propoj.getUID();
ProductObj product = new ProductObj();
product.setName(namee);
product.setDetails(details);
product.setcategory(category);
product.setOwner(ownerr);
product.setWeight(weight);
product.setSize(size);
product.setPrice(price);
product.setimageurl(imageurl);
product.setOwner_address(owner_address);
product.setOwner_phone(owner_phone);
product.setUID(UID);
final ArrayList<ProductObj> productlist = new ArrayList<>();
productlist.add(product);
gv = (GridView) getView().findViewById(R.id.grid);
clicknums = new int[productlist.size()];
gv.setAdapter(new CustomGrid(getActivity(), productlist));
gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
int clicknum;
clicknums[position] = clicknums[position] + 1;
clicknum = clicknums[position];
String image = productlist.get(position).imageurl;
String text = productlist.get(position).name;
String owenr = productlist.get(position).owner;
Intent intent = new Intent(getActivity(), Product.class);
intent.putExtra("numofclick", clicknum);
intent.putExtra("title", text);
intent.putExtra("owner", owenr);
intent.putExtra("image", image);
//Start details activity
startActivity(intent);
}
});
}
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
/**
* Created by toshiba c660-12T on 16/08/2016.
*/
//Extending FragmentStatePagerAdapter
public class Pager extends FragmentStatePagerAdapter {
//integer to count number of tabs
int tabCount;
//Constructor to the class
public Pager(FragmentManager fm, int tabCount) {
super(fm);
//Initializing tab count
this.tabCount= tabCount;
}
//Overriding method getItem
@Override
public Fragment getItem(int position) {
//Returning the current tabs
switch (position) {
case 0:
Tab1 tab1 = new Tab1();
return tab1;
case 1:
Tab2 tab2 = new Tab2();
return tab2;
default:
return null;
}
}
//Overriden method getCount to get the number of tabs
@Override
public int getCount() {
return tabCount;
}
}
需要在oncreateview
中调用getarguments()
,以获得使用setarguments
发送的Bundle:
bundle=this.getArguments();
下面的第一个代码块是我的主要活动,我在其中创建了第二个活动的意图。在这个活动中,我将在列表视图中显示费用,因为它还没有完全实现,所以我暂时忽略了它。我想做的就是启动第二个活动,让用户输入详细信息,然后按下一个按钮将活动添加到列表视图中。@override公共布尔onOptionsItemSelected(MenuItem item){int id=item.getItemId(); 这是用户输入数
问题内容: 在我的活动中,我创建了一个对象,然后需要启动另一个对象,如何从子活动中传递该对象(将要启动的对象)? 问题答案: 实现,因此你可以始终按以下意图传递它: 并在另一端检索它:
我尝试使用 如有任何帮助,不胜感激,谢谢。
我想问一下如何将值从片段a传递到活动B,然后再传递回片段。我尝试使用bundle来传递值,但是它会给出错误的数据。 非常感谢。
如何将数据从活动传递到对话框? 有关更多详细信息,请参阅下图:
问题内容: 当我尝试将Servlet中的列表值设置为会话变量并像JSP一样访问它时, 做得到 JSP 我在JSP中遇到错误,因为“列表无法解析为类型” 那我该怎么做呢?我想将列表从Servlet传递到JSP并填充一个下拉列表。 问题答案: 您要导入列表和演员表吗?