ListView控件显示无数据,同时通过光标填充 [英] LIstView shows no data while populating through cursor

查看:145
本文介绍了ListView控件显示无数据,同时通过光标填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的Andr​​oid开发。我使用的ListView列出从SQLite数据库的一些数据。
我的main.xml文件看起来像:

I am new to Android development. I am using ListView to list some data from SQLite database. my Main.xml file looks like:

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/inspections"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
            <!-- Header -->
    <LinearLayout android:id="@+id/header"
        android:background="#ff347c12"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        >
        <TextView android:id="@+id/item1"
            android:layout_height="fill_parent"
            android:layout_width="wrap_content"
            android:width="100dip"
            android:height="30dip"
            android:text="Id"
        />
        <TextView android:id="@+id/item2"
            android:layout_height="fill_parent"
            android:layout_width="wrap_content"
            android:text="Customer"
            android:width="100dip"
            android:height="30dip"
        />
        <TextView android:id="@+id/item3"
            android:layout_height="fill_parent"
            android:layout_width="wrap_content"
            android:text="Contract"
            android:width="100dip"
            android:height="30dip"
        />
        <TextView android:id="@+id/item4"
            android:layout_height="fill_parent"
            android:layout_width="wrap_content"
            android:text="Inspector"
            android:width="100dip"
            android:height="30dip"
        />
    </LinearLayout>

    <!-- List Divider -->
    <View android:layout_width="fill_parent"
        android:layout_height="1dip"
        android:background="?android:attr/listDivider" />

    <!-- ListView (grid_items) -->
    <LinearLayout android:id="@+id/layout"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent">
        <ListView android:id="@+id/listview"
            android:layout_height="fill_parent"
            android:layout_width="fill_parent">
        </ListView>
    </LinearLayout>
</LinearLayout>

我使用的是用来存放每行的数据gr​​id_item.xml,它看起来像:

I am using grid_item.xml which is used to hold data of each row, it looks like:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
        <TextView android:id="@+id/item1"
            android:text="row_id"
            android:layout_height="fill_parent"
            android:layout_width="wrap_content"
            android:width="20dip"
        />
        <TextView android:id="@+id/item2"
            android:text="col_1"
            android:layout_height="fill_parent"
            android:layout_width="wrap_content"
            android:width="100dip"
        />
        <TextView android:id="@+id/item3"
            android:text="col_2"
            android:layout_height="fill_parent"
            android:layout_width="wrap_content"
            android:width="100dip"
        />
        <TextView android:id="@+id/item4"
            android:text="col_3"
            android:layout_height="fill_parent"
            android:layout_width="wrap_content"
            android:width="100dip"
        /> </LinearLayout>

code:

public class Inspections extends Activity{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        try
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.inspections);

            LoadInspections();
        }
        catch (Exception e) {
            CommonMethods.ShowMessageBox(this, e.toString());
        }
    }

    private void LoadInspections()
    {
        try
        {
            DBAdapter db = new DBAdapter(this);
            db.open();
            Cursor cur = db.GetInspections();
            if(cur.moveToFirst())
            {
                startManagingCursor(cur);
                ListView lv = (ListView)findViewById(R.id.listview);
                // create the grid item mapping
                String[] from = new String[] {"_id", "customer", "contract", "inspector"};
                int[] to = new int[] {R.id.item1, R.id.item2, R.id.item3, R.id.item4 };

                // fill in the grid_item layout
                SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.grid_item, cur, from, to);

                lv.setAdapter(adapter);  
                Log.d("Inspection", "Records found " + cur.getString(1) + "/ "+ cur.getString(2)+ "/ "+ cur.getString(3));
            }
            else
            {
                Log.d("Inspection", "No reords");
            }

            db.close();
        }
        catch(Exception ex)
        {
            Log.d("Inspection", "Error: "+ ex.toString());
        }
     }
}

问题
也有一些是错误的LoadInspections(),列表视图不显示数据,但只有标题-ROW。光标检索数据,检查该行:

Problem: There is something wrong with LoadInspections(), the list-view show no data but only the HEADER-ROW. The cursor is retrieving data, check this line:

Log.d("Inspection", "Records found " + cur.getString(1) + "/ "+ cur.getString(2)+ "/ "+ cur.getString(3));

日志窗口显示数据。

The log window is displaying the data.

我尽我所能找到计算器答案,但未能成功。
预先感谢您的宝贵时间和放大器;帮帮我。

I tried my best to find the answer on stackoverflow but could not succeeded. Thanks in advance for your valuable time & help.

推荐答案

我尝试不同的东西和放大器; 当我去掉了头和安培;从我的main.xml列表分隔部分,它的工作。很奇怪?

I was trying different things & when i removed the "Header" & "List Divider" sections from my main.xml it worked. Very Strange??

所以,我的main.xml现在看起来像:

So my main.xml now looks like:

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/inspections"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <!-- ListView (grid_items) -->
    <LinearLayout android:id="@+id/layout"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent">
        <ListView android:id="@+id/mylistview"
            android:layout_height="fill_parent"
            android:layout_width="fill_parent">
        </ListView>
    </LinearLayout>
</LinearLayout>

在列表视图的这种奇怪的行为可有人对此有何评论?

Can somebody comment on this strange behavior of listview?

这篇关于ListView控件显示无数据,同时通过光标填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆