如何使用来自两个不同表的数据填充自定义Listview行?

时间:2015-06-27 09:37:55

标签: android mysql listview android-listview

我有ListView它有三个TextViews和一个Button。我必须从一个表中获取数据以填充TextViews,并且Button由其他表中的数据填充。我使用TextViews填充了第一个表格中的CursorAdapter。现在我不知道如何在第二个数据库表的同一Button项中填充不可点击的ListView。有没有办法从两个单独的表中填充自定义ListView项?

  

我的DBHelper是

public class DatabaseHelper extends SQLiteOpenHelper {
public static final String db_name="unitdb";
public static final int version=1;
Context context;

public DatabaseHelper(Context context){
    super(context, db_name, null, version);
    this.context=context;
}

@Override
public void onCreate(SQLiteDatabase db){

    db.execSQL("CREATE TABLE bikeTable( _id INTEGER PRIMARY KEY AUTOINCREMENT, userName TEXT, mobileNo INTEGER, address TEXT, uSerialNo INTEGER UNIQUE, uImeiNo integer, uCimiNo INTEGER, uContactNo INTEGER, dealerCode INTEGER, dealerContactNo INTEGER, vehicleType TEXT)");
    Toast.makeText(context,"Database Created",Toast.LENGTH_SHORT).show();
    Log.i("dbcreate", "Database Created");
    Log.i("Table Created", "Bike Tracker Table Created");

    db.execSQL("CREATE TABLE dealerTable(_id INTEGER PRIMARY KEY AUTOINCREMENT, dealerName TEXT, mobileNo INTEGER, dealerCode INTEGER UNIQUE, address TEXT)");
    Log.i("Table Created", "Dealer Details Table Created");

    db.execSQL("CREATE TABLE statusTable(_id INTEGER PRIMARY KEY AUTOINCREMENT, userName TEXT, mobileNo INTEGER, serialNo INTEGER, deviceContact INTEGER, sentTime TEXT, sent TEXT, delivered TEXT, reply TEXT )");
    Log.i("Table Status Created","Status Table Created");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    if (oldVersion>=newVersion)

        return;

    if (oldVersion==1){

        Log.d("New Version","Data's can be Upgraded");

    }

    Log.d("Sample Data", "onUpgrade:" + newVersion);

}

public Cursor getAllDataBike(){

    String query= "select rowid _id,* from bikeTable where vehicleType='Two Wheeler'";
    SQLiteDatabase db=this.getReadableDatabase();
    return db.rawQuery(query,null);

}

public Cursor getAllDataCar(){

    String query= "select rowid _id,* from bikeTable where vehicleType='Car'";
    SQLiteDatabase db=this.getReadableDatabase();
    return db.rawQuery(query,null);

}

public Cursor getAllDataCv(){

    String query= "select rowid _id,* from bikeTable where vehicleType='Commercial Vehicle'";
    SQLiteDatabase db=this.getReadableDatabase();
    return db.rawQuery(query,null);

}

public Cursor getDealers(){

    String query="select rowid _id,* from dealerTable";
    SQLiteDatabase db=this.getReadableDatabase();
    return db.rawQuery(query,null);

}

public Cursor searchCustomerBike(String strFilter){

    String query;
    query="Select rowid _id,* from bikeTable where uSerialNo='"+strFilter+"' AND vehicleType='Two Wheeler'";
    SQLiteDatabase db=this.getReadableDatabase();
    return db.rawQuery(query,null);

}

public Cursor searchCustomerCar(String strFilter){

    String query;
    query="Select rowid _id,* from bikeTable where uSerialNo='"+strFilter+"' AND vehicleType='Car'";
    SQLiteDatabase db=this.getReadableDatabase();
    return db.rawQuery(query,null);

}

public Cursor searchCustomerCv(String strFilter){

    String query;
    query="Select rowid _id,* from bikeTable where uSerialNo='"+strFilter+"' AND vehicleType='Commercial Vehicle'";
    SQLiteDatabase db=this.getReadableDatabase();
    return db.rawQuery(query,null);

}

public Cursor getActivated(){

    String query="Select rowid _id,* from bikeTable where userName IS NOT NULL AND vehicleType='Two Wheeler'";
    SQLiteDatabase db=this.getReadableDatabase();
    return db.rawQuery(query,null);

}

public Cursor getNotActivated(){

    String query="Select rowid _id,* from bikeTable where userName IS NULL AND vehicleType='Two Wheeler'";
    SQLiteDatabase db=this.getReadableDatabase();
    return db.rawQuery(query,null);

}

public Cursor getActivatedCar(){

    String query="Select rowid _id,* from bikeTable where userName IS NOT NULL AND vehicleType='Car'";
    SQLiteDatabase db=this.getReadableDatabase();
    return db.rawQuery(query,null);

}

public Cursor getNotActivatedCar(){

    String query="Select rowid _id,* from bikeTable where userName IS NULL AND vehicleType='Car'";
    SQLiteDatabase db=this.getReadableDatabase();
    return db.rawQuery(query,null);

}

public Cursor getActivatedCv(){

    String query="Select rowid _id,* from bikeTable where userName IS NOT NULL AND vehicleType='Commercial Vehicle'";
    SQLiteDatabase db=this.getReadableDatabase();
    return db.rawQuery(query,null);

}

public Cursor getNotActivatedCv(){

    String query="Select rowid _id,* from bikeTable where userName IS NULL AND vehicleType='Commercial Vehicle'";
    SQLiteDatabase db=this.getReadableDatabase();
    return db.rawQuery(query,null);

}

public Cursor getStatus(String srNo){
    String query="select rowid _id,* from statusTable where serialNo='"+srNo+"'";
    SQLiteDatabase db=this.getReadableDatabase();
    return db.rawQuery(query,null);
}

}

  

我的CursorAdapter是

public class DisplayAdapter extends CursorAdapter{

DatabaseHelper databaseHelper;
SQLiteDatabase db;

@SuppressWarnings("deprecation")
public DisplayAdapter(Context context,Cursor c){
    super(context,c);
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    LayoutInflater inflater = LayoutInflater.from(parent.getContext());
    View retView = inflater.inflate(R.layout.card_items, parent, false);

    return retView;
}

@Override
public void bindView(View view, Context context, Cursor cursor) {
    TextView tvUserName=(TextView)view.findViewById(R.id.textView27);
    tvUserName.setText(cursor.getString(2));

    TextView tvMobile=(TextView)view.findViewById(R.id.textView28);
    tvMobile.setText(cursor.getString(3));

    TextView tvSerail=(TextView)view.findViewById(R.id.textView29);
    tvSerail.setText(cursor.getString(5));
}

}

0 个答案:

没有答案