英文:
Trying to send the userid from the activity to the viewholder fragment
问题
以下是已翻译的内容:
我已经创建了一个安卓应用程序,在其中添加了一个名为profileview.java
的活动,在这个活动中,用户可以查看自己或其他用户的个人资料数据。Profileview
活动包含两个片段,即 post
和 likes
,在这两个片段中,我需要展示用户发布的帖子和用户点赞的帖子,但是不知道如何将用户的用户ID从 profileview
活动发送到另外两个片段中。
profileviewactivity.class
:
public class profileviewActivity extends AppCompatActivity {
public String userid;
private ViewPager viewPager;
private TabLayout tabLayout;
private settingtabAdopter Tabadapter;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profileview);
viewPager = (ViewPager) findViewById(R.id.viewpager);
Tabadapter = new settingtabAdopter(getSupportFragmentManager());
viewPager.setAdapter(Tabadapter);
tabLayout = (TabLayout) findViewById(R.id.tablayout);
tabLayout.setupWithViewPager(viewPager);
userid = getIntent().getExtras().get("userid").toString();
}
}
settingadapter.java
:
public class settingtabAdopter extends FragmentPagerAdapter {
public settingtabAdopter(@NonNull FragmentManager fm) {
super(fm);
}
@NonNull
@Override
public Fragment getItem(int position) {
switch (position){
case 0:
posttimeline postfragment = new posttimeline();
Bundle postBundle = new Bundle();
postBundle.putString("userid", userid);
postfragment.setArguments(postBundle);
return postfragment;
case 1:
like chatfragment = new like();
Bundle likeBundle = new Bundle();
likeBundle.putString("userid", userid);
chatfragment.setArguments(likeBundle);
return chatfragment;
default:
return null;
}
}
@Override
public int getCount() {
return 2;
}
@Nullable
@Override
public CharSequence getPageTitle(int i) {
switch (i){
case 0:
return "Posts";
case 1:
return "Likes";
default:
return null;
}
}
}
posttimeline.java
:
public class posttimeline extends Fragment {
View posttimelineview;
private String userid;
public posttimeline() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
userid = getArguments().getString("userid");
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
posttimelineview = inflater.inflate(R.layout.fragment_posttimeline, container, false);
return posttimelineview;
}
}
like.java
:
public class like extends Fragment {
View Likeview;
private String userid;
public like() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
userid = getArguments().getString("userid");
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
Likeview = inflater.inflate(R.layout.fragment_like, container, false);
return Likeview;
}
}
我想要从profileviewactivity
将用户ID传递给posttimeline
和like
片段。
英文:
I have created an android app in which I have added an activity called profileview.java on which users can view there or other user's profile data. Profileview activity contains two fragments post and likes on which I have to show the post that is been posted by the user and the post liked by the user but not getting any idea how to send the userid from the profileview activity to the other two fragments.
profileviewactivity.class
public class profileviewActivity extends AppCompatActivity {
public String userid;
private ViewPager viewPager;
private TabLayout tabLayout;
private settingtabAdopter Tabadapter;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profileview);
viewPager = (ViewPager) findViewById(R.id.viewpager);
Tabadapter = new settingtabAdopter(getSupportFragmentManager());
viewPager.setAdapter(Tabadapter);
tabLayout = (TabLayout) findViewById(R.id.tablayout);
tabLayout.setupWithViewPager(viewPager);
userid = getIntent().getExtras().get("userid").toString();
}
}
settingadapter.java
public class settingtabAdopter extends FragmentPagerAdapter {
public settingtabAdopter(@NonNull FragmentManager fm) {
super(fm);
}
@NonNull
@Override
public Fragment getItem(int position) {
switch (position){
case 0:
posttimeline postfragment = new posttimeline();
return postfragment;
case 1:
like chatfragment = new like();
return chatfragment;
default:
return null;
}
}
@Override
public int getCount() {
return 2;
}
@Nullable
@Override
public CharSequence getPageTitle(int i) {
switch (i){
case 0:
return "Posts";
case 1:
return "Likes";
default:
return null;
}
}
}
posttimeline.java
public class posttimeline extends Fragment {
View posttimelineview;
public posttimeline() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
posttimelineview = inflater.inflate(R.layout.fragment_posttimeline, container, false);
return posttimelineview;
}
}
like.java
public class like extends Fragment {
View Likeview;
public like() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
Likeview = inflater.inflate(R.layout.fragment_like, container, false);
return Likeview;
}
}
i want to send userid from the profileaviewactivity to the posttimeline and like fragment.
答案1
得分: 0
- 在
settingtabAdopter
构造函数中作为参数传递userid
:
public settingtabAdopter(@NonNull FragmentManager fm, String userid)
在 profileviewActivity
的 onCreate
方法中:
Tabadapter = new settingtabAdopter(getSupportFragmentManager(), userid);
- 在
settingtabAdopter
内部创建私有变量:
private String userid;
在构造函数中进行初始化:
this.userid = userid;
- 在每个片段中创建私有变量,并添加设置器(对于
posttimeline
和like
片段):
private String mUserid;
void setMUserId(String mUserid){
this.mUserid = mUserid;
}
- 在
settingtabAdopter
中的getItem
函数中,在初始化每个片段之后,使用userid
设置私有变量:
public Fragment getItem(int position) {
switch (position){
case 0:
posttimeline postfragment = new posttimeline();
postfragment.setMUserId(userid);
return postfragment;
case 1:
like chatfragment = new like();
chatfragment.setMUserId(userid);
return chatfragment;
default:
return null;
}
}
英文:
-
pass
userid
as parameter atsettingtabAdopter
constructorpublic settingtabAdopter(@NonNull FragmentManager fm, String userid)
profileviewActivity -> onCreate
Tabadapter = new settingtabAdopter(getSupportFragmentManager(), userid);
-
make private variable inside
settingtabAdopter
private String userid;
initialize it inside constructor
this.userid = userid
-
Create private variable in each fragment and add setter (posttimeline and like) fragments
private String mUserid; void setMUserId(String mUserid){ this.mUserid = mUserid; }
-
On
settingtabAdopter
insidegetItem
function after initialize each fragment set the private varaible with theuserid
public Fragment getItem(int position) { switch (position){ case 0: posttimeline postfragment = new posttimeline(); postfragment.setMUserId(userid) return postfragment; case 1: like chatfragment = new like(); chatfragment.setMUserId(userid) return chatfragment; default: return null; } }
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论