mardi 30 juin 2015

How to select the remaining rows after selected some in Android SQLite?

I have a task in which i have to display the rows selected using a query in one tab and the remaining rows in another tab. I have displayed the selected rows using SimpleCursorAdapter. When i tried to display the remaining rows in next tab it throws error. I have tried NOT IN but it also doesn't work. I have tried NOT EXISTS also it shows all rows. Anyone who can answer please help. I have posted my code here. Thanks in advance. This is the activity of first tab which displays selected rows

public class OnlineDevices extends Activity {
ListView listOnline;
DatabaseHelper databaseHelper;
String count;
int conut;
TextView tvOnlineCount;

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

    listOnline=(ListView)findViewById(R.id.listView3);
    tvOnlineCount = (TextView) findViewById(R.id.textView59);

    databaseHelper=new DatabaseHelper(this);
    SQLiteDatabase db=databaseHelper.getReadableDatabase();

    String date= DateFormat.getDateInstance().format(new Date());
    String statusQuery="select rowid _id, deviceContact from statusTable where sentTime='"+date+"'";
    Cursor cursor1=db.rawQuery(statusQuery,null);

    if (cursor1.getCount()>0){
        while (cursor1.moveToNext()){
            String deviceNo=cursor1.getString(cursor1.getColumnIndex("deviceContact"));

            String device=deviceNo.substring(2, 12);

            String query="select rowid _id, userName, uSerialNo from bikeTable where uContactNo='"+device+"' AND userName IS NOT NULL";
            Cursor cursor2=db.rawQuery(query, null);
            SimpleCursorAdapter adapter=new SimpleCursorAdapter(this,R.layout.status_item,cursor2,new String[]{"userName","uSerialNo"},new int[]{R.id.textView51,R.id.textView52});
            listOnline.setAdapter(adapter);
        }
    }
    try {
        conut = listOnline.getAdapter().getCount();
        count = String.valueOf(conut);
        tvOnlineCount.setText(count);
    }catch (Exception e){
        e.printStackTrace();
        int i=0;
        Toast.makeText(OnlineDevices.this,"No device is active",Toast.LENGTH_SHORT).show();
        tvOnlineCount.setText(String.valueOf(i));
    }
}

}

Activity for second tab which display the remaining rows are

public class OfflineDevices extends Activity {

ListView listOffline;
DatabaseHelper databaseHelper;
String deviceContact;

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

    listOffline=(ListView)findViewById(R.id.listView4);

    databaseHelper=new DatabaseHelper(this);
    SQLiteDatabase db=databaseHelper.getReadableDatabase();

    String date= DateFormat.getDateInstance().format(new Date());
    String query="select rowid _id, deviceContact from statusTable where sentTime='"+date+"'";
    Cursor cursor1=db.rawQuery(query,null);

    if (cursor1.getCount()>0){
        while (cursor1.moveToNext()) {
            deviceContact = cursor1.getString(cursor1.getColumnIndex("deviceContact"));
        }
            String device=deviceContact.substring(2, 12);
            String query2="select rowid _id, userName, uSerialNo from bikeTable where userName IS NOT NULL AND uContactNo='"+device+"'";
            Cursor cursor3=db.rawQuery(query2,null);
            String query1="select rowid _id,*from bikeTable where userName IS NOT NULL NOT IN('"+query2+"')";
            Cursor cursor2=db.rawQuery(query1,null);
            SimpleCursorAdapter adapter=new SimpleCursorAdapter(this,R.layout.status_item,cursor2,new String[]{"userName","uSerialNo"},new int[]{R.id.textView51,R.id.textView52});
            listOffline.setAdapter(adapter);


    }
}

}

Aucun commentaire:

Enregistrer un commentaire