0

我正在做一些需要将所选项目的 ID 发送到我的一个布局中包含的片段的事情。我确实知道片段的基本知识,但我不知道如何正确地从 Activity 到 Fragment 进行通信。好吧,这是我现在的代码:

    public class GroupActivity extends FragmentActivity {

    Button information, back;
    TextView group_name;
    String name, group_description, group_administrator,image_id;
    public static String group_id;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.group_screen);
        Intent intent = getIntent();

        name = intent.getStringExtra("group_name");
        group_description = intent.getStringExtra("group_description");
        group_administrator = intent.getStringExtra("group_administrator");
        image_id = intent.getStringExtra("image_id");
        group_id = intent.getStringExtra("group_id");

        information = (Button)findViewById(R.id.btn_information);
        back = (Button)findViewById(R.id.btn_back);
        group_name = (TextView)findViewById(R.id.txt_group_name);

        Toast.makeText(getBaseContext(),"The group id "+group_id,Toast.LENGTH_SHORT).show();


        group_name.setText(name);

        information.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(GroupActivity.this, GroupInformationActivity.class);
                intent.putExtra("group_name",name);
                intent.putExtra("group_description",group_description);
                intent.putExtra("group_administrator",group_administrator);
                intent.putExtra("image_id",image_id);
                startActivity(intent);
                GroupActivity.this.overridePendingTransition(R.anim.in_from_left, R.anim.out_to_right);
            }
        });

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onBackPressed();
            }
        });
    }



    private void showCreateNewTopicDialog() {
        FragmentManager fm = getSupportFragmentManager();
        DialogNewTopic newTopic = new DialogNewTopic();
        newTopic.show(fm, "new_topic");
    }

    @Override
    public void onBackPressed(){
        super.onBackPressed();
        overridePendingTransition(R.anim.in_from_right,R.anim.out_to_left);
    }

}

和片段

public class GroupConversationsFragment extends ListFragment {

    final ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();

    String[][] conversation_header = {{"0","Topic 1","10/03/2013 10:21:am","2","0"},{"0","Topic 2","10/04/2013 1:29:pm","5","0"}};
    String group_id;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        setConversations();
        return inflater.inflate(R.layout.conversations_list_view, container, false);
    }


    public void onListItemClick(ListView l, View v, int position, long id) {
        Intent intent = new Intent(getActivity(), ConversationActivity.class);
        startActivity(intent);
        getActivity().overridePendingTransition(R.anim.in_from_left, R.anim.out_to_right);
    }

    private void setConversations(){
        list.clear();
        SimpleAdapter adapter = new SimpleAdapter(
                getActivity(),
                list,
                R.layout.custom_list_main_conversations,
                new String[] {"message","date", "reply_count", "stars_count"},
                new int[] {R.id.txt_conversation_message,R.id.txt_topic_date, R.id.txt_no_of_reply, R.id.txt_no_of_stars}
        );

        for(int ctr=0; ctr < conversation_header.length; ctr++){
            Log.v("The header group id", conversation_header[ctr][0]);

            if(conversation_header[ctr][0].equals(group_id)){
                HashMap<String,String> item_list = new HashMap<String,String>();
                item_list.put("message", conversation_header[ctr][1]);
                item_list.put("date", conversation_header[ctr][2]);
                item_list.put("reply_count", conversation_header[ctr][3]);
                item_list.put("stars_count", conversation_header[ctr][4]);
                list.add(item_list);
            }
        }


        setListAdapter(adapter);
    }

}

为了进一步解释,我在这里所做的是,活动先从活动中获取 stringExtra,然后再将其设置在变量上。现在这个活动有它自己的布局,其中包含 Listfragment,因为我使用的是自定义列表。然后我需要将 group_id 传递给片段以根据 group_id 显示正确的主题。到目前为止,我尝试的是使用 Activity 中的静态字符串变量并在我的 Fragment 上访问它,但我认为 Fragment Lifecycle 存在一个问题,它没有正确设置 id。希望有人能给我一个直截了当的答案,告诉我如何实现这一目标。

4

1 回答 1

1
  • 您可以创建一个Activity扩展基本活动并访问片段中活动的所需方法或字段的方法。
  • 你可以去广播接收器。
  • Activity您可以通过 Intent发送数据,如下所示:

    Bundle bundle = new Bundle();
    bundle.putString("key","value - from the activity");
    
    GroupConversationsFragment fragmentObject = new GroupConversationsFragment();
    fragmentObject.setArguments(bundle);
    
        -> in GroupConversationsFragment onCreateView():
    
    @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
    
                String val = getArguments.getString("key");
    
                return inflater.inflate(R.layout.fragment, container, false);
            }
    
于 2013-10-03T10:06:37.837 回答