显示从sqlite到listview的单个列

时间:2018-05-09 12:07:53

标签: android sqlite listview

我有一个大学数据库。我只想在列表视图中显示大学名称列。为此,我创建了getData()对象来获取行。然后它将显示在列表视图中。我从数据库中获取大学名称,但格式不正确。我只是在列表视图中随机获得多个大学名称。

这是我的数据库文件

public class UniversityFinderDB  extends SQLiteOpenHelper{

private static final int DATABASE_VERSION= 1;
static final String DATABASE_NAME="universities.db";
private static final String TABLE_NAME="universityFinder";

private static final String COLUMN_UNIVID="univId";
private static final String COLUMN_UNIVERSITYNAME="univName";
private static final String COLUMN_SCORE="score";

SQLiteDatabase db;




private static final String TABLE_CREATE = "create table IF NOT EXISTS universityFinder (univId integer PRIMARY KEY , univName varchar not null, score integer not null);";

public UniversityFinderDB(Context context) {
    super(context, DATABASE_NAME,null ,DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase database) {

    database.execSQL(TABLE_CREATE);


}


public void addUser(Universities universities) {
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();

    values.put(COLUMN_UNIVID, universities.getUnivId());
    values.put(COLUMN_UNIVERSITYNAME, universities.getUnivName());
    values.put(COLUMN_SCORE, universities.getGre());



    // Inserting Row
    db.insert(TABLE_NAME, null, values);
    db.close();
}



public List<String> getData(int input){
    String query="select univName from universityFinder  where score >="+input+"";
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor =db.rawQuery(query, null);

    List<String> attrStr = new Vector<String>();

    if(cursor.moveToFirst()){
        do{
            attrStr.add(cursor.getString(cursor.getColumnIndex(COLUMN_UNIVERSITYNAME)));
            Arrays.toString(new List[]{attrStr});
        }while  (cursor.moveToNext());
    }
    while (cursor.moveToNext()){

    }
    return attrStr;
}

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

    String query = "DROP TABLE IF EXISTS " +TABLE_NAME;
    db.execSQL(query);
    onCreate(db);
}


}

这里是listview文件,它将从用户那里获得分数,然后只显示具有该分数或更高分数的大学。但不是那样,我只是随机抽取一系列大学。

public class ListviewFinderUniversities extends AppCompatActivity {

UniversityFinderDB myDB;
Universities universities;

ListView finderListview;

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


     myDB = new UniversityFinderDB(this);
     universities = new Universities();

    int gre = getIntent().getIntExtra("Gre",0);


    finderListview = (ListView) findViewById(R.id.finderListView);


    List<String> data = myDB.getData(gre);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(ListviewFinderUniversities.this, android.R.layout.simple_list_item_1, data);

    finderListview.setAdapter(adapter);
}
}

1 个答案:

答案 0 :(得分:0)

使用以下查询: 避免使用多个名称,也可以按顺序获取列表

字符串查询=“从universityFinder中选择不同的univName,其中得分&gt; =”+输入+“按univName排序”;