GridView图片不显示在片段中,仅在主要活动上显示

时间:2015-08-18 20:31:36

标签: android android-fragments gridview imageview

我正在尝试创建一个将url加载到其中的gridview。我可以在MainActivity中使用它,但是当我将代码粘贴到Gridview时。图像没有显示。

这是我在Fragment上的代码

$q2= "SELECT COUNT( DISTINCT(`slotid`) ) as cnt FROM `individualavailability`";
$r2 = mysqli_query ($dbcon, $q2);
$row = mysqli_fetch_assoc($r2);
echo $row["cnt"];

activity_main.xml中

public static String[] eatFoodyImages = {
        "http://image.tmdb.org/t/p/w185//nBNZadXqJSdt05SHLqgT0HuC5Gm.jpg",
        "http://i.imgur.com/C9pBVt7.jpg",
};

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

    View rootView = inflater.inflate(R.layout.fragment_main, container, false);
    // Inflate the layout for this fragment

    GridView gridview = (GridView) rootView.findViewById(R.id.gridview);
    gridview.setAdapter(new ImageListAdapter(getActivity(), eatFoodyImages));

    return inflater.inflate(R.layout.fragment_main, container, false);
}

Fragment_main_xml

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/fragment"
android:name="com.example.android.movie.MainFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout="@layout/fragment_main" />

Image_view.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.android.movie.MainFragment">

<GridView
    android:id="@+id/gridview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:adjustViewBounds="true"
    android:horizontalSpacing="0dp"
    android:numColumns="2"
    android:stretchMode="columnWidth"
    android:verticalSpacing="0dp" />

ImageListAdapter:

<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list_item_movie"
android:layout_width="match_parent"
android:layout_height="200dp">

1 个答案:

答案 0 :(得分:1)

<a href="WebAdress.netdom" target="_blank"><button>Open New Tab</button></a> 方法存在错误。

看起来应该是这样的:

onCreateView