【问题标题】:List not displaying in Fragment列表未显示在片段中
【发布时间】:2015-11-25 14:13:17
【问题描述】:

我正在尝试在片段中显示列表视图。但是,没有显示任何内容,并且日志记录似乎正确。屏幕完全空白。我认为原因是放大视图时出错,但我不知道。我研究了 SO 和其他网站,但没有运气。

     @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        Log.v("FeaturesFragment", "FeaturesFragment");

        //View v = inflater.inflate(R.layout.carfeatures_list, container, false);
        //View v = inflater.inflate(R.layout.features_list_item, container, false);
        View v = inflater.inflate(R.layout.features_list_item, null);
        //View v = inflater.inflate(R.layout.carfeatures_list,null);
        ListView lv = (ListView) v.findViewById(android.R.id.list);
        Log.v("ListView lv1-features", lv.toString());

        adapter = new CustomListViewAdapter(carFeaturesList, R.layout.carfeatures_list, super.getActivity());
        //adapter = new CustomListViewAdapter(carFeaturesList, R.layout.features_list_item, super.getActivity());
        lv.setAdapter(adapter);
        Log.v("ListView lv2-features", lv.toString());

        adapter.notifyDataSetChanged();

        //Listview on item click listener
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                //Gets values from selected ListItem

                String car_features = ((TextView) view.findViewById(R.id.car_features)).getText().toString();
                //Log.v("onCreate-car_features", car_features.toString());

                String carfeatures_id = ((TextView) view.findViewById(R.id.carfeatures_id)).getText().toString();
                //Log.v("onCreate-carfeatures_id", carfeatures_id.toString());

                String model_id = ((TextView) view.findViewById(R.id.model_id)).getText().toString();
                //Log.v("onCreate-model_id", model_id.toString());

                String carfeatures_desc = ((TextView) view.findViewById(R.id.carfeatures_desc)).getText().toString();
                //Log.v("onCreate-carfeats_desc", carfeatures_desc.toString());

                Boolean b = Boolean.valueOf(TAG_CARID);
                if (b == true) {
                    view.setBackgroundColor(Color.GREEN);
                }

                //Colors the selected listview item
                if (previouslySelectedItem != null) {
                    previouslySelectedItem.setBackgroundColor(Color.TRANSPARENT);
                    //Log.v("previouslySelectedItem", "!=null");
                    //Log.v("position", Integer.toString(position));
                }

                view.setBackgroundColor(Color.GREEN);
                //Log.v("position", Integer.toString(position));
                //Log.v("outofif-view", view.toString());
                previouslySelectedItem = view;
            }
        });

        //Calls async task to get json
        new GetCarFeatures().execute();

        Log.v("return called-Features", "return called");
        Log.v("v", v.toString());
        return v;
    }

@Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            CarFeatures item = getItem(position);
            //Log.v("CarFeaturesitem", Integer.toString(position));

            if (convertView == null) {

                holder = new ViewHolder();

                LayoutInflater mInflater = LayoutInflater.from(getContext());
                convertView = mInflater.inflate(R.layout.carfeatures_list, null);
                //convertView = mInflater.inflate(R.layout.features_list_item, null);
                Log.v("getView", "getView");
                Log.v("View convertView", convertView.toString());
                Log.v("ViewGroup parent", parent.toString());

                holder.carfeatures_id = (TextView) convertView.findViewById(R.id.carfeatures_id);
                //Log.v("if-holder.carfeat_id", holder.carfeatures_id.toString());

                holder.model_id = (TextView) convertView.findViewById(R.id.model_id);
                //Log.v("if-holder.model_id", holder.model_id.toString());

                holder.carfeatures_desc = (TextView) convertView.findViewById(R.id.carfeatures_desc);
                //Log.v("if-holder.carfeat_id", holder.carfeatures_desc.toString());

                convertView.setTag(holder);
                //Log.v("if-convertView", convertView.toString());
                //Log.v("if-holder", holder.toString());

            } else {

                holder = (ViewHolder) convertView.getTag();
                //Log.v("else-convertView", convertView.toString());
                //Log.v("else-holder", holder.toString());
            }

            holder.carfeatures_id.setText(item.carfeatures);
            //Log.v("holder.carfeatures_id", holder.carfeatures_id.toString());

            holder.model_id.setText(item.modelid);
            //Log.v("holder.model_id", holder.model_id.toString());

            holder.carfeatures_desc.setText(item.carfeaturesdesc);
            //Log.v("holder.carfeatures_desc", holder.carfeatures_desc.toString());

            adapter.getItemId(position);
            //Log.v("getitemid1", Long.toString(adapter.getItemId(position)));

            Log.v("convertView", convertView.toString());
            return convertView;

        }
    }


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView
        android:id="@android:id/list"
        android:layout_height="match_parent"
        android:layout_width="fill_parent"
        android:choiceMode="singleChoice"
        android:layout_gravity="center_horizontal|top"
        android:layout_alignParentStart="true" />

</RelativeLayout>



<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/car_features"
        android:layout_below="@+id/model_id"
        android:layout_alignParentStart="true" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/carfeatures_id"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:layout_above="@+id/car_features" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/model_id"
        android:layout_alignParentStart="true" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/carfeatures_desc"
        android:layout_above="@+id/car_features"
        android:layout_alignParentStart="true" />

</RelativeLayout>

【问题讨论】:

  • 您的 XML 可能配置错误。比如你有android:layout_below="@+id/header,这里没有header ID。您也不需要对未添加的任何 id 进行加号。
  • 有关未来的问题:您能尝试仅显示相关代码吗?很难弄清楚问题到底是哪一行,但在许多情况下,您可以将其分解为几行代码。如果你只是复制这么多代码,那么只有少数人会完整地阅读你的问题,而你得到的答案就会更少。
  • 首先你的 ListView 有一个 match_parent 高度并且父布局(RelativeLayout)有一个 wrap_content 高度,如果是你的活动的根,你至少可以设置相对视图的高度来匹配父级/fragment 并且如果片段设置在具有匹配父级作为高度的视图上
  • 为了消除歧义,您应该更新您的问题并记下给定的 XML 布局,以便我们知道它是否代表在片段或适配器中膨胀的布局:)
  • @Cata 已更新。被膨胀的布局被记录下来。

标签: android android-fragments android-listview


【解决方案1】:

我可能是错的,但似乎在你的

 public View getView(int position, View convertView, ViewGroup parent)

你在试图夸大它

 <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/car_features"
            android:layout_below="@+id/model_id"
            android:layout_alignParentStart="true" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/carfeatures_id"
            android:layout_alignParentTop="true"
            android:layout_alignParentStart="true"
            android:layout_above="@+id/car_features" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/model_id"
            android:layout_below="@+id/header"
            android:layout_alignParentStart="true" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/carfeatures_desc"
            android:layout_above="@+id/car_features"
            android:layout_alignParentStart="true" />

        <ListView
            android:id="@android:id/list"
            android:layout_height="match_parent"
            android:layout_width="fill_parent"
            android:choiceMode="singleChoice"
            android:layout_gravity="center_horizontal|top"
            android:layout_alignParentStart="true" />


    </RelativeLayout>

这不起作用,因为您需要膨胀列表的 item(不是包含列表的布局),这是一个项目,您将使用数组中的数据填充它。我建议您为该项目创建一个布局,将其命名为 list_item.xml 并对其进行充气。

哦,如果你有这个项目,请给我们看看

【讨论】:

  • 你能详细说明你在解释什么吗?我很困惑。
  • RexSplode 是对的,您不应该在 ListView 适配器中膨胀该布局。相反,您应该为列表视图项目使用不同的布局..
  • 那么有一个只包含列表视图的布局文件吗?然后有另一个只包含文本视图的布局文件?我会在 getView 中膨胀 textview,然后在 oncreateview 中膨胀 listview 吗?
  • 是的,应该是:)
  • 现在您更改了代码,并且您的适配器方法出现在您的活动中。您需要拥有包含列表视图的布局。您在活动中膨胀的布局。 Aaand 你需要有一个适配器类,它在'getView()'中膨胀'list_item'。还要确保将“R.layout.list_item”传递给适配器的构造函数,不是 list.xml!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-07-19
  • 1970-01-01
  • 2021-10-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多