凌云志轩 2015-05-07 03:19 采纳率: 89%
浏览 2071
已采纳

android 操作girdview数据

我的main.xml里面有一个添加按钮,点击该按钮后,我希望在弹出的页面录入信息并将信息传送到main.xml,gridview添加一条tiem。如何实现呢。
main.xml
android:id="@+id/add"/>
android:id="@+id/grid"/>

add_dev.xml
<spinner
android:id = "@+id/selected"
<Button
android:id="@+id/add_ok"
item.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:gravity="center_horizontal"
        android:background="@drawable/yu">

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="姓名"
                android:id="@+id/name"
                android:layout_weight="0.8"
                android:layout_gravity="center_vertical"
                android:textSize="16dp"
                android:layout_marginLeft="15dp"
                android:layout_marginTop="5dp"
                android:layout_marginBottom="5dp"
                android:textColor="@color/black" /> 
        </LinearLayout>

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:background="@drawable/yu2">

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/yu3"
                android:layout_weight="0.55"
                android:layout_gravity="center_vertical">
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_weight="0.6"
                    android:background="@null"
                    android:src="@drawable/tock"
                    android:layout_gravity="center_vertical"
                    android:layout_marginTop="5dp"
                    android:layout_marginBottom="5dp" />

                <ImageButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/tock_on"
                    android:layout_weight="1"
                    android:background="@drawable/tock_on"
                    android:layout_gravity="center_vertical" />

                <ImageButton

                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/tock_off"
                    android:layout_weight="1"
                    android:background="@drawable/tock_off"
                    android:layout_gravity="center_vertical" />

            </LinearLayout>
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center_vertical"
                android:layout_gravity="center_vertical">
                <ImageButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/voice"
                    android:layout_weight="1"
                    android:background="@drawable/vocie"
                    android:layout_gravity="center_vertical" />
            </LinearLayout>
        </LinearLayout>


    </LinearLayout>
</LinearLayout>

myadapter.java

 public class MyAdapter extends BaseAdapter {
    private LayoutInflater inflater;
    private final String layoutInflater = Context.LAYOUT_INFLATER_SERVICE;
    private Context mcontext;
    private static boolean flag =false;
    private List<dev_item> appList;
    private GridView myGridView;

    public MyAdapter( Context c,List<dev_item> list)
    {
        if (0 != dev_grp.dev_num) {
            appList= list;
        } else {
            appList= new ArrayList<dev_item>();
        }
        mcontext = c;
        inflater = (LayoutInflater)mcontext.getSystemService(layoutInflater);
    }
    public int getCount() {
        return dev_grp.dev_num;
    }

    public void addItem(dev_item  item) {//添加单项
        if (item != null) {
            appList.add(item);
            notifyDataSetChanged();   // 通知适配器数据已改变
        }
    }
    public void removeItem(int position) {//删除
        if (appList.get(position) != null) {
            appList.remove(position);
            notifyDataSetChanged();  // 通知适配器数据已改变
        }
    }

    public dev_item getItem(int position) {
        return appList.get(position);
    }
    public long getItemId(int position) {
        return dev_grp.dev_num;
    }
    public View getView(int position, View grid, ViewGroup parent)
    {

            int h = myGridView.getHeight() * 12 / 27;
            grid = LayoutInflater.from(mcontext).inflate(R.layout.activity_dev_tock_control, null);
            AbsListView.LayoutParams param = new AbsListView.LayoutParams(
                    ViewGroup.LayoutParams.FILL_PARENT, h);
            grid.setLayoutParams(param);

        return grid;
    }    
}
  • 写回答

1条回答 默认 最新

  • danielinbiti 2015-05-07 03:28
    关注

    理解adapter和gridview的呈现是依据后台绑定的数据源来的。要界面增加item,也就是绑定的数据对象增加item数据再刷新。
    像你这getview很让人疑惑,这些有adapter都是根据数据驱动的。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?