gpt4 book ai didi

Android - 使用 swipeview 将数据从一个 fragment 发送到另一个 fragment

转载 作者:行者123 更新时间:2023-11-30 02:55:26 25 4
gpt4 key购买 nike

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

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

我知道我应该通过连接的 MainActivity 进行通信,我已经在 TaskGroupsFragment 中创建了一个接口(interface)并在 Activity 中实现了它。经过试验和测试, Activity 收到 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(发送 fragment ):

    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());
}
}

主要 Activity :

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(接收 fragment ):

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;
}
}
}

解决方案

感谢 Alireza!我不得不对他提出的代码进行几处更改,但最终它帮助我找到了解决方案!

主页面适配器:

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;
}
}

主要 Activity :

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(接收 fragment ):

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 用作字符串,而不是整数。

最佳答案

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

public class MainPagerAdapter extends FragmentStatePagerAdapter {
private int taskGroupId;


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

然后在您的 Activity 中存储对您的适配器的引用。现在只需在 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

}

然后您需要在开始您的 fragment 之前添加一些参数。

    @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 中使用此数据来显示正确的内容。

关于Android - 使用 swipeview 将数据从一个 fragment 发送到另一个 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23368061/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com