不要在片段中显示列表视图项
Posted
技术标签:
【中文标题】不要在片段中显示列表视图项【英文标题】:Don't show listview item in fragment 【发布时间】:2021-09-29 23:49:49 【问题描述】:我把一个listView放在一个fragment中,把一个fragment放在activiti_main.xml中,从volley库中得到一个json文件,然后解析那个json文件并用listView显示它,下面的代码工作正常。
MainActivity.java
public class MainActivity extends AppCompatActivity
public ListView listView;
private RequestQueue queue;
private ArrayList<Category> categories;
public CategoryAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
queue = Volley.newRequestQueue(MainActivity.this);
String url = "***";
StringRequest request = new StringRequest(Request.Method.GET, url, new Response.Listener<String>()
@Override
public void onResponse(String response)
CategoriesJsonParser jsonParser = new CategoriesJsonParser();
try
categories = jsonParser.parseJson(response);
adapter = new CategoryAdapter(getApplicationContext(), categories, R.layout.category_item);
listView.setAdapter(adapter);
catch (JSONException e)
e.printStackTrace();
, new Response.ErrorListener()
@Override
public void onErrorResponse(VolleyError error)
);
queue.add(request);
但是当我在片段中编写相同的代码时 程序关闭后我该怎么办?
BlankFragment.java
public class BlankFragment extends Fragment
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
public ListView listView;
private RequestQueue queue;
private ArrayList<Category> categories;
public CategoryAdapter adapter;
public BlankFragment()
// Required empty public constructor
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment BlankFragment.
*/
// TODO: Rename and change types and number of parameters
public static BlankFragment newInstance(String param1, String param2)
BlankFragment fragment = new BlankFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
if (getArguments() != null)
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
listView = getActivity().findViewById(R.id.listview);
queue = Volley.newRequestQueue(getActivity());
String url = "https://dikushop.ir/android/category/getCategories.php";
StringRequest request = new StringRequest(Request.Method.GET, url, new Response.Listener<String>()
@Override
public void onResponse(String response)
CategoriesJsonParser jsonParser = new CategoriesJsonParser();
try
categories = jsonParser.parseJson(response);
adapter = new CategoryAdapter(getContext(), categories, R.layout.category_item);
// listView.setAdapter(adapter);
catch (JSONException e)
e.printStackTrace();
, new Response.ErrorListener()
@Override
public void onErrorResponse(VolleyError error)
);
queue.add(request);
listView.setAdapter(adapter);
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_blank, container, false);
【问题讨论】:
【参考方案1】:首先,从片段代码中删除下面不必要的部分
`
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment BlankFragment.
*/
// TODO: Rename and change types and number of parameters
public static BlankFragment newInstance(String param1, String param2)
BlankFragment fragment = new BlankFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
@Override
public void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
if (getArguments() != null)
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
`
并将其添加到 onCreateView
View view = inflater.inflate(your xml which contain list view eg. R.layout.list, container, false);
最后
//return view which you inflated
return view;
【讨论】:
以上是关于不要在片段中显示列表视图项的主要内容,如果未能解决你的问题,请参考以下文章