尝试将用户ID从活动发送到视图持有者片段。

huangapple go评论76阅读模式
英文:

Trying to send the userid from the activity to the viewholder fragment

问题

以下是已翻译的内容:

我已经创建了一个安卓应用程序,在其中添加了一个名为profileview.java的活动,在这个活动中,用户可以查看自己或其他用户的个人资料数据。Profileview 活动包含两个片段,即 postlikes,在这两个片段中,我需要展示用户发布的帖子和用户点赞的帖子,但是不知道如何将用户的用户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传递给posttimelinelike片段。

英文:

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

  1. settingtabAdopter 构造函数中作为参数传递 userid
public settingtabAdopter(@NonNull FragmentManager fm, String userid)

profileviewActivityonCreate 方法中:

Tabadapter = new settingtabAdopter(getSupportFragmentManager(), userid);
  1. settingtabAdopter 内部创建私有变量:
private String userid;

在构造函数中进行初始化:

this.userid = userid;
  1. 在每个片段中创建私有变量,并添加设置器(对于 posttimelinelike 片段):
private String mUserid;

void setMUserId(String mUserid){
    this.mUserid = mUserid;
}
  1. 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;
    }
}
英文:
  1. pass userid as parameter at settingtabAdopter constructor

    public settingtabAdopter(@NonNull FragmentManager fm, String userid) 
    

profileviewActivity -> onCreate

    Tabadapter = new settingtabAdopter(getSupportFragmentManager(), userid);
  1. make private variable inside settingtabAdopter

    private String userid;
    

initialize it inside constructor

    this.userid = userid
  1. Create private variable in each fragment and add setter (posttimeline and like) fragments

    private String mUserid;
    void setMUserId(String mUserid){
        this.mUserid = mUserid;
    }
    
  2. On settingtabAdopter inside getItem function after initialize each fragment set the private varaible with the 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;
        }
    }
    

huangapple
  • 本文由 发表于 2020年3月15日 14:03:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/60690179.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定