片段中gridview的Android文本和图像适配器

Posted

技术标签:

【中文标题】片段中gridview的Android文本和图像适配器【英文标题】:Android text and image adapter for gridview in fragment 【发布时间】:2016-06-28 16:56:16 【问题描述】:

我对 frgament 中的 gridview 有问题.... 拜托,我需要你的帮助!!!!

片段中的代码:

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:长度=2;指数=-1 在 com.universe.time.profesionaldiary.adapters.GridOdAdapter.getView(GridOdAdapter.java:81) 在 android.widget.AbsListView.obtainView(AbsListView.java:2828) 在 android.widget.GridView.onMeasure(GridView.java:1064) 在 android.view.View.measure(View.java:18647) 在 android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5832) 在 android.widget.FrameLayout.onMeasure(FrameLayout.java:430) 在 android.view.View.measure(View.java:18647) 在 android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5832) 在 android.widget.FrameLayout.onMeasure(FrameLayout.java:430) 在 android.view.View.measure(View.java:18647) 在 android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5832) 在 android.widget.FrameLayout.onMeasure(FrameLayout.java:430) 在 android.view.View.measure(View.java:18647) 在 android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5832) 在 android.widget.FrameLayout.onMeasure(FrameLayout.java:430) 在 android.view.View.measure(View.java:18647) 在 android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5832) 在 android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:664) 在 android.support.design.widget.HeaderScrollingViewBehavior.onMeasureChild(HeaderScrollingViewBehavior.java:90) 在 android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onMeasureChild(AppBarLayout.java:1139) 在 android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:729) 在 android.view.View.measure(View.java:18647) 在 android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:1071) 在 android.view.View.measure(View.java:18647) 在 android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5832) 在 android.widget.FrameLayout.onMeasure(FrameLayout.java:430) 在 android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135) 在 android.view.View.measure(View.java:18647) 在 android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5832) 在 android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1435) 在 android.widget.LinearLayout.measureVertical(LinearLayout.java:721) 在 android.widget.LinearLayout.onMeasure(LinearLayout.java:612) 在 android.view.View.measure(View.java:18647) 在 android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5832) 在 android.widget.FrameLayout.onMeasure(FrameLayout.java:430) 在 android.view.View.measure(View.java:18647) 在 android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5832) 在 android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1435) 在 android.widget.LinearLayout.measureVertical(LinearLayout.java:721) 在 android.widget.LinearLayout.onMeasure(LinearLayout.java:612) 在 android.view.View.measure(View.java:18647) 在 android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5832) 在 android.widget.FrameLayout.onMeasure(FrameLayout.java:430) 在 android.view.View.measure(View.java:18647) 在 android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5832) 在 android.widget.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.ViewRootImpl.performMeasure(ViewRootImpl.java:2298) 在 android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1345) 在 android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1588) 在 android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1230) 在 android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6748) 在 android.view.Choreographer$CallbackRecord.run(Choreographer.java:777) 在 android.view.Choreographer.doCallbacks(Choreographer.java:590) 在 android.view.Choreographer.doFrame(Choreographer.java:560) 在 android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:763) 在 android.os.Handler.handleCallback(Handler.java:739)

【问题讨论】:

【参考方案1】:

日志语句不是很明显吗?在 GridOdAdapter 的第 45 行,它尝试获取空对象的长度属性。您没有将任何数据传递给适配器。因此该表为空。你应该把你的适配器构造函数改成这样。

public GridOdAdapter(Context c, int active,Integer[] ids)
    context=c;
    ACTIVE=active;
    mThumbIds = ids;

【讨论】:

嘿家伙,非常感谢您的帮助,但现在我有另一个问题,请再次帮助我,我已经更改了片段和适配器的代码,也更改了日志 当一个答案解决了您的问题时,您应该将其标记为已接受。至于你的其他问题,这又很明显了。它说 ArrayIndexOutOfBoundsException。您的错误是您请求 [position - 1]。当位置为0时等于-1。没有索引 -1.==> 错误。除此之外......堆栈溢出不适合其他人为您编写代码。您请求我们帮助的错误是非常基本的。在此处发布之前,请尽量自行弄清楚您的代码有什么问题。否则你永远不会变得更好。

以上是关于片段中gridview的Android文本和图像适配器的主要内容,如果未能解决你的问题,请参考以下文章

Android drawSelectorOnTop 与 GridView

如何从片段中检索gridview中的图像?

Android:在网格中显示文本的简单 GridView

如何在选项卡式布局中显示 Android gridview

在片段中重新加载android视图

Android AsyncTask 与特定情况下的线程