Java 在 Fragment 中使用 Gridview 的 Android ImageAdapter
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19214620/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android ImageAdapter with Gridview in Fragment
提问by Jai Romeo
I have an adapter with gridview that works as an Activity. I am trying to place it in a Fragment now and converted things but it does not work. When I include the IconFragmentSystem in my Activity I get a force close when I try to open the Activity.
我有一个带有 gridview 的适配器,可用作活动。我现在试图将它放在 Fragment 中并进行转换,但它不起作用。当我在我的活动中包含 IconFragmentSystem 时,当我尝试打开活动时,我会强制关闭。
I know the Activity works because I can use other Fragments and everything is okay so I know my issue lies within this file.
我知道 Activity 有效,因为我可以使用其他 Fragment 并且一切正常,所以我知道我的问题出在这个文件中。
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,"drawable", packageName);
if (thumbRes != 0) {
mThumbs.add(thumbRes);
}
}
}
}
}
}
I have tried different ways to implement this but all have failed and I could really use help pointing me in the right direction.
我尝试了不同的方法来实现这一点,但都失败了,我真的可以使用帮助来指明正确的方向。
This is my xml in layout
这是我在布局中的 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>
采纳答案by Jai Romeo
I was able to fix the issue
我能够解决这个问题
I changed
我变了
GridView gridview = (GridView) getActivity().findViewById(R.id.icon_grid);
to
到
GridView gridview = (GridView) view.findViewById(R.id.icon_grid);
and I also changed
我也改变了
gridview.setAdapter(new IconAdapter(this, iconSize));
to
到
gridview.setAdapter(new IconAdapter(getActivity(), iconSize));
回答by r2DoesInc
GridView gridview = (GridView) getActivity().findViewById(R.id.icon_grid);
GridView gridview = (GridView) getActivity().findViewById(R.id.icon_grid);
should be
应该
GridView gridview = (GridView) container.findViewById(R.id.icon_grid);
GridView gridview = (GridView) container.findViewById(R.id.icon_grid);
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.main, container, false);
int iconSize = getResources().getDimensionPixelSize(android.R.dimen.app_icon_size);
GridView gridview = (GridView) view.findViewById(R.id.icon_grid);
gridview.setAdapter(new IconAdapter(this, iconSize));
gridview.setOnItemClickListener(this);
CONTENT_URI = Uri.parse("content://"+iconsProvider.class.getCanonicalName());
return view;
}
Your going to want to remove the call to super, that is a leftover from you activity, the fragment does not call this method.
您将要删除对 super 的调用,这是您活动的剩余部分,片段不会调用此方法。
回答by Kishor Ramani
Try this.
尝试这个。
gridView = (GridView) rootView.findViewById(R.id.gridView);
gridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view,int pos, long l) {
// do something...
}
});
For more info, see this link: Android gridview in fragment adapter constructor missing
有关更多信息,请参阅此链接: 片段适配器构造函数中的 Android gridview 丢失