java – 在Fragment中使用Gridview的Android ImageAdapter

前端之家收集整理的这篇文章主要介绍了java – 在Fragment中使用Gridview的Android ImageAdapter前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个gridview适配器作为活动.我现在试图将它放在片段中并转换成东西,但它不起作用.当我在我的Activity中包含IconFragmentSystem时,当我尝试打开Activity时,我会收到一个强制关闭.

我知道活动有效,因为我可以使用其他碎片,一切都很好,所以我知道我的问题在于这个文件.

package com.designrifts.ultimatethemeui;
import com.designrifts.ultimatethemeui.R;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;

import java.util.ArrayList;

public class IconFragmentSystem extends Fragment implements AdapterView.OnItemClickListener{
private static final String RESULT_OK = null;
public Uri CONTENT_URI;

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

    View view = inflater.inflate(R.layout.main,container,false);

    super.onCreate(savedInstanceState);
    int iconSize=getResources().getDimensionPixelSize(android.R.dimen.app_icon_size);

    GridView gridview = (GridView) getActivity().findViewById(R.id.icon_grid);
    gridview.setAdapter(new IconAdapter(this,iconSize));
    gridview.setOnItemClickListener(this);
    CONTENT_URI=Uri.parse("content://"+iconsProvider.class.getCanonicalName());
    return view;
}

@Override
public void onItemClick(AdapterView<?> adapterView,View view,int i,long l) {
    String icon=adapterView.getItemAtPosition(i).toString();
    Intent result = new Intent(null,Uri.withAppendedPath(CONTENT_URI,icon));
    setResult(RESULT_OK,result);
    finish();
}
private void setResult(String resultOk,Intent result) {
    // TODO Auto-generated method stub

}

private void finish() {
    // TODO Auto-generated method stub

}
private class IconAdapter extends BaseAdapter{
    private Context mContext;
    private int mIconSize;
    public IconAdapter(Context mContext,int iconsize) {
        super();
        this.mContext = mContext;
        this.mIconSize = iconsize;
        loadIcon();
    }

    public IconAdapter(IconFragmentSystem iconssystem,int iconSize) {
        // TODO Auto-generated constructor stub
    }

    @Override
    public int getCount() {
        return mThumbs.size();
    }

    @Override
    public Object getItem(int position) {
        return mThumbs.get(position);
    }

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

    @Override
    public View getView(int position,View convertView,ViewGroup parent) {
        ImageView imageView;
        if (convertView == null) {
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(mIconSize,mIconSize));
        } else {
            imageView = (ImageView) convertView;
        }
        imageView.setImageResource(mThumbs.get(position));
        return imageView;
    }

    private ArrayList<Integer> mThumbs;
    ////////////////////////////////////////////////
    private void loadIcon() {
        mThumbs = new ArrayList<Integer>();

        final Resources resources = getResources();
        final String packageName = getActivity().getApplication().getPackageName();

        addIcon(resources,packageName,R.array.systemicons);

    }
    private void addIcon(Resources resources,String packageName,int list) {
        final String[] extras = resources.getStringArray(list);
        for (String extra : extras) {
            int res = resources.getIdentifier(extra,"drawable",packageName);
            if (res != 0) {
                final int thumbRes = resources.getIdentifier(extra,packageName);
                if (thumbRes != 0) {
                    mThumbs.add(thumbRes);
                }
            }
            }
           }
        }
    }

我已经尝试了不同的方法来实现这一点,但都失败了,我真的可以帮助我指向正确的方向.

这是我的布局中的xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >           
  <GridView
    android:id="@+id/gridview"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    android:columnWidth="90dp"
    android:numColumns="auto_fit"
    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp"
    android:stretchMode="columnWidth"
    android:gravity="center"
/>

    </RelativeLayout>

解决方法

我能够解决这个问题

我变了

GridView gridview = (GridView) getActivity().findViewById(R.id.icon_grid);

GridView gridview = (GridView) view.findViewById(R.id.icon_grid);

而且我也改变了

gridview.setAdapter(new IconAdapter(this,iconSize));

gridview.setAdapter(new IconAdapter(getActivity(),iconSize));
原文链接:https://www.f2er.com/java/121857.html

猜你在找的Java相关文章