2013-11-22 99 views
0

我這裏有一個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; 
} 

} 

但可繪製模式1和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; 
} 

} 
+0

你通過不會將你的drawables列表從'Drag_and_Drop_App'傳遞到你的適配器 – grexter89

+0

好吧,那麼我應該怎樣去增加drawables呢(或者哪裏可能是更好的問題)?在適配器中?或者我應該改變我的名單? – user2909006

+0

如果您不需要重新使用您的適配器,我建議您在適配器中初始化您的列表。 – grexter89

回答

0

在GridView的xml文件

</gridview> 

是ImageView的前關閉標籤,因此你的圖片不會在gridView中。

更一般地說,如果圖像不顯示,請確保您有分辨率爲您正在使用的設備的屏幕上的圖像。

+0

這ImageView的是別的東西...我想要添加到GridView的形象是我的編碼上述 – user2909006

+0

和圖形圖像應該是罰款決議的一部分,我所知道的 – user2909006

+0

的第二塊的模式1和模式2請考慮在你的問題中只提供最小的有用代碼。 –