我目前在列表视图中显示子项时遇到问题。我不确定是什么导致了这一点。我试过玩.put
但是没有成功。
这是我的代码:
public class HomeFragment extends Fragment {
private ListView lv_shifts;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
((MainActivity)getActivity()).setActionBarTitle("WorkMi");
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_home, container, false);
lv_shifts = view.findViewById(R.id.shfits_lv);
HashMap<String,String> day_Time = new HashMap<String, String>();
day_Time.put("4/12/18","7AM - 2PM" );
day_Time.put("4/14/18","5PM - 10PM" );
day_Time.put("4/15/18","10AM - 2PM" );
day_Time.put("4/16/18","7AM - 4PM" );
day_Time.put("4/18/18","5AM - 2PM" );
day_Time.put("4/21/18","7AM - 2PM" );
List<HashMap<String, String>> listItems = new ArrayList<>();
SimpleAdapter adapter = new SimpleAdapter(getContext(), listItems, R.layout.shift_list,
new String[]{"First Line", "Second Line"},
new int[]{R.id.date_tv, R.id.time_tv});
Iterator it = day_Time.entrySet().iterator();
while (it.hasNext()){
HashMap<String,String> resultMap = new HashMap<String, String>();
Map.Entry pair = (Map.Entry)it.next();
resultMap.put("First Line", pair.getKey().toString());
resultMap.put("Second Line", pair.getValue().toString());
listItems.add(resultMap);
}
lv_shifts.setAdapter(adapter);
return view;
}
}
答案 0 :(得分:0)
您在将项目添加到listview
之前将listItem添加到适配器,导致空listView
。在实例化适配器之前添加while
循环,然后setAdapter
。
答案 1 :(得分:0)
您可以在一行中以简单的视图显示数据。为什么需要hashMap?
String[] day_Time = new String[] { "4/12/18 7AM - 2PM",
"4/14/18 5PM - 10PM",
"4/15/18 10AM - 2PM",
"4/16/18 7AM - 4PM",
"4/18/18 5AM - 2PM",
"4/21/18 7AM - 2PM"}
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, day_Time);
lv_shifts = view.findViewById(R.id.shfits_lv);
// Set Adapter to List View to display items
lv_shifts.setAdapter(adapter);