与ListView控件中的其他活动无法显示列表 [英] Can not display list with ListView in another activity

查看:122
本文介绍了与ListView控件中的其他活动无法显示列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从活动1开始的意图,像后显示从数据库中活动2的一些数据:

I am trying to display some data from DB in Activity 2 after starting intent from Activity 1, like:

{       case R.id.buttonRead:

            intent = new Intent(this, ListDataActivity.class);

            startActivity(intent);

            break;
}

ListDataActivity有如下编码:

ListDataActivity has the following coding:

{
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_data);

        ListView lv = (ListView) findViewById(R.id.view_all_data);

        String[] from = new String[] { DBHelper.COLUMN_NAME,
                DBHelper.COLUMN_LAST_NAME };

        int[] to = new int[] { R.id.textView_name_reflect,
                R.id.textView_last_name_reflect };

        // create Adapter

        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
                R.layout.item, datasource.getAllEmployees(), from, to,
                FLAG_REGISTER_CONTENT_OBSERVER);

        // make adapter available for list

        lv.setAdapter(adapter);

}

ListDataActivity清单中没有停留意图过滤器。 当我点击按钮,开始ListDataActivity应用程序。崩溃与NullPointerException异常。 最有趣的是,当我从的onCreate ListDataActivity删除ListView控件运行在正常的方式显示空白屏幕。你能告诉我什么是错?

ListDataActivity in Manifest stays without Intent Filter. When I click button and start ListDataActivity app. crashes with NullPointerException. Most interesting is that when I delete ListView from onCreate ListDataActivity runs in normal way displaying blank screen. Could you please tell me what is wrong?

<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/view_all_data"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

</ListView>

从数据源的方法

method from DataSource

公开光标getAllEmployees(){

public Cursor getAllEmployees() {

Log.d(LOG_TAG, "--- Rows in mytable: ---");

// make query of all data from the table and receive instance of Cursor

Cursor c = db.query(DBHelper.TABLE_STAFF, null, null, null, null, null,
        null);

return c;

}

试过以下

case R.id.buttonRead:


    ArrayList<String> value = datasource.getAllEmployees();

    intent.putStringArrayListExtra("123", (ArrayList<String>)value);

    startActivity(intent);

    break;

}

修改

public ArrayList<String> getAllEmployees() {

ArrayList<String> empList = new ArrayList<String>();

    empList.add("Start");

    return empList;

}

以及ListDataActivity

as well as ListDataActivity

Intent intent = getIntent();

ArrayList<String> value = intent.getStringArrayListExtra("123");




ListView lv = (ListView) findViewById(R.id.view_all_data);



// create Adapter

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
        android.R.layout.simple_list_item_1, value);

// make adapter available for list

lv.setAdapter(adapter);

和它工作得很好

已经改变了我getAllEmployees如果看到的一切正确使用DB

Have changed my getAllEmployees to see if everything correct with DB

公共无效getAllEmployees(){

public void getAllEmployees() {

open();

Log.d(LOG_TAG, "--- Rows in mytable: ---");

// make query of all data from the table and receive instance of Cursor

Cursor c = db.query(DBHelper.TABLE_STAFF, null, null, null, null, null,
        null);

// put cursor into position of the first row of required data, if no
// rows false will return

if (c.moveToFirst()) {

    // get columns numbers

    int idColIndex = c.getColumnIndex(DBHelper.COLUMN_ID);
    int nameColIndex = c.getColumnIndex(DBHelper.COLUMN_NAME);
    int lastnameColIndex = c.getColumnIndex(DBHelper.COLUMN_LAST_NAME);
    int positionColIndex = c.getColumnIndex(DBHelper.COLUMN_POSITION);
    int departmentColIndex = c
            .getColumnIndex(DBHelper.COLUMN_DEPARTMENT);
    int inttelColIndex = c.getColumnIndex(DBHelper.COLUMN_INT_TEL);
    int mobileColIndex = c.getColumnIndex(DBHelper.COLUMN_MOB_TEL);
    int homeColIndex = c.getColumnIndex(DBHelper.COLUMN_HOME_TEL);
    int officemailColIndex = c
            .getColumnIndex(DBHelper.COLUMN_OFFICE_E_MAIL);
    int personalmailColIndex = c
            .getColumnIndex(DBHelper.COLUMN_PERSONAL_E_MAIL);

    do {

        // getting values by column numbers and put them into log

        Log.d(LOG_TAG,
                DBHelper.COLUMN_ID + c.getInt(idColIndex) + "\n"
                        + DBHelper.COLUMN_NAME
                        + c.getString(nameColIndex)
                        + DBHelper.COLUMN_LAST_NAME
                        + c.getString(lastnameColIndex)
                        + DBHelper.COLUMN_POSITION
                        + c.getString(positionColIndex)
                        + DBHelper.COLUMN_DEPARTMENT
                        + c.getString(departmentColIndex)
                        + DBHelper.COLUMN_INT_TEL
                        + c.getString(inttelColIndex)
                        + DBHelper.COLUMN_MOB_TEL
                        + c.getString(mobileColIndex)
                        + DBHelper.COLUMN_HOME_TEL
                        + c.getString(homeColIndex)
                        + DBHelper.COLUMN_OFFICE_E_MAIL
                        + c.getString(officemailColIndex)
                        + DBHelper.COLUMN_PERSONAL_E_MAIL
                        + c.getString(personalmailColIndex));

        // move to next row, if no next then end loop

    } while (c.moveToNext());

} else

    Log.d(LOG_TAG, "0 rows");

c.close();

}

verything正常工作与日志当我打电话datasource.getAllEmployees();从MainActivity但是当我尝试从ListDataActivity应用程序崩溃调用它。 有人可以解释我是怎么回事? 有检查DB一切工作正常。当把ListView控件到MainActivity一切工作正常,以及。 为什么它不与其他活动工作的?

verything works fine with Logs when I call datasource.getAllEmployees(); from MainActivity but when I try to call it from ListDataActivity app crashes. Can someone explain me what is going on? Have checked DB everything works fine. When put ListView into the MainActivity everything works fine as well. WHY IT DOES NOT WORK with another Activity?

推荐答案

对不起,是讨厌的StackOverflow的社区,但它似乎迟迟没有实行Android已经捉弄​​我。简单地忘了申报数据源类的变量。人民留心你做什么,给自己和surrounders!

Sorry for being annoying to stackoverflow community but it seems that long delay in practicing Android has played a trick with me. Simply forgot to declare variable of DataSource class. PEOPLE be ATTENTIVE to what you do, to yourself and to surrounders!

这篇关于与ListView控件中的其他活动无法显示列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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