2014-12-19 49 views
-2

我是新来的android和第一次使用sqlite rawquery动态where子句条件,并不知道如何使用它。我想给动态值的where子句根据特定的“mid”得到listview。如何从SubjectActivty在android中的rawquery动态where子句条件?

这里提供的价值中旬是我的代码:

TestTable的:

public long insert(String id, String time, int mid, String cmarks, String nmarks, 
     String questions, String testType, String test,String marks) { 

    log("insert test : " + test); 

    ContentValues values = new ContentValues(); 
    values.put(KEY_TESTID, id); 
    values.put(KEY_MID,mid); 
    values.put(KEY_TEST, test); 
    values.put(KEY_TIME, time); 
    values.put(KEY_CMARK, cmarks); 
    values.put(KEY_NMARK, nmarks); 
    values.put(KEY_TESTTYPE, testType); 
    values.put(KEY_QUESTION, questions); 
    values.put(KEY_Total_Marks, marks); 
    return db.insert(TABLE_NAME2, null, values); 
} 

    public ArrayList<NotificationListItem> getAllList(
     ArrayList<NotificationListItem> privateArrayList) { 

    openToRead(); 
    privateArrayList.clear(); 

    Cursor cursor = null; 
    String sql ="SELECT * FROm test_list WHERE mid=?"; 
    cursor= db.rawQuery(sql, null); 
    log("getAlllist() cursor : " + cursor.getCount()); 

    if (cursor != null) { 
     log("getAlllist() cursor not null "); 

     int index = 0; 
     cursor.moveToFirst(); 

     while (index < cursor.getCount()) { 

      NotificationListItem item = new NotificationListItem(); 

      int idIndex = cursor.getColumnIndex(TestTable.KEY_TESTID); 
      int subid= cursor.getColumnIndex(TestTable.KEY_MID); 
      int nameIndex = cursor.getColumnIndex(TestTable.KEY_TEST); 
      int idTime = cursor.getColumnIndex(TestTable.KEY_TIME); 
      int cMarks = cursor.getColumnIndex(TestTable.KEY_CMARK); 
      int nMarks = cursor.getColumnIndex(TestTable.KEY_NMARK); 
      int testTypeIndex = cursor.getColumnIndex(TestTable.KEY_TESTTYPE); 
      int questions = cursor.getColumnIndex(TestTable.KEY_QUESTION); 

      item.name = cursor.getString(nameIndex); 
      item.testID = cursor.getString(idIndex); 
      item.mid=cursor.getInt(subid); 
      item.time = cursor.getString(idTime); 
      item.cmark = cursor.getString(cMarks); 
      item.nmark = cursor.getString(nMarks); 
      item.testType = cursor.getString(testTypeIndex); 
      item.questions = cursor.getString(questions); 

      index++; 
      privateArrayList.add(item); 
      cursor.moveToNext(); 
     } 
     log(" query(): cursor closing"); 
     cursor.close(); 
     db.close(); 
     db = null; 
    } 
    return privateArrayList; 
} 

SubjectActvity.class

@Override 
protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_subject); 

    privateListLV = (ListView) findViewById(R.id.privateListLV); 
    privatelistTable = new SubjectTable(SubjectActivity.this); 
    testTableStatic = new StaticTestTable(this); 
    testTable = new TestTable(SubjectActivity.this); 

    privatelistTable.openToWrite(); 
    privatelistTable.deleteAll(); 
    privatelistTable.insert(10, "Biology"); 
    privatelistTable.insert(20, "Chemistry"); 
    privatelistTable.insert(30, "English"); 
    privatelistTable.insert(40, "Maths"); 
    privatelistTable.insert(50, "GK"); 

    testTable.openToWrite(); 
    testTable.deleteAll(); 
    testTable.insert("1", "10", 10, "5", "2", "2", "Both", "Anatomy", "10"); 
    testTable.insert("2", "10", 20, "5", "2", "2", "Both", "Paper1", "10"); 

    privateArrayList = new ArrayList<NotificationListItem>(); 

    listAdapter = new SubjectCustomListAdapter(this, privateArrayList, 
      privatelistTable); 

    privateListLV.setAdapter(listAdapter); 

    privateListLV.setOnItemClickListener(new OnItemClickListener() { 

     @SuppressWarnings("unchecked") 
     public void onItemClick(AdapterView<?> adapter, View arg1, 
       int position, long arg3) { 
      NotificationListItem selection = (NotificationListItem) adapter 
        .getItemAtPosition(position); 
      String item = selection.getName(); 
      System.out.println("item" +item); 

      if (!item.contentEquals(" ")) { 
       subjectid = privatelistTable.getSinlgeEntry(item); 
       Log.e("selected Value", " " + subjectid); 


       Intent testact = new Intent(getApplicationContext(), 
         TestsActivity.class); 
       testact.putExtra("subject", item); 
       testact.putExtra("mid",subjectid); 
       startActivity(testact); 

      } else { 
       return; 
      } 
     } 
    }); 
} 

@Override 
protected void onResume() { 

    super.onResume(); 
    updateList(); 

} 
private void updateList() { 
    privatelistTable.getAllList(privateArrayList); 
    listAdapter.notifyDataSetChanged(); 
} 

回答

1

做的@Der傀儡回答或另一种方式是

Cursor c =db.rawQuery("SELECT * FROM " + tableName + " where mid=" + mid , null); 
+1

@DerG olem Yup刚刚编辑.. – 2014-12-19 11:35:19

+0

@DerGolem如何从活动中传递mid的值,因为我想从运行时决定mid的值。请帮忙。 – 2014-12-22 07:53:20