简体   繁体   English

Android:ListActivity不显示任何项目

[英]Android: ListActivity does not display any Item

I hope you help me with this problem. 希望您能帮我解决这个问题。 My ListActivity Class woudlnt show ant item in the list 我的ListActivity类将在列表中显示ant项目

this is my code: 这是我的代码:

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();
        }
    }

}

and this is row.xml 这是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>

Notes: 笔记:

Im sure that the cursor has some data on it. 确保游标上有一些数据。 But It wouldnt display them 但是它不会显示它们

Help is appreciated 感谢帮助

您错过了对setContentView(View)的调用,该调用包含一个listview,应该使用Cursor中的数据填充该listview

Try this: 尝试这个:

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();
        }
    }

}

I have changed the dataSource to ListAdapter and also added the startManagingCursor . 我已经将dataSource更改为ListAdapter ,还添加了startManagingCursor

Also see the example over here 另请参阅此处的示例

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

You have forgotten to use the reference to the ListView, but as you have extended the ListActivity instead of Activity, you must get the reference to the ListActivity's List as follow: 您忘记了使用对ListView的引用,但是由于扩展了ListActivity而不是Activity,因此必须按如下方式获取对ListActivity的List引用:

ListView lv = getListView(); ListView lv = getListView();

I think you need to set the adapter on the list object loaded from xml. 我认为您需要在从xml加载的列表对象上设置适配器。 Something like: 就像是:

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

where R.id.ListViewLista is the id of the List resource inside your layout xml declaration (NOT row.xml) 其中R.id.ListViewLista是布局xml声明内的List资源的ID(不是row.xml)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM