【问题标题】:Don't show my ListView with custom adapter in a Fragment不要在片段中显示带有自定义适配器的 ListView
【发布时间】:2018-10-16 02:11:50
【问题描述】:

我在 Fragment 中显示带有自定义适配器的 listView 时遇到问题,此代码在活动中有效,但在我的 Fragment 中无效,当我运行代码时不显示任何错误。 同样,此代码在活动中工作,使用适配器正确显示 listView 并显示从 JsonObjectRequest 获得的每个数据的 Toast,但在 Fragment 中不只显示 listView 而是正确显示 Toast。我将片段的背景颜色设置为红色,但这会显示白色屏幕和 Toast。(对不起,我的英语不好)。

public class ComplaintsFragment extends Fragment {
ListView lstCategories;
private RequestQueue queue;
private String url="http://192.168.1.3:8000/api/tipos";
private ArrayAdapter<CategoriaModel> categoriaAdapter;
List<CategoriaModel> categories;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup 
container,@Nullable Bundle savedInstanceState) {
    final View view= inflater.inflate(R.layout.fragment_complaints, 
    container, false);
    queue=Volley.newRequestQueue(view.getContext());
    categories=new ArrayList<>();
    Toast.makeText(view.getContext(), "Hello you're Fragment", 
    Toast.LENGTH_SHORT).show();
    lstCategories=(ListView) view.findViewById(R.id.lstCategories);
    JsonObjectRequest jsonObjectRequest = new 
    JsonObjectRequest(Request.Method.GET, url, null, new 
    Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            try {
                JSONArray jsonArray = response.getJSONArray("data");
                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                    int idcategory=jsonObject.getInt("idcategory");
                    String name = jsonObject.getString("name");
                    String details= jsonObject.getString("details");
                    CategoriaModel category=new 
                    CategoriaModel(idcategory,name,details);
                    Toast.makeText(view.getContext(), 
                    category.toString(), Toast.LENGTH_SHORT).show();
                    categories.add(category);
                }
                categoriaAdapter=new 
                CategoriaAdapter(getActivity(), 
                android.R.layout.simple_list_item_1,categories);
                lstCategories.setAdapter(categoriaAdapter);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
     }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

        }
    });
    queue.add(jsonObjectRequest);
    return view;
 }
}

这是我的列表视图适配器

public class CategoriaAdapter extends ArrayAdapter<CategoriaModel> {
    public CategoriaAdapter(@NonNull Context context, int resource, @NonNull List<CategoriaModel> objects) {
        super(context, resource, objects);
    }

    @NonNull
    @Override
        public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
            CategoriaModel objItem=getItem(position);
            LayoutInflater inflater= (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View contenedor=inflater.inflate(R.layout.item_category,parent,false);
            TextView lblNombre=contenedor.findViewById(R.id.lblNombre);
            TextView lblDescripcion=contenedor.findViewById(R.id.lblDescripcion);
            lblNombre.setText(objItem.getNombre());
            lblDescripcion.setText(objItem.getDescripcion());
            return contenedor;
    }
}

【问题讨论】:

  • 您是否有要添加片段的活动?
  • 是的,在来自 Nav 活动的活动中,我创建了一个 id=fragment_container 的 FrameLayout 并在函数 NavigationItemSelect getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,new ComplaintsFragment( ))。犯罪();从导航视图中选择项目时运行。
  • 谁可以提供屏幕截图。你说吐司管用。那么这是否意味着列表视图确实出现但它是空白的?
  • 问题已解决,感谢您抽出时间@noyanc。
  • 另外,您正在使用自己的视图查看列表中的项目,因此不需要传入 android.R.layout.simple_list_item_1。您正在使用自己的一个 item_category。所以删除它并通过删除资源更正构造函数和超级。

标签: android listview android-fragments


【解决方案1】:

我替换了这个:

getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,new ComplaintsFragment()).commit();

用这个:

getSupportFragmentManager().beginTransaction().replace(android.R.content,new ComplaintsFragment()).commit();

一切顺利,问题解决

【讨论】:

  • 好的。您可以添加具有片段容器的 xml。因为它也应该与它一起工作。您是否为片段容器设置了正确的高度以使其可见。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-01
  • 1970-01-01
相关资源
最近更新 更多