使用查询在列表视图中显示Sliqte数据

时间:2018-05-04 00:05:12

标签: java android xml sqlite listview

我有一个得分大学的SQLite数据库。每所大学都有独特的分数。这是数据库代码。

public class UniversityFinderDB {

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

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

SQLiteDatabase db;

Context context;


public void openDatabase(Context context, String database){
    this.context=context;
    db=this.context.openOrCreateDatabase(database,MODE_PRIVATE,null);
    createTable();
}

private void createTable() {
    String query="create table university (univId integer primary key  not null, univName varchar not null, score integer not null);";
    db.execSQL(query);
}

public void executeEventInsert(){

    String query="INSERT INTO university (univName, score) VALUES('UCI',122);";
    String query1="INSERT INTO university (univName, score) VALUES('UMI',132);";
    String query2="INSERT INTO university (univName, score) VALUES('LLC',222);";
    String query3="INSERT INTO university (univName, score) VALUES('OIC',432);";
    db.execSQL(query);
    db.execSQL(query1);
    db.execSQL(query2);
    db.execSQL(query3);
}


public ArrayList<HashMap<String,String>> executeSelectEvents(int input){
    String query="select" +COLUMN_UNIVERSITYNAME+ "from"+TABLE_NAME+"where" +COLUMN_SCORE+" >="+input;
    Cursor cursor= db.rawQuery(query,null);
    ArrayList<HashMap<String,String>> events=new ArrayList<>();
    while (cursor.moveToNext()){
        HashMap<String,String> event=new HashMap<>();

        String colUniversityId=cursor.getColumnName(0);
        String colUniversityIdValue=cursor.getString(0);
        String  colUniversityName=cursor.getColumnName(1);
        String colUniversityValue=cursor.getString(1);


        event.put(colUniversityId,colUniversityIdValue);
        event.put(colUniversityName,colUniversityValue);
        events.add(event);
    }
    return events;
}

}

然后我有一个School Finder活动课,它有一个得分编辑文本和一个按钮。这是XML文件。

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="26dp"
    android:layout_marginTop="107dp"
    android:text="Gre:"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<Button
    android:id="@+id/shoot_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="31dp"
    android:layout_marginStart="106dp"
    android:onClick="onClickUnivList"
    android:text="View Universities"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toStartOf="parent" />

<EditText
    android:id="@+id/greFinder"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:ems="10"
    android:inputType="number"
    tools:layout_editor_absoluteX="72dp"
    tools:layout_editor_absoluteY="96dp" />

这个学校发现者的java课程在下面

public class SchoolFinder extends AppCompatActivity {

UniversityFinderDB db;

EditText greScore;
Button submitButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_school_finder);

    submitButton = (Button) findViewById(R.id.shoot_button);
    greScore = (EditText) findViewById(R.id.greFinder);
}
public void onClickUnivList(View view) {
    String gre = greScore.getText().toString();
    int greValue = Integer.parseInt(gre);
    Intent intent = new Intent(SchoolFinder.this, 
 ListviewFinderUniversities.class);
    intent.putExtra("Gre",greValue);
    startActivity(intent);
}
}

Listview类

 public class ListviewFinderUniversities extends AppCompatActivity {

UniversityFinderDB myDB;

ArrayList<HashMap<String, String>> eventData;
ListView finderListview;

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




    finderListview = (ListView) findViewById(R.id.finderListView);
    myDB.openDatabase(getApplicationContext(),UniversityFinderDB.DATABASE_NAME);



    loadUnivFromDb();

}

private void createUnivAdapter(){
    String from[]={"univId","univName"};
    int to[] = {R.id.eventId, R.id.evenLabel};
    SimpleAdapter adapter = new SimpleAdapter(getApplicationContext(), eventData, R.layout.uni_item_finder, from, to);
    finderListview.setAdapter(adapter);
}

private void loadUnivFromDb() {
    int gre = getIntent().getIntExtra("Gre",0);

    ArrayList<HashMap<String, String>> data= myDB.executeSelectEvents(gre);
    if(data.size()>0){
        eventData=data;
        createUnivAdapter();
    }else {
        Toast.makeText(getApplicationContext(),"NoEvents",Toast.LENGTH_SHORT).show();
    }

}

现在,一旦用户输入分数并按下提交按钮,应用程序必须显示分数高于用户在具有列表视图的新活动中输入的数字的大学列表。如何为此问题添加查询?请帮助尝试构建我自己的项目的新编程。

1 个答案:

答案 0 :(得分:0)

您可以使用

public ArrayList<HashMap<String,String>> executeSelectEvents(int input){
    String query="select * from "+TABLE_NAME+" where " +COLUMN_SCORE+" >= "+input;
    Cursor cursor= db.rawQuery(query,null);
    ArrayList<HashMap<String,String>> events=new ArrayList<>();
    while (cursor.moveToNext()){
        HashMap<String,String> event=new HashMap<>();

        String colUniversityId=cursor.getColumnName(0);
        String colUniversityIdValue=cursor.getString(0);
        String  colUniversityName=cursor.getColumnName(1);
        String colUniversityValue=cursor.getString(1);


        event.put(colUniversityId,colUniversityIdValue);
        event.put(colUniversityName,colUniversityValue);
        events.add(event);
    }
    return events;
}