如何在Android中的GridView中以片段形式显示ArrayList对象

时间:2015-03-11 07:26:45

标签: java android gridview android-fragments

我正在尝试学习如何在片段中显示3列的网格视图。值来自strings.xml文件作为数组列表。当我运行应用程序时,它崩溃了。任何人都可以帮我显示3列中的值,即位置,描述和时间。我不能继续如何在gridview中显示读取值。

My Code are as follows:
**Strings.XML**
    <string-array name="PlacesName">
        <item>Durban</item>
        <item>Paris</item>
        <item>HongKong</item>
        <item>Canada</item>
    </string-array>

    <string-array name="Description">
        <item>Description1</item>
        <item>Description2</item>
        <item>Description3</item>
    </string-array>

    <string-array name="ScheduleDate">
        <item>12/1/2015</item>
        <item>13/2/2015</item>
        <item>14/4/2015</item>
        <item>15/5/2015</item>
    </string-array>

**Fragment**

    */
public class FlightFragment extends Fragment {

    GridView grid;

    public FlightFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_flight, container, false);
        // Inflate the layout for this fragment
        grid = (GridView) view.findViewById(R.id.gridView);
        MyAdapter myAdapter = new MyAdapter();
        grid.setAdapter(myAdapter);
        return view;

    }

    class MyAdapter extends BaseAdapter {

        ArrayList<Schedule> list;
        private Context context;

        MyAdapter() {
            list = new ArrayList();
            Resources resources = context.getResources();
            String[] tempPlaces = resources.getStringArray(R.array.PlacesName);
            String[] tempDate = resources.getStringArray(R.array.ScheduleDate);
            String[] tempDescription = resources.getStringArray(R.array.Description);
            for(int count=0;count<4;count++ ){
                Schedule tempSchedule = new Schedule(tempPlaces[count],tempDate[count],tempDescription[count]);
                list.add(tempSchedule);
            }
        }
        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

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

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            Schedule tempSchedule = list.get(position);
            TextView textView;
            if (convertView == null){
                textView = new TextView(context);
                textView.setLayoutParams(new GridView.LayoutParams(85, 85));
                textView.setPadding(8, 8, 8, 8);
            }
            else{
                textView = (TextView) convertView;
            }
            textView.setText(tempSchedule.Place);
            return textView;
        }
    }

    class Schedule {
        String Place;
        String ScheduleTime;
        String Description;

        Schedule(String place, String scheduleTime, String description ) {
            this.Description = description;
            this.Place = place;
            this.ScheduleTime = scheduleTime;
        }
    }
}

错误

  

致命的例外:主要       显示java.lang.NullPointerException               在com.ts.FlightFragment $ MyAdapter。(FlightFragment.java:51)               at com.ts.FlightFragment.onCreateView(FlightFragment.java:38)               在android.support.v4.app.Fragment.performCreateView(Fragment.java:1786)               在android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)               在android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)               在android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)               在android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1489)               在android.support.v4.app.FragmentManagerImpl $ 1.run(FragmentManager.java:454)               在android.os.Handler.handleCallback(Handler.java:725)               在android.os.Handler.dispatchMessage(Handler.java:92)               在android.os.Looper.loop(Looper.java:176)               在android.app.ActivityThread.main(ActivityThread.java:5279)               at java.lang.reflect.Method.invokeNative(Native Method)               在java.lang.reflect.Method.invoke(Method.java:511)               在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1102)               在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)               在dalvik.system.NativeStart.main(本地方法)

2 个答案:

答案 0 :(得分:0)

问题

  MyAdapter中的

上下文为空

解决方案: -

将上下文传递给MyAdapter构造函数,如下所示: -

MyAdapter myAdapter = new MyAdapter(getActivity());

并在MyAdapter类中更改此内容

MyAdapter(Context context) {
this.context=context;
......
}

答案 1 :(得分:0)

问题是您忘记在适配器中分配上下文。你可以通过构造函数传递它。在活动中,您只需使用this作为上下文。在您的情况下,在片段中,需要将getActivity()作为上下文传递。

public class FlightFragment extends Fragment {

GridView grid;

public FlightFragment() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_flight, container, false);
    // Inflate the layout for this fragment
    grid = (GridView) view.findViewById(R.id.gridView);
    MyAdapter myAdapter = new MyAdapter(getActivity());
    grid.setAdapter(myAdapter);
    return view;

}

class MyAdapter extends BaseAdapter {

    ArrayList<Schedule> list;
    private Context context;

    MyAdapter(Context context) {
        this.context = context;
        list = new ArrayList();
        Resources resources = context.getResources();
        String[] tempPlaces = resources.getStringArray(R.array.PlacesName);
        String[] tempDate = resources.getStringArray(R.array.ScheduleDate);
        String[] tempDescription = resources.getStringArray(R.array.Description);
        for(int count=0;count<4;count++ ){
            Schedule tempSchedule = new Schedule(tempPlaces[count],tempDate[count],tempDescription[count]);
            list.add(tempSchedule);
        }
    }
    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Schedule tempSchedule = list.get(position);
        if (convertView == null){
            convertView = LayoutInflater.from(context).inflate(R.layout.grid_row, parent, false);
        }
        TextView placeName = (TextView) convertView.findViewById(R.id.place_name);
        TextView scheduleDate = (TextView) convertView.findViewById(R.id.schedule_date);
        TextView description = (TextView) convertView.findViewById(R.id.description);
        placeName.setText(tempSchedule.Place);
        scheduleDate.setText(tempSchedule.ScheduleTime);
        description.setText(tempSchedule.Description);
        return convertView;
    }
}

在grid_row.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical">
    <TextView
        android:id="@+id/place_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <TextView
        android:id="@+id/schedule_date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    <TextView
        android:id="@+id/description"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</LinearLayout>

修改 您需要创建grid_row.xml,其中包含3个textviews。在getView上对该布局进行通知,然后将其返回。 您可以使用ViewHolder Pattern来提高效果以避免findViewById()