图像没有出现在gridView中

时间:2013-11-22 23:20:12

标签: android gridview android-gridview

我在这里有一个gridView:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rl_drag_and_drop_app"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<SlidingDrawer
    android:id="@+id/bottom"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerVertical="true"
    android:orientation="horizontal"
    android:layout_marginTop="200dp"
    android:content="@+id/content"
    android:handle="@+id/handle" >

    <Button
        android:id="@+id/handle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/arrow" />

    <LinearLayout
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="match_parent" 
        android:orientation="vertical"
        android:background="#00FF00">

            <!-- Editext for Search -->
<EditText android:id="@+id/inputSearch"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:hint="@string/Search_applications"
    android:inputType="textVisiblePassword"/>

<ListView
    android:id="@+id/lvApps"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"       
    />
    </LinearLayout>
    </SlidingDrawer>

        <Button
        android:id="@+id/btnLinkToPersonalize"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dip"
        android:background="@null"
        android:text="@string/Personalize"
        android:textColor="#21dbd4"
        android:textStyle="bold" />

<GridView 
android:id="@+id/GRIDVIEW1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dip"
android:numColumns="auto_fit"
android:columnWidth="60dp"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp"
android:gravity="center"
android:stretchMode="columnWidth"
 >  

</GridView>

 <ImageView
    android:id="@+id/trash_can"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:contentDescription="@string/trashcanDescription_Delete"
    android:padding="40dip"
    android:src="@drawable/trashcan"
    android:visibility="gone" >
</ImageView>

</RelativeLayout>

然后进行编码:

package com.example.awesomefilebuilderwidget;

IMPORTS

public class Drag_and_Drop_App extends Activity {

//For GridView
private int draggedIndex = -1;
private BaseAdapter adapterGV;
ArrayList<Integer> drawables = new ArrayList<Integer>();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // set layout for the main screen
    setContentView(R.layout.drag_and_drop_app);

    // GridView
    Log.d("GridView", "onCreate called");
    drawables.add(R.drawable.pattern1);
    drawables.add(R.drawable.pattern2);
    android.widget.GridView gridView = (android.widget.GridView) findViewById(R.id.GRIDVIEW1);

    // Instance of Adapter Class
    gridView.setAdapter(new GridViewAdapter(this));
   // gridView.setOnItemLongClickListener(this);

和这个适配器:

package com.example.awesomefilebuilderwidget;

IMPORTS

public class GridViewAdapter extends BaseAdapter {
private Context mContextGV;

// Keep all Images in array list
public ArrayList<Integer> drawables = new ArrayList<Integer>();

// Constructor
public GridViewAdapter(Context c){
    mContextGV = c;
}

@Override
// How many items are in the data set represented by this Adapter
public int getCount() {
    return drawables.size();
}

@Override
// Get the data item associated with the specified position in the
// data set
public Object getItem(int position) {
    return drawables.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // Try to reuse the views
    ImageView view = (ImageView) convertView;
    // if convert view is null then create a new instance else reuse it
    if (view == null) {
        view = new ImageView(mContextGV);
    }

    view.setImageResource(drawables.get(position));
    view.setScaleType(ImageView.ScaleType.CENTER_CROP);
    view.setLayoutParams(new android.widget.GridView.LayoutParams(70, 70));
    view.setTag(String.valueOf(position));
    return view;
}

}

但是drawables pattern1和pattern2从未出现在我的gridView中。

我确保正在调用gridView onCreate()方法(它是)。

我该怎么做才能解决这个问题?

新编码: Drag_and_Drop_App:

 @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // set layout for the main screen
    setContentView(R.layout.drag_and_drop_app);

    // GridView
    Log.d("D&D", "onCreate called");

    android.widget.GridView gridView = (android.widget.GridView) findViewById(R.id.GRIDVIEW1);

    // Instance of Adapter Class
    gridView.setAdapter(new GridViewAdapter(this));
   // gridView.setOnItemLongClickListener(this);

我的gridViewAdapter:

package com.example.awesomefilebuilderwidget;

IMPORTS
public class GridViewAdapter extends BaseAdapter {
private Context mContextGV;

// Keep all Images in array list
public ArrayList<Integer> drawables = new ArrayList<Integer>();

// Constructor
public GridViewAdapter(Context c){
    mContextGV = c;
    Log.d("GridViewAdapter", "Constructor is set");
}

@Override
// How many items are in the data set represented by this Adapter
public int getCount() {
    return drawables.size();
}

@Override
// Get the data item associated with the specified position in the
// data set
public Object getItem(int position) {
    return drawables.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // Try to reuse the views
    ImageView view = (ImageView) convertView;
    // if convert view is null then create a new instance else reuse it
    if (view == null) {
        view = new ImageView(mContextGV);
        Log.d("GridViewAdapter", "new imageView added");
    }
    drawables.add(R.drawable.pattern1);
    Log.d("GridViewAdapter", "pattern1 added");

    drawables.add(R.drawable.pattern2);
    Log.d("GridViewAdapter", "pattern2 added");

    drawables.add(R.drawable.trashcan);
    Log.d("GridViewAdapter", "trashcan added");

    drawables.add(R.drawable.ic_launcher);
    Log.d("GridViewAdapter", "ic_launcher added");

    view.setImageResource(drawables.get(position));
    view.setScaleType(ImageView.ScaleType.CENTER_CROP);
    view.setLayoutParams(new android.widget.GridView.LayoutParams(70, 70));
    view.setTag(String.valueOf(position));
    return view;
}

}

1 个答案:

答案 0 :(得分:0)

xml文件中gridview的结束标记

</gridview>

位于imageview之前,因此您的图像不会出现在gridView中。

更一般地说,如果图像没有显示,请确保您的图像具有所用设备屏幕的分辨率。