从sqlite数据库中获取特定数据并在列表视图中显示它

时间:2014-09-26 07:03:34

标签: android sqlite

我想从Sqlite数据库中获取特定数据并将其显示在列表中,但是我在获取特定数据时遇到问题所以请帮助我在android中新手。
提前致谢

这是活动类:

    private ListView obj;   
    DBHelper mydb;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.list);

            mydb = new DBHelper(this);
            ArrayList array_list = mydb.getAllCotacts();

            ArrayAdapter arrayAdapter = 
            new ArrayAdapter(this,android.R.layout.simple_list_item_1, array_list);

            // Adding it to the list view.
            obj = (ListView)findViewById(R.id.listView1);
            obj.setAdapter(arrayAdapter);
            obj.setOnItemClickListener(new OnItemClickListener(){
                    @Override
                    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                            // TODO Auto-generated method stub
                            int id_To_Search = arg2 + 1;
                            Bundle dataBundle = new Bundle();
                            dataBundle.putInt("id", id_To_Search);
                            Intent intent = new Intent(getApplicationContext(),DisplayTask.class);
                            intent.putExtras(dataBundle);
                            startActivity(intent);
                    }
            });
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.mainmenu, menu);
            return true;
    }
    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
            super.onOptionsItemSelected(item); 
            switch(item.getItemId()) 
            { 
                    case R.id.item1: 
                    Bundle dataBundle = new Bundle();
                    dataBundle.putInt("id", 0);
                    Intent intent = new Intent(getApplicationContext(),DisplayTask.class);
                    intent.putExtras(dataBundle);
                    startActivity(intent);
                    return true; 

                    case R.id.item2: 

                    Intent intent1 = new Intent(getApplicationContext(),radioButton.class);
                    startActivity(intent1);

                    default: 
                    return super.onOptionsItemSelected(item); 
            } 
    } 
    public boolean onKeyDown(int keycode, KeyEvent event) {
            if (keycode == KeyEvent.KEYCODE_BACK) {
                    moveTaskToBack(true);
            }
            return super.onKeyDown(keycode, event);
    }

这是DBHelper类

    public ArrayList getAllCotacts()
    {
            ArrayList array_list = new ArrayList();
            //hp = new HashMap();
            SQLiteDatabase db = this.getReadableDatabase();
            Cursor res =  db.rawQuery( "select status from task WHERE status LIKE "Processing%"", null );
            res.moveToFirst();
            while(res.isAfterLast() == false){

                    String Name = res.getString(res.getColumnIndex(TASKS_COLUMN_TITLE));
                    Log.d("DBHelper", Name);

                    array_list.add(res.getString(res.getColumnIndex(TASKS_COLUMN_TITLE)));
                    res.moveToNext();
            }
            return array_list;
    }

2 个答案:

答案 0 :(得分:0)

我认为那里有一个奇怪的问题。尝试更改您的查询Cursor res = db.rawQuery( "select status from task WHERE status LIKE ? " + Processing +", null );

答案 1 :(得分:0)

 public ArrayList getProcessingCotacts()
   {
  ArrayList array_list = new ArrayList();
  //hp = new HashMap();
  SQLiteDatabase db = this.getReadableDatabase();

  String textValue = "Processing";
  String query = "select * from task where status = ?";
  Cursor res = db.rawQuery(query, new String[] {textValue});

  //Cursor res =  db.rawQuery( "select * from task", null );
  res.moveToFirst();
  while(res.isAfterLast() == false){

  array_list.add(res.getString(res.getColumnIndex(TASKS_COLUMN_TITLE)));
  res.moveToNext();
  }
   return array_list1;
   }