片段中用于gridview的Android文本和图像适配器
在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; 指数= -1在com.universe.time.proffesionaldiary.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)at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5832)at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)at 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)at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:664)at android.support.design.widget.HeaderScrollingViewBehavior.onMeasureChild(HeaderScrollingViewBehavior.java:90)at android.view.design.widget.AppBarLayout $ ScrollingViewBehavior.onMeasureChild(AppBarLayout.java:1139)at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:729)at android.view.View.measure(View。在Android.view.View.measureChildChildWithMargins(ViewGroup.java)上的android.view.View.measure(View.java:18647)处的android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:1071) :5832)在android.widget.FrameLayout.onMeasure(FrameLayout.java:430)在android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)在android.view.View.measure(View.java: 18647)at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5832)at android.widget.LinearLayout.measu android.widget.LinearLayout.measureVertical(LinearLayout.java:721)在android.widget.LinearLayout.onMeasure(LinearLayout.java:612)android.view.View.measure(View.java)上的reChildBeforeLayout(LinearLayout.java:1435) :18647)at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5832)at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)at android.view.View.measure(View.java:18647)at android .view.ViewGroup.measureChildWithMargins(ViewGroup.java:5832)at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1435)at android.widget.LinearLayout.measureVertical(LinearLayout.java:721)at 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)at android.view.View.measure(View.java:18647)at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5832)at android.widge t.FrameLayout.onMeasure(FrameLayout.java:430)at com.android.internal.policy.impl.PhoneWindow $ DecorView.onMeasure(PhoneWindow.java:3012)at android.view.View.measure(View.java:18647)在android.view.android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1588)处的android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1345)处的android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2298)。在android.view.ViewRootImpl上的ViewRootImpl.doTraversal(ViewRootImpl.java:1230)android.view.Choreographer $ CallbackRecord.run(Choreographer.java:777)android.view.Choreographer上的$ TraversalRunnable.run(ViewRootImpl.java:6748) .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)
日志语句不明显吗? 在你的GridOdAdapter的第45行,它试图获得空对象的length属性。 您没有将任何数据传递给适配器。 因此该表为空。 你应该改变你的适配器构造函数为这样的东西。
public GridOdAdapter(Context c, int active,Integer[] ids){
context=c;
ACTIVE=active;
mThumbIds = ids;
}
链接地址: http://www.djcxy.com/p/84725.html
上一篇: Android text and image adapter for gridview in fragment