1

可能这是一个骗局,但我一直在寻找解决方案,它们总是与我的问题略有不同。

所以:我目前正在创建一个具有 2 个可滑动片段的应用程序。TaskGroupFragment 显示一个列表,当您单击一个项目时,它会滑动到 TasksFragment 并显示一个子列表。我现在要做的是将所选项目的 id 从组发送到任务,以便我可以从 SQLite 中获取子列表。

我知道我应该通过连接的 MainActivity 进行通信,并且我已经在 TaskGroupsFragment 中创建了一个接口并在活动中实现了它。经过尝试和测试,活动收到了 TaskGroupID。

我被卡住的部分是在 TasksFragment 中获取此信息。尤其是使用 swipeview 会使这更难。

我的代码:

MainPagerAdapter:

public class MainPagerAdapter extends FragmentStatePagerAdapter {

    public MainPagerAdapter(FragmentManager fm) {
    super(fm);
    }

    @Override
    public Fragment getItem(int i) {
        switch (i) {
            case 0: return TaskGroupFragment.newInstance();
            case 1: return TasksFragment.newInstance();
            default: return TaskGroupFragment.newInstance();
        }
    }

    @Override
    public int getCount() {
        return 2;
    }
}

TaskGroupActivity(发送片段):

    public class TaskGroupFragment extends ListFragment {

    private DoItDataSource dataSource;
    private List<TaskGroups> groups;

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

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_task_group, container, false);

        dataSource = new DoItDataSource(getActivity());

        dataSource.open();
        JSONContainer jsonContainer = dataSource.sqliteToContainer();
        dataSource.close();

        groups = jsonContainer.getTask_groups();

        TaskGroupAdapter adapter = new TaskGroupAdapter(getActivity(), groups);
        setListAdapter(adapter);

        return view;
    }

    public static TaskGroupFragment newInstance() {
        TaskGroupFragment tgf = new TaskGroupFragment();
        return tgf;
    }

    public interface OnTaskGroupSelectedListener {
        public void onTaskGroupSelected(String taskGroupId);
    }

    OnTaskGroupSelectedListener mListener;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mListener = (OnTaskGroupSelectedListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " Interface not implemented in activity");
        }
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {

        ((MainActivity)getActivity()).setCurrentItem(1, true);
        mListener.onTaskGroupSelected(groups.get(position).getId());
    }
}

主要活动:

public class MainActivity extends FragmentActivity  implements
        TaskGroupFragment.OnTaskGroupSelectedListener{
    private SharedPreferences savedValues;
    private DoItDataSource dataSource = new DoItDataSource(this);

    private String identifier, user, domain;

    private JSONContainer containerToday;
    private JSONContainer containerTomorrow;

    public ViewPager pager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        savedValues = getSharedPreferences("SavedValues", MODE_PRIVATE);
        identifier = savedValues.getString("Identifier", "");

        pager = (ViewPager) findViewById(R.id.activity_main_pager);
        pager.setAdapter(new MainPagerAdapter(getSupportFragmentManager()));

        if (identifier == null || identifier.equals("")) {
            Intent intent = new Intent(MainActivity.this, LoginActivity.class);
            intent.putExtra("APP_ID", APP_ID);
            startActivity(intent);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();

        identifier = savedValues.getString("Identifier", "");
        user = savedValues.getString("User", "");
        domain = savedValues.getString("Domain", "");
        boolean onBackPressed = savedValues.getBoolean("OnBackPressed", false);

        //
        // getting lists
        //
    }

    private void resultHandling(String json, String day) {
        if (day.equals("today")) {
            Gson gson = new Gson();
            containerToday = gson.fromJson(json, JSONContainer.class);
            jsonToSQLite(containerToday, "Today");


        } else if (day.equals("tomorrow")) {
            Gson gson = new Gson();
            containerTomorrow= gson.fromJson(json, JSONContainer.class);
            jsonToSQLite(containerTomorrow, "Tomorrow");
        }
    }

    String taskGroupId = "";

    @Override
    public void onTaskGroupSelected(String taskGroupId) {
        this.taskGroupId = taskGroupId;


    // Enter missing link here?

    }
}

TaskFragment(接收片段):

public class TasksFragment extends ListFragment
        implements OnClickListener {
    private final static String TAG = "TaskItemFragment logging";

    private DoItDataSource dataSource;
    private List<Tasks> tasks;

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

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_task_item, container, false);

        Button backButton = (Button) 
                view.findViewById(R.id.fragment_task_item_bar_back_button);

        dataSource = new DoItDataSource(getActivity());

        dataSource.open();
        tasks = dataSource.getTasks("204"); // 204 is a placeholder, TaskGroupId should be here
        dataSource.close();

        TasksAdapter adapter = new TasksAdapter(getActivity(), tasks);
        setListAdapter(adapter);

        backButton.setOnClickListener(this);

        return view;
    }

    public static TasksFragment newInstance() {
        TasksFragment tif = new TasksFragment();
        return tif;
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        Toast.makeText(getActivity(), "Clicked item " + position, Toast.LENGTH_LONG).show();
    }

    @Override
    public void onClick(View v) {
        switch(v.getId()) {
            case R.id.fragment_task_item_bar_back_button:
                ((MainActivity)getActivity()).setCurrentItem(0, true);
                break;
        }
    }
}

解决方案

感谢阿里雷萨!我不得不对他提出的代码进行一些更改,但最终它帮助我找到了解决方案!

主页面适配器:

public class MainPagerAdapter extends FragmentStatePagerAdapter {
    // ADDED
    private String taskGroupId;

    public MainPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int i) {
        switch (i) {
            case 0: return TaskGroupFragment.newInstance();
            // MODIFIED
            case 1:
                Bundle args = new Bundle();
                logcat("before setBundle " + taskGroupId);
                args.putString("taskGroupId",taskGroupId);
                Fragment fragment  =  new TasksFragment();
                fragment.setArguments(args);
                return fragment;
            default: return TaskGroupFragment.newInstance();
        }
    }

    // ADDED
    public void setTaskGroupId(String id){
        this.taskGroupId = id;
    }

    @Override
    public int getCount() {
        return 2;
    }
}

主要活动:

public class MainActivity extends FragmentActivity  implements
        TaskGroupFragment.OnTaskGroupSelectedListener{
    private SharedPreferences savedValues;

    private DoItDataSource dataSource = new DoItDataSource(this);

    private String identifier, user, domain;

    private JSONContainer containerToday;
    private JSONContainer containerTomorrow;

    // ADDED
    private MainPagerAdapter adapter;

    public ViewPager pager;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            savedValues = getSharedPreferences("SavedValues", MODE_PRIVATE);
            identifier = savedValues.getString("Identifier", "");

            // ADDED
            adapter = new MainPagerAdapter(getSupportFragmentManager());

            pager = (ViewPager) findViewById(R.id.activity_main_pager);
            // MODIFIED
            pager.setAdapter(adapter);

            if (identifier == null || identifier.equals("")) {
                Intent intent = new Intent(MainActivity.this, LoginActivity.class);
            intent.putExtra("APP_ID", APP_ID);
            startActivity(intent);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();

        identifier = savedValues.getString("Identifier", "");
        user = savedValues.getString("User", "");
        domain = savedValues.getString("Domain", "");
        boolean onBackPressed = savedValues.getBoolean("OnBackPressed", false);

        //
        // Getting lists
        //
    }


    String taskGroupId = "";

    @Override
    public void onTaskGroupSelected(String taskGroupId) {
        this.taskGroupId = taskGroupId;

        // ADDED
        adapter.setTaskGroupId(taskGroupId);
        pager.setAdapter(adapter);
        pager.setCurrentItem(1);
    }
}

TaskFragment(接收片段):

public class TasksFragment extends ListFragment implements OnClickListener {
    private final static String TAG = "TaskItemFragment logging";

    private DoItDataSource dataSource;
    private List<Tasks> tasks;

    private String taskGroupId;

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

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_task_item, container, false);

        Button backButton = (Button) view.findViewById(R.id.fragment_task_item_bar_back_button);

        dataSource = new DoItDataSource(getActivity());

        // ADDED
        Bundle bundle = getArguments();
        taskGroupId = bundle.getString("taskGroupId");

        // MODIFIED
        dataSource.open();
        tasks = dataSource.getTasks(taskGroupId);
        dataSource.close();

        TasksAdapter adapter = new TasksAdapter(getActivity(), tasks);
        setListAdapter(adapter);

        backButton.setOnClickListener(this);

        return view;
    }

    // CAN BE REMOVED?
    //public static TasksFragment newInstance() {
    //    TasksFragment tif = new TasksFragment();
    //    return tif;
    //}

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        Toast.makeText(getActivity(), "Clicked item " + position, Toast.LENGTH_LONG).show();
    }

    @Override
    public void onClick(View v) {
        switch(v.getId()) {
            case R.id.fragment_task_item_bar_back_button:
                ((MainActivity)getActivity()).setCurrentItem(0, true);
                break;
        }
    }
}

请注意,我使用 taskGroupId 作为字符串,而不是 int。

4

1 回答 1

1

首先,您需要确保您的适配器知道 taskGroupID。只需向您的适配器添加一个变量和一个公共方法。

public class MainPagerAdapter extends FragmentStatePagerAdapter {
private int taskGroupId;


public void setTaskGroupId(int id){
   this.taskGroupId = id;
}
}

然后在您的活动中存储对您的适配器的引用。现在只需在 GroupId 更改时调用此方法

    @Override
public void onTaskGroupSelected(String taskGroupId) {
    this.taskGroupId = taskGroupId;
    adapter.setTastGroupId = taskGroupId; //data needed to initialize fragment. 
    adapter.setCurrentItem(1); //going to TasksFragment page

}

那么你需要在开始你的片段之前放置一些参数。

    @Override
    public Fragment getItem(int i) {
        //this code is only for case 1:
        Bundle args = new Bundle();
        args.putInt("taskGroupId",taskGroupId);
        Fragment fragment =  new TasksFragment();
        fragment.setArguments(args);
        return fragment;
    }

最后在您的 TaskFragment 中使用这些数据来显示正确的内容。

于 2014-04-29T17:14:22.187 回答