0

我希望你能帮我解决这个问题。我的 ListActivity 类不会在列表中显示蚂蚁项目

这是我的代码:

import android.os.Bundle;
import android.provider.BaseColumns;
import android.app.Activity;
import android.app.ListActivity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.CursorAdapter;
import android.widget.EditText;
import android.widget.SimpleCursorAdapter;
import android.support.v4.app.NavUtils;

public class Sale2 extends ListActivity {
    private static final int DIALOG_ID = 100;

    private SQLiteDatabase database;

    DBAdapter db;

    private CursorAdapter dataSource;

    private View entryView;

    private EditText firstNameEditor;

    private EditText lastNameEditor;

    private static final String fields[] = { "_id", "_date", "_soldPrice" };

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        try {
            db = new DBAdapter(this);
            db.open();
            Cursor data = db.getAllSales2();
            System.out.println("after getallsales2");
            dataSource = new SimpleCursorAdapter(this, R.layout.row, data,
                    fields, new int[] { R.id._id, R.id.first, R.id.last });
            System.out.println("after simplecursoradapter"
                    + dataSource.toString());

            setListAdapter(dataSource);
            System.out.println("setListAdapter");
        } catch (Exception e) {
            Log.e("ERROR", "ERROR IN CODE: " + e.toString());
            e.printStackTrace();
        }
    }

}

这是 row.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/first"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/last"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

笔记:

我确定光标上有一些数据。但它不会显示它们

帮助表示赞赏

4

4 回答 4

1

您缺少对 setContentView(View) 的调用,其中包含一个列表视图,该列表视图应填充来自 Cursor 的数据

于 2012-07-17T14:50:24.243 回答
0

尝试这个:

public class Sale2 extends ListActivity {
    private static final int DIALOG_ID = 100;

    private SQLiteDatabase database;

    DBAdapter db;

    private ListAdapter dataSource;

    private View entryView;

    private EditText firstNameEditor;

    private EditText lastNameEditor;

    private static final String fields[] = { "_id", "_date", "_soldPrice" };

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        try {
            db = new DBAdapter(this);
            db.open();
            Cursor data = db.getAllSales2();
            System.out.println("after getallsales2");
            startManagingCursor(data);
            dataSource = new SimpleCursorAdapter(this, R.layout.row, data,
                    fields, new int[] { R.id._id, R.id.first, R.id.last });
            System.out.println("after simplecursoradapter"
                    + dataSource.toString());

            setListAdapter(dataSource);
            System.out.println("setListAdapter");
        } catch (Exception e) {
            Log.e("ERROR", "ERROR IN CODE: " + e.toString());
            e.printStackTrace();
        }
    }

}

我已将 dataSource 更改为ListAdapter并添加了startManagingCursor.

另请参阅此处的示例

http://developer.android.com/reference/android/app/ListActivity.html

于 2012-07-17T15:49:34.870 回答
0

您忘记使用对 ListView 的引用,但由于您扩展了 ListActivity 而不是 Activity,因此您必须获取对 ListActivity 的 List 的引用,如下所示:

ListView lv = 获取列表视图();

于 2012-07-17T16:26:40.727 回答
-1

我认为您需要在从 xml 加载的列表对象上设置适配器。就像是:

listaNodiView = (ListView) findViewById(R.id.ListViewLista);
listaNodiView.setListAdapter(dataSource);

布局 xml 声明中的 List 资源的 id在哪里R.id.ListViewLista(不是 row.xml)

于 2012-07-17T14:49:22.343 回答