我有一些布局,我需要创建许多视图来将tham添加到我的gridLayout里面的片段中。
我现在有这个:
ArrayList<Tables> tables;
ArrayList<View> tableList = new ArrayList<>();
tables = parsedArray.get(0).getTables();
GridLayout gridLayout = (GridLayout) getView().findViewById(R.id.grid_layout);
for (int i = 0; i < tables.size(); i++) {
tableList.add(getView().findViewById(R.id.table_layout));
//TextView textView = (TextView) tableList.get(i).findViewById(R.id.table_number);
//textView.setText(tables.get(i).getTableNumber());
GridLayout.Spec row = GridLayout.spec(i / 5);
GridLayout.Spec col = GridLayout.spec(i);
GridLayout.LayoutParams params = new GridLayout.LayoutParams(row, col);
gridLayout.addView(tableList.get(i), i, params);
}
}
但它错误的方式,所以我需要找出如何创建新的视图并将它们添加到我的列表中,在这种状态下,我认为我要引用相同的视图,因为我有这种类型的错误:< / p>
Cannot add a null child view to a ViewGroup
片段代码:
public static class tablesFragment extends Fragment {
private VolleySinglton volleySinglton;
private RequestQueue requestQueue;
public static tablesFragment getInstance(int position) {
tablesFragment myTablesFragment = new tablesFragment();
Bundle args = new Bundle();
args.putInt("position", position);
myTablesFragment.setArguments(args);
return myTablesFragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View layout = inflater.inflate(R.layout.fragment_1, container, false);
GridLayout gridLayout = (GridLayout) layout.findViewById(R.id.grid_layout);
volleySinglton = VolleySinglton.returnInstance();
requestQueue = volleySinglton.getRequestQueue();
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET,
url,
(String) null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
ArrayList<RoomsClass> parsedArray = parseRoomsResponse(response);
updateViews(parsedArray);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
if (error instanceof TimeoutError) {
}
}
});
requestQueue.add(jsonObjectRequest);
return layout;
}
private void updateViews(ArrayList<RoomsClass> parsedArray) {
// Extract relevant data from the array
if (parsedArray == null || parsedArray.isEmpty()) {
} else {
ArrayList<Tables> tables;
ArrayList<View> tableList = new ArrayList<>();
tables = parsedArray.get(0).getTables();
GridLayout gridLayout = (GridLayout) getView().findViewById(R.id.grid_layout);
for (int i = 0; i < tables.size(); i++) {
tableList.add(getView().findViewById(R.id.table_layout));
//TextView textView = (TextView) tableList.get(i).findViewById(R.id.table_number);
//textView.setText(tables.get(i).getTableNumber());
GridLayout.Spec row = GridLayout.spec(i / 5);
GridLayout.Spec col = GridLayout.spec(i);
GridLayout.LayoutParams params = new GridLayout.LayoutParams(row, col);
gridLayout.addView(tableList.get(i), i, params);
}
}
// Replace the place holders in the relevant views
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
@Override
public void onResume() {
super.onResume();
}
public interface VolleyCallback {
void onSuccess(ArrayList<RoomsClass> result);
}
private ArrayList<RoomsClass> parseRoomsResponse(JSONObject response) {
ArrayList<RoomsClass> roomsArray = new ArrayList<>();
try {
JSONArray rooms = response.getJSONArray("rooms");
for (int i = 0; i < rooms.length(); i++) {
JSONObject currentRoom = rooms.getJSONObject(i);
RoomsClass room = new RoomsClass();
room.setRoomId(currentRoom.getInt("room_id"));
room.setRoomLabel(currentRoom.getString("label"));
JSONArray tables = currentRoom.getJSONArray("tables");
for (int j = 0; j < tables.length(); j++) {
JSONObject currentTable = tables.getJSONObject(j);
room.addTable(currentTable.getInt("table_id"), currentTable.getInt("table_number"));
}
roomsArray.add(room);
}
} catch (JSONException e) {
Toast.makeText(getActivity(), e.toString(), Toast.LENGTH_LONG).show();
}
return roomsArray;
}
}
可能还有一些问题因为我现在正在编辑它。
我做了这个chaneges:
View view = getActivity().getLayoutInflater().inflate(R.layout.table_layout, gridLayout, false);
tableList.add(view);
当我试图添加视图时,事情就好了。
P.S。 我还没有提出问题,因为有些麻烦。