2014-09-26 121 views
-1

我想从Sqlite数据库中提取特定数据并将其显示到列表中,但我有一个问题来获取特定数据,请帮助我我是newbee机器人。
在此先感谢从sqlite数据库中提取特定数据并在列表视图中显示

这是Activity类:

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; 
    } 
+1

有什么错误?如果是的话发布logcat – 2014-09-26 07:08:31

+0

你在找什么?查询是错误的。 – iZBasit 2014-09-26 07:13:12

+0

我只是编辑你的问题缩进代码,看到你有一个大括号“}”不想和你的活动类的最后一行,它可能是...(我删除它在你的问题) – 2014-09-26 07:16:31

回答

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; 
    } 
0

我认为你有一个奇怪的查询存在。尝试改变你的查询是这样的Cursor res = db.rawQuery("select status from task WHERE status LIKE ? " + Processing +", null);

相关问题