【发布时间】:2019-10-10 13:40:19
【问题描述】:
我想提高我的代码效率,这就是我试图简化事情的原因(请随时在 KOTLIN 中帮助我)
这是关于一个餐厅预订系统,我有 2 个大房间,每个房间都有一定数量的桌子(=我的配置中的硬编码数字)。 我正在尝试为每个表获取一个选项卡,其中每个选项卡都包含一个 ListView,它通过 SQLite DB 获取他的信息。
我目前正在做的是,我为每个房间设置了 2 个单独的登录页面,并且为每张桌子编写了一个标签。这当然不是这样做的方法,但是,是的,这就是我在这里寻求您帮助的原因:
主页使用 SectionsPageAdapter 进行片段处理,如您所见,我已经实现了包含“Room 1”或“Room 2”的“currentLocation”字符串。
private SectionsPageAdapter mSectionsPageAdapter;
private ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_landing_one);
Intent mIntent = getIntent();
Bundle extras = mIntent.getExtras();
final String currentLocation = extras.getString("currentLocation");
mSectionsPageAdapter = new SectionsPageAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter
mViewPager = (ViewPager) findViewById(R.id.container);
setupViewPager(mViewPager);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
}
private void setupViewPager (ViewPager viewPager) {
SectionsPageAdapter adapter = new SectionsPageAdapter(getSupportFragmentManager());
if(currentLocation.equals(config.roomone) {
// for loop?
adapter.addFragment(new table_new_main(),"INFO");
adapter.addFragment(new table_one(),"TABLE 1");
adapter.addFragment(new table_two(),"TABLE 2");
adapter.addFragment(new table_three(),"TABLE 3");
} else { ... }
viewPager.setAdapter(adapter);
}
}
问题 1
if 循环完成了他的工作,现在我如何根据给定的表数设置动态的 Fragment 数量?使用 for 循环?
现在是片段(这是我卡住的棘手部分):
公共类 table_one 扩展片段 {
private ListView roomlist;
ListAdapter JSON_List;
ArrayList<String> Array_dates;
ArrayList<String> Array_table,Array_dayid,Array_location;
String currentLocation;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.table, container, false);
roomlist = (ListView) view.findViewById(R.id.list_table);
String id = config.ID_table3; // I hardcoded each table number, how can I pass that through my landing page to get it here?
AppDatabase db = Room.databaseBuilder(getContext(),
AppDatabase.class, config.DB_TABLES).allowMainThreadQueries().build();
Array_dates = new ArrayList<String>();
Array_table = new ArrayList<String>();
Array_dayid = new ArrayList<String>();
Array_location = new ArrayList<>();
for(int i=0;i<value.size();i++) {
Array_dates.add(value.get(i).getDaysDate());
Array_table.add(value.get(i).getDaysPRX());
Array_dayid.add(value.get(i).getDayid());
Array_location.add(value.get(i).getLocation_id());
}
JSON_List = new RoomListAdapter(getContext(),id,Array_dates,null,null,Array_table,null,Array_dayid,Array_location,currentLocation);
roomlist.setAdapter(JSON_List);
return view;
}
问题 2
所以我需要传递给每个片段:currentLocation(房间 1/2)和它自己的 table_id,以了解从我的数据库中获取数据的位置。 我该怎么做?
【问题讨论】:
-
你应该考虑使用 recyclerview 而不是列表视图
-
嗯,你认为这对我解决这个问题有帮助吗?我的意思是,这如何使我更容易获得解决方案:)?
-
你的问题对我来说有点宽泛,所以我只是快速查看了一下,看看发生了什么,总的来说,我建议人们从列表视图切换到回收站视图,因为它是谷歌出于各种原因推荐:) 我会看看我现在是否可以就如何让这个更好地为你提供一些一般性建议
-
好的,更新了我的答案,试一试:D
标签: java android android-fragments kotlin fragment