Loading [MathJax]/jax/output/CommonHTML/config.js
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >如何在GridView中以片段形式显示GridView对象

如何在GridView中以片段形式显示GridView对象
EN

Stack Overflow用户
提问于 2015-03-10 23:26:45
回答 2查看 4.9K关注 0票数 1

我正在学习如何在一个片段中显示带有3列的网格视图。这些值来自作为数组列表的strings.xml文件。当我运行应用程序时,它会崩溃。谁能帮我显示3列的值,即地点,描述和时间。我无法继续在网格视图中显示读取的值。

代码语言:javascript
运行
AI代码解释
复制
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;
        }
    }
}

错误

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-03-10 23:51:51

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

代码语言:javascript
运行
AI代码解释
复制
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中

代码语言:javascript
运行
AI代码解释
复制
<?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>

编辑您需要创建包含3个文本视图的grid_row.xml。在getView上膨胀这个布局,然后返回它。您可以通过使用findViewById()来提高性能以避免使用ViewHolder模式

票数 0
EN

Stack Overflow用户

发布于 2015-03-10 23:49:22

问题

MyAdapter中的上下文为空

解决方案:-

向MyAdapter构造函数传递上下文,如下所示:

代码语言:javascript
运行
AI代码解释
复制
MyAdapter myAdapter = new MyAdapter(getActivity());

并在MyAdapter类中更改

代码语言:javascript
运行
AI代码解释
复制
MyAdapter(Context context) {
this.context=context;
......
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28980792

复制
相关文章
如何在GridView的Footer内显示总计?
前台: <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound" ShowFooter="true"> <Columns> <asp:TemplateField> <ItemTemplate> <asp:Label ID="labelfirst" runat="server" Text='<%# Eval("first") %>'>
菩提树下的杨过
2018/01/22
9390
gridview属性_GridView
GridView在生成HTML代码的时候会自动加上style=”border-collapse:collapse;”以及border=1,rules=”all”这些属性,这些在IE下都没什么影响,但是在FF下就会影响显示,style=”border-collapse:collapse;”;是由于设置了CellSpacing=”0″产生的,当设置CellSpacing=”1″后就没有,可以去掉style=”border-collapse:collapse;”;默认情况下CellSpacing=”0″,所以默认情况下会有style=”border-collapse:collapse;”这个属性生成。GridLines=”Both”会带来border=1,rules=all这两个属性,设置GridLines=”None”后border=0,rules属性则不会出现。
全栈程序员站长
2022/11/08
1.5K0
gridview布局_GridView
<link href=”StyleSheet.css” rel=”stylesheet” type=”text/css” />
全栈程序员站长
2022/11/09
8250
gridview布局_GridView
Android SimpleAdapter显示ListView、GridView
SimpleAdapter作为一个数据集,主要向ListView、GridView容器填充数据,总结了几种填充方式,原理很简单,直接看代码和效果图
阳光岛主
2019/02/19
6370
gridview分页显示_html分页显示数据
.gv { border: 1px solid #D7D7D7; font-size:12px; text-align:center; } .gvHeader { color: #3F6293; background-color: #F7F7F7; height: 24px; line-height: 24px; text-align: center; font-weight: normal; font-variant: normal; } .gvHeader th { font-weight: normal; font-variant: normal; } .gvRow, .gvAlternatingRow, .gvEditRow { line-height: 20px; text-align: center; padding: 2px; height: 20px; } .gvAlternatingRow { background-color: #F5FBFF; } .gvEditRow { background-color: #FAF9DD; } .gvEditRow input { background-color: #FFFFFF; width: 80px; } .gvEditRow .gvOrderId input, .gvEditRow .gvOrderId { width: 30px; } .gvEditRow .checkBox input, .gvEditRow .checkBox { width: auto; } .gvCommandField { text-align: center; width: 130px; }
全栈程序员站长
2022/09/29
5.8K0
gridview分页显示_html分页显示数据
Gridview导出到Excel,Gridview中的各类控件,Gridview中删除记录的处理
Asp.net 2.0中新增的gridview控件,是十分强大的数据展示控件,在前面的系列文章里,分别展示了其中很多的基本用法和技巧(详见< ASP.NET 2.0中Gridview控件高级技巧>)。在本文中,将继续探讨有关的技巧。
Java架构师必看
2021/03/22
2.7K0
Gridview][UpdateCommand的写法要点]
在ASP.NET2.0中的GridView为我们浏览更新数据提供了一个方便的途径。我们只需要添加一个
Java架构师必看
2020/10/26
1.1K0
GridView中DropDownList的事件
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) { DropDownList drp = sender as DropDownList; GridViewRow row = drp.NamingContainer as GridViewRow; // GridView 中对应的行
全栈程序员站长
2022/10/01
6030
GridView删除事件
RowDeleting和RowDeleted事件 RowDeleting发生在删除数据之前,RowDeleted发生在删除数据之后。 使用RowDeleting事件,可以在真正删除前再次确认是否删除,可以通过设置GridViewDeleteEventArgs.Cancel=True来取消删除;也可以用于判断当前数据库记录数,如果只剩一条记录且数据库不能为空则提示并取消删除操作。 使用RowDeleted事件,可以在删除后,通过GridViewDeletedEventArgs的Exception属性判断删除过程中是否产生异常,如无异常,则可以显示类似于” 1 Records deleted” 之类的提示信息。
全栈程序员站长
2022/09/07
1.2K0
GridView分页事件
 protected void GridView1_PageIndexChanging(object移动开发
Java架构师必看
2021/03/22
7320
Flutter GridView 使用
今天学习一下在Flutter中怎么使用GridView,效果如上图。 头部是一个Banner,使用的是第三方的
赵哥窟
2019/09/20
2.2K0
Flutter GridView 使用
可拖拽gridview
在Android开发中,我们常常用到ListView和GridView,而有的时候系统的ListView,GridView并不能满足我们的需求,所以我们需要自己定义一个ListView或者GridView,我的上一篇文章中就是自定义的一个左右滑动删除item的例子,大家有兴趣的可以去看看 Android 使用Scroller实现绚丽的ListView左右滑动删除Item效果,今天这篇文章就给大家来自定义GridView的控件,GridView主要是来显示网格的控件,在Android的开发中使用很普通,相对
xiangzhihong
2018/01/29
4.9K0
可拖拽gridview
GridView出现不完整–GridView删除滚动条
GridView显示不完毕的原因是由于,他的外层也套用了一个滑动的控件,解决办法就是重写GridView,是控制GridView不能滚动,就是写一个类继承GridView 代码例如以下
全栈程序员站长
2022/07/05
7130
gridview列 数字、货币和日期 显示格式
在设置gridview等数据绑定控件的模版列时,总要设置显示的格式,这里是我查询一些资料后统计出来的。
Java架构师必看
2021/03/22
1.3K0
获取GridView中的某列值
    protected void GridView1_RowEditing(object sen
Java架构师必看
2021/03/22
10.5K0
让GridView中CheckBox列支持FireFox
在Asp.net中,可以通过模板列,在Gridview中实现CheckBox列的实现,相关的代码并不复杂,你可以参考这里,我抽取的部分代码如下: <script language=”javascript” type=”text/javascript”> function selectAll(obj) { var theTable = obj.parentElement.parentElement.parentElement; var i; var j = obj.parentElement.
Jianbo
2018/01/15
1.1K0
gridview分页样式[通俗易懂]
在ASP.NET 2.0种提供了GridView控件。该控件的分页比较方便,可以通过在Visual Studio .NET 2005种简单设置即可实现各种分页功能。
全栈程序员站长
2022/09/09
1.3K0
GridView用法,分页
其中Bind是双向数据绑定的,不能单独使用,一般用于textbox等的Text属性,并且要用单引号,比如 标签内属性Text='<%# Bind(“Id”)%>’
全栈程序员站长
2022/09/08
1.3K0
点击加载更多

相似问题

以片段形式显示从url到gridview的图像

11

Gridview没有在片段中显示

16

在android片段中显示GridView

11

片段中的Gridview

10

在Gridview中显示Gridview

10
添加站长 进交流群

领取专属 10元无门槛券

AI混元助手 在线答疑

扫码加入开发者社群
关注 腾讯云开发者公众号

洞察 腾讯核心技术

剖析业界实践案例

扫码关注腾讯云开发者公众号
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档