0

我有一个 gridView 应该显示来自另一个活动的位图和某些图像(我在我的 drawables 文件夹中专门添加的那些)。但是,我的可绘制文件夹中的图像根本没有显示在 gridView 中。

这是xml文件:

<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/grid_view"
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 GridView extends Activity { // implements OnItemLongClickListener, OnDragListener{

ArrayList<Integer> drawables = new ArrayList<Integer>();

private BaseAdapter adapter;
private int draggedIndex = -1;
android.widget.GridView gridView = (android.widget.GridView) findViewById(R.id.GRIDVIEW1);

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.drag_and_drop_app);
    Log.d("GridView", "onCreate called");
    drawables = new ArrayList<Integer>();
    drawables.add(R.drawable.pattern1);
    drawables.add(R.drawable.pattern2);
    // gridView.setOnItemLongClickListener(this);
    gridView.setAdapter(adapter = new BaseAdapter() {

        @Override
        // Get a View that displays the data at the specified position in
        // the data set.
        public View getView(int position, View convertView,
                ViewGroup gridView) {
            // 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(GridView.this);
            }
            /*Bundle extras = getIntent().getExtras();
            byte[] byteArray = extras.getByteArray("picture");

            Bitmap default_b = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);

            view.setImageBitmap(default_b);*/
            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;
        }

        @Override
        // Get the row id associated with the specified position in the
        // list.
        public long getItemId(int position) {
            return position;
        }

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

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

/*@Override
public boolean onItemLongClick(AdapterView<?> gridView, View view,
        int position, long row) {
    ClipData.Item item = new ClipData.Item((String) view.getTag());
    ClipData clipData = new ClipData((CharSequence) view.getTag(),
            new String[] { ClipDescription.MIMETYPE_TEXT_PLAIN }, item);
    view.startDrag(clipData, new View.DragShadowBuilder(view), null, 0);
    View trashCan = findViewById(R.id.trash_can);
    trashCan.setVisibility(View.VISIBLE);
    trashCan.setOnDragListener(GridView.this);
    trashCan.setOnDragListener(GridView.this);
    draggedIndex = position;
    return true;
}

@Override
public boolean onDrag(View view, DragEvent dragEvent) {
    switch (dragEvent.getAction()) {
    case DragEvent.ACTION_DRAG_STARTED:
        // Drag has started
        // If called for trash resize the view and return true
        if (view.getId() == R.id.trash_can) {
            view.animate().scaleX(1.0f);
            view.animate().scaleY(1.0f);
            return true;
        } else // else check the mime type and set the view visibility
        if (dragEvent.getClipDescription().hasMimeType(
                ClipDescription.MIMETYPE_TEXT_PLAIN)) {
            view.setVisibility(View.GONE);
            return true;

        } else {
            return false;
        }
    case DragEvent.ACTION_DRAG_ENTERED:
        // Drag has entered view bounds
        // If called for trash can then scale it.
        if (view.getId() == R.id.trash_can) {
            view.animate().scaleX(1.5f);
            view.animate().scaleY(1.5f);
        }
        return true;
    case DragEvent.ACTION_DRAG_EXITED:
        // Drag exited view bounds
        // If called for trash can then reset it.
        if (view.getId() == R.id.trash_can) {
            view.animate().scaleX(1.0f);
            view.animate().scaleY(1.0f);
        }
        view.invalidate();
        return true;
    case DragEvent.ACTION_DRAG_LOCATION:
        // Ignore this event
        return true;
    case DragEvent.ACTION_DROP:
        // Dropped inside view bounds
        // If called for trash can then delete the item and reload the grid
        // view
        if (view.getId() == R.id.trash_can) {
            drawables.remove(draggedIndex);
            draggedIndex = -1;
        }
        adapter.notifyDataSetChanged();
    case DragEvent.ACTION_DRAG_ENDED:
        // Hide the trash can
        new Handler().postDelayed(new Runnable() {

            @Override
            public void run() {
                findViewById(R.id.trash_can).setVisibility(View.GONE);
            }
        }, 1000l);
        if (view.getId() == R.id.trash_can) {
            view.animate().scaleX(1.0f);
            view.animate().scaleY(1.0f);
        } else {
            view.setVisibility(View.VISIBLE);
        }
        // remove drag listeners
        view.setOnDragListener(null);
        return true;

    }
    return false;
}*/

}

请注意,由于某种原因,在 LogCat 中永远不会打印出 gridView onCreate

添加清单:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.awesomefilebuilderwidget"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >

<receiver android:name="com.example.awesomefilebuilderwidget.AFBWidget" android:label="@string/app_name">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
</intent-filter>

<meta-data android:name="android.appwidget.provider"
android:resource="@xml/widget_stuff"/>

</receiver>

<activity android:name="com.example.awesomefilebuilderwidget.WidgetConfig" android:label="@string/app_name">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_CONFIGURE"/>

</intent-filter> 

</activity>   

<activity android:name="com.example.awesomefilebuilderwidget.Drag_and_Drop_App" android:label="@string/app_name" android:windowSoftInputMode="stateHidden" android:screenOrientation="portrait"></activity>
<activity android:name="com.example.awesomefilebuilderwidget.AppInfoAdapter" android:label="@string/app_name" ></activity>
<activity android:name="com.example.awesomefilebuilderwidget.Feedback" android:label="@string/app_name" ></activity>
<activity android:name="com.example.awesomefilebuilderwidget.SendMessageActivity" android:label="@string/app_name" ></activity>
<activity android:name="com.example.awesomefilebuilderwidget.Utilities" android:label="@string/app_name" ></activity>
<activity android:name="com.example.awesomefilebuilderwidget.Personalize" android:label="@string/app_name" ></activity>
<activity android:name="com.example.awesomefilebuilderwidget.GridView" android:label="@string/app_name"></activity>
<activity android:name="com.example.awesomefilebuilderwidget.SwipeDetector"  android:label="@string/app_name"></activity>

</application>

</manifest> 

我已经尝试更改我的 gridView 的宽度和高度以确保它正在被绘制并且在试图解决这个问题的网站上不断地环顾四周,但我不知道该怎么做。困扰我的是 onCreate() 方法永远不会被调用。

为什么是这样????请帮忙!

4

0 回答 0