当前位置: 首页 > 知识库问答 >
问题:

用于碎片gridview的Android文本和图像适配器

乜安志
2023-03-14

我在Frgament的gridview有问题....拜托,我需要你的帮助!!!!

FRGAMENT中的代码:

public class GridAdapterFragment extends Fragment{
String[]title;
Integer[]ids;
Database database;
String[] Description;
String[] Title;
int[] Minute;
int[] Hour;
int[] Day;
int[] Month;
int[] Year;
int mId;
int[]Ids;
@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }


public View onCreateView (LayoutInflater inflater, ViewGroup fragment1ODC, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_1_od, fragment1ODC, false);
    database = new Database(view.getContext());
    int mId = database.getLastIdOD();
    title =new String[mId];
    Ids = new int[mId];
    ids = new Integer[mId];
    Title = new String[mId];
    Description = new String[mId];
    Minute = new int[mId];
    Hour = new int[mId];
    Day = new int[mId];
    Month = new int[mId];
    Year = new int[mId];
    database.close();
    for(int i=1;i==mId;i++){
        database = new Database(view.getContext());
        OneDayTD notification = database.getOneDay(i);
        database.close();
        if(notification.getActive()==1) {
            title[i-1] = notification.getTitle();
            Ids[i-1] = notification.getId();
            int idp = Integer.valueOf("R.drawable." +Integer.valueOf(notification.getIcon()));
            ids[i-1] = idp;
            Title[i-1] = notification.getTitle();
            Description[i-1] = notification.getContent();
            Minute[i-1]= Integer.valueOf(getMinuteInt(notification.getDateAndTime()));
            Hour[i-1]= Integer.valueOf(getHourInt(notification.getDateAndTime()));
            Day[i-1]= Integer.valueOf(getDayInt(notification.getDateAndTime()));
            Month[i-1]= Integer.valueOf(getMonthInt(notification.getDateAndTime()));
            Year[i-1]= Integer.valueOf(getYearInt(notification.getDateAndTime()));
        }
    }
    TextView textView= (TextView) view.findViewById(R.id.textView4);
    GridView gridView = (GridView) view.findViewById(R.id.gridViewActiveOd);
    if(0<mId){
    gridView.setAdapter(new GridOdAdapter(view.getContext(),1,ids,Description,Title,Minute,Hour,Day,Month,Year));
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                getActivity().finish();
                Intent intent = new Intent(getActivity().getApplicationContext(),CreateOneDayActivity.class);
                intent.putExtra("mId",Ids[position]);
                startActivity(intent);
            }
    });
    }else{
        textView.setText("You Haven't got any tasks !");
    }
    // uses the view to get the context instead of getActivity().
    return view;
}



    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

public String getMinuteInt(String i){
    Integer iN = Integer.valueOf(i);
    int d = iN % 100;
    String dS = Integer.toString(d);
    return  dS;
}

public String getHourInt(String i){
    Integer iN = Integer.valueOf(i);
    int d = iN % 10000;
    d = d/100;
    String dS = Integer.toString(d);
    return  dS;
}

public String getDayInt(String i){
    Integer iN = Integer.valueOf(i);
    int d = iN % 1000000;
    d = d/10000;
    String dS = Integer.toString(d);
    return  dS;
}
public String getMonthInt(String i){
    Integer iN = Integer.valueOf(i);
    int d = iN % 100000000;
    d = d/1000000;
    String dS = Integer.toString(d);
    return  dS;
}

public String getYearInt(String i){
    Integer iN = Integer.valueOf(i);
    int d = iN % 100000000;
    String dS = Integer.toString(d);
    return  dS;
}
}

适配器中的代码:

public class GridOdAdapter  extends BaseAdapter {
 String[] Description;
 String[] Title;
int[] Minute;
 int[] Hour;
int[] Day;
int[] Month;
int[] Year;
public Integer[] mThumbIds;

private Context mContext;
 int ACTIVE;

public GridOdAdapter(Context c,int active,Integer[] ids,String[] description, String[] title,int[] minute,int[] hour, int[] day,int[] month,int[] year) {
    mContext = c;
    ACTIVE = active;
    mThumbIds = ids;
    Description = description;
    Title = title;
    Minute = minute;
    Hour = hour;
    Day = day;
    Month = month;
    Year = year;
}

public int getCount() {
    return mThumbIds.length;
}

public Object getItem(int position) {
    return mThumbIds[position-1];
}

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


@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub

    View grid;
    LayoutInflater inflater = (LayoutInflater) mContext.getSystemService( Context.LAYOUT_INFLATER_SERVICE );

    if (convertView == null) {
        grid = new View(mContext);
        //LayoutInflater inflater = getLayoutInflater();

        grid = inflater.inflate(R.layout.grid_od,null);
        ImageView imageView = (ImageView) grid.findViewById(R.id.imagepart);
        TextView textView = (TextView) grid.findViewById(R.id.textpart);

        TextView textView2 = (TextView) grid.findViewById(R.id.texttime);

        TextView textView4 = (TextView) grid.findViewById(R.id.Timer);
        TextView textView5 = (TextView) grid.findViewById(R.id.textpart2);
        imageView.setImageResource(mThumbIds[position-1]);
        textView.setText(Title[position-1]);
        textView5.setText(Description[position-1]);
        textView2.setText("Year: "+Year[position-1] + " Month: " + Month[position-1] + " Day: " + Day[position-1] + " Hour: "+ Hour[position-1] + " Minute: " + Minute[position-1]);
        textView4.setText(Title[position]);

    } else {
        grid = (View) convertView;
    }




    return grid;
}

// references to our images

}

和日志:

致命异常:主进程:com.universe.time.officeReminder,pid:18976 java.lang.ArrayIndexOutOfBoundsException:length=2;在com.universe.time.proffesionaldiary.adapters.gridodadapter.getView(gridodadapter.java:81)在Android.widget.abslistview.obtainView(abslistview.java:2828)在Android.widget.gridview.obtainView(abslistview.java:1064)在Android.view.view.measure(view.java:18647)在Android.view.view.measure(view.java:18647)在.ViewGroup.MeasureChildWithMargins(ViewGroup.java:5832)位于Android.Support.Design.Widget.CoordinatorLayout.OnMeasureChild(CoordinatorLayout.java:664)位于Android.Support.Design.Widget.HeaderScrollingViewBehavior.OnMeasureChild(HeaderScrollingViewBehavior.90)位于在Android.Widget.LinearLayout.MeasureVertical(LinearLayout.java:1435)在Android.Widget.LinearLayout.java:721)在Android.Widget.LinearLayout.OnMeasure(LinearLayout.java:612)在Android.View.View.Measure(LinearLayout.java:18647)在Android.View.View.Measure(View.java:18647)在Android.View.ViewGroup.MeasureChildWithMargins(t.FrameLayout.onMeasure(FrameLayout.java:430)在com.android.internal.policy.impl.PhoneWindow$decorview.onMeasure(PhoneWindow.java:3012)在android.view.view.measure(view.java:18647)在android.view.view.measure(view.view.viewRootimpl.PerformMeasure(view.view.viewRootimpl.2298)在android.view.viewRootimpl.MeasureHierarchy

共有1个答案

公孙栋
2023-03-14

日志语句不是很明显吗?在GridOdAdapter的第45行,它尝试获取空对象的length属性。您没有向适配器传递任何数据。因此表为空。您应该将适配器构造函数更改为如下所示。

public GridOdAdapter(Context c, int active,Integer[] ids){
    context=c;
    ACTIVE=active;
    mThumbIds = ids;
}
 类似资料:
  • 问题内容: 我目前正在做一个小项目,该项目将从URL加载图像,调整大小,并使用指定的调色板将其颜色深度更改为仅16种颜色。我的主要问题是,我需要一个程序,可以在Android设备和台式计算机上使用。您知道在两个系统上都可以使用的良好图像处理库吗? 提前致谢。 问题答案: 有几种工具: 图片J,http: //rsbweb.nih.gov/ij/ 斐济,http://fiji.sc/wiki/ind

  • 问题内容: 嗨,您想创建一个自定义光标适配器,以便显示2行文本的图像。我在理解自定义光标适配器时遇到了一些麻烦,但是我不明白如何添加要从数据库路径填充的imageview。 问题答案: 将, 实际上,我已经实现了与您正在寻找的东西非常相似的东西。这是我的实现。 XML只是万一… 根据特定条件,这将在文本的两行显示最多2幅图像。 希望这可以为您的工作打下基础! 祝好运 :]

  • 我正在使用assets文件夹中的hdpi、mdpi、xhdpi,xxhdpi和xxxhdpi来响应本地android。 但是有些图像工作正常,有时即使在提供固定的高度和宽度后,相同的图像也会调整大小(大小增加和减小)。 对于ios来说一切都很好,但是在Android的情况下,图像会失真,有时无法显示。 我已尽一切努力解决此问题,但尚无法解决。

  • 我可以使用一个片段作为一个活动吗?我已经创建了一个片段,但我希望它有像活动一样的功能,所以我使用片段扩展碎片活动。然而,我有一个带有碎片的导航抽屉。当我更改为“扩展碎片活动”时,我的代码有问题?请给我指路。

  • 我必须文件上传操作连续,第一个图像像gif|jpg|jpeg|png|svg和第二个psd|rar|zip|doc|word|txt|xlsx|pdf第一个工作正常,我可以上传所有图像,但第二个,我不能上传任何这些类型但当我尝试上传图像在第二段它的作品。

  • 我正在试用Bento4和Shaka Packager来使用碎片MP4为DASH和HLS输出文件。 我有一些困难理解的差异和利弊之间的MPEG-DASH直播和点播配置文件。如果我是流式直播内容,我会使用live profile,但对于静态点播视频,似乎我可以使用点播或live profile。每个配置文件以完全不同的文件格式和文件夹结构输出文件,随需输出包含.mp4文件的平面文件夹结构,实时输出包含