gpt4 book ai didi

android - 用于GridView的Android文本和图像适配器的 fragment

转载 作者:行者123 更新时间:2023-12-02 13:10:20 26 4
gpt4 key购买 nike

我在fragment中遇到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.legememinder,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)
在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)

最佳答案

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

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

关于android - 用于GridView的Android文本和图像适配器的 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35974229/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com