列表未显示在片段

时间:2015-11-25 14:13:17

标签: android android-fragments android-listview

我试图在片段中显示列表视图。但是,没有显示任何内容,日志记录似乎正确。屏幕完全空白。我认为导致视图膨胀的原因是错误的,但我不知道。我已经研究过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>

1 个答案:

答案 0 :(得分:0)

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

 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>

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

哦,如果您有这个项目,请向我们展示