从SQLite数据库填充ListView

时间:2011-01-31 11:16:04

标签: android sqlite listview

public class ConnectDatabase extends Activity {
    private SimpleDBAdapter mDbHelper;
    private ListView list;
    private String[] values;
    private String[] list1;
    private int i=0;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        list=(ListView )findViewById(R.id.simpleListView);

        mDbHelper = new SimpleDBAdapter(ConnectDatabase.this);
        mDbHelper.createDatabase();
        mDbHelper.open();
        values = mDbHelper.getEditTextValue();
        mDbHelper.close();

        for(i=0;i<10;i++) {
            String tempvalue;
            tempvalue=values[i];
            list1[i]=tempvalue;
            Log.v("log_tag"," tempvalue   "+tempvalue);
            Log.v("log_tag", "list1 is"+list1[i]);

            //Log.v("log_tag"," list1"+list1[i]); 
        }

        list.setAdapter(new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, values));

        //list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

        //list.setTextFilterEnabled(true);

        list.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?> parent, View view, 
                    int position, long id) {
                // When clicked, show a toast with the TextView text
                Toast.makeText(getApplicationContext(),
                       ((TextView) view).getText(), Toast.LENGTH_SHORT).show();
                /*for(int i=0;i<values.length;i++) {
                    String templist=values[i];
                    list1[i]=templist;

                }*/

                String fname=values[position];
                Log.v("log_tag", "The fname is"+ fname);
                Intent intent=new Intent(ConnectDatabase.this, Userinfo.class);
                intent.putExtra("FirstName",fname );
                startActivity(intent);
            }
        });

    // to remove the last comma

    }
}

这里我正在从SQLite数据库中做ListView并希望在ListView中显示当时只有前10个ListView行但我在行list1[i]=tempvalue;处得到错误所以我不能复制数组这里任何人都可以帮我复制数组名称值,从我的SQLite数据库中获取所有列索引值。

希望任何人都有这样的想法。

1 个答案:

答案 0 :(得分:1)

您的帖子很难阅读,但我认为问题是list1尚未初始化。

您需要添加如下所示的行:

list1 = new String[10];
在尝试在数组中设置值之前

ps - 由于10循环有for,我选择了i<10的值。

相关问题