【问题标题】:how to get values from sqlite and store it in array then how to give that array to the list view in android如何从 sqlite 获取值并将其存储在数组中,然后如何将该数组提供给 android 中的列表视图
【发布时间】:2013-02-25 06:32:26
【问题描述】:

伙计们,当我运行这个时,我得到了错误作为事实异常和空指针异常 我如何解决它
如何从 sqlite 获取值并将其存储在数组中,然后如何将该数组提供给 android 中的列表视图

公共类 FindByLocation 扩展 ListActivity{

AutoCompleteTextView t1;
Button b1;
ListView l1;
SQLiteDatabase sqldb;
Cursor cur;
String[] loc;
String[] locat={"//some values i given "};  

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.location);
    t1=(AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);
    b1=(Button)findViewById(R.id.locationSearchbtn);

    l1=getListView();
    l1.setChoiceMode(2);
    l1.setTextFilterEnabled(true);
    ArrayAdapter<String> aa=new ArrayAdapter<String>(FindByLocation.this, android.R.layout.simple_dropdown_item_1line, locat);
    t1.setAdapter(aa);

    b1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            String getloc=t1.getText().toString();
            sqldb=FindByLocation.this.openOrCreateDatabase("madrasmtcinfo", MODE_PRIVATE, null);
            try{
                cur=sqldb.rawQuery("select * from mtcbusroute where route Like '%"+getloc+"%';", null);

                if(cur.moveToFirst()){  
                    int i=0;
                    do{

                        loc[i]="" + cur.getString(cur.getColumnIndexOrThrow("routeno")).toString() + ", " + cur.getString(cur.getColumnIndexOrThrow("source")).toString()+", "+ cur.getString(cur.getColumnIndexOrThrow("Destination")).toString();
                        i++;
                        cur.moveToNext();   
                    }while(cur.isLast()!=true);
                }
            }

            catch(Exception e){
                Toast.makeText(getBaseContext(), "Error"+e, Toast.LENGTH_SHORT).show();
            }
            setListAdapter(new ArrayAdapter<String>(FindByLocation.this, android.R.layout.simple_list_item_1, loc));


        }
    });
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    // TODO Auto-generated method stub
    super.onListItemClick(l, v, position, id);
    Toast.makeText(getBaseContext(), ""+loc[position], Toast.LENGTH_SHORT).show();
}

}

【问题讨论】:

  • 使用前需要初始化数组String[] loc;

标签: android


【解决方案1】:

看看这些链接

SQLite and populating list view, android

http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/

我认为它会对你有很大帮助。让我知道您对此的状态。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-31
    • 2014-01-12
    • 1970-01-01
    • 2017-05-10
    • 2020-05-13
    相关资源
    最近更新 更多