java—如何使用用户输入获取sqlite数据?

kmbjn2e3  于 2021-06-27  发布在  Java
关注(0)|答案(1)|浏览(362)

目前,我正在使用一个应用程序,通过将输入作为他们的总百分比来向学生推荐大学。在第一个活动中,我有一个用于百分比的编辑文本。在第二个活动中,我想根据输入百分比推荐学院,因为我使用了明确的意图(将百分比从第一个活动传递到第二个活动),但我不熟悉如何使用该百分比推荐适合输入百分比的学院列表。
主活动.java

public class MainActivity extends AppCompatActivity {

    Button submitData;
    EditText percentage;
    public String pData;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
        setContentView(R.layout.activity_main);

        percentage = (EditText)findViewById(R.id.studentpercentage);
        submitData = (Button)findViewById(R.id.submitdetails);

        submitData.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        pData = percentage.getText().toString();
                        Intent intent = new Intent(getApplicationContext(), DisplayList.class);
                        intent.putExtra("Percentage", pData);
                        startActivity(intent);
                    }
                }
        );

    }
}

显示列表.java

public class DisplayList extends AppCompatActivity {

    private ListView listView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
        setContentView(R.layout.activity_display_list);

        TextView percentage = (TextView)findViewById(R.id.textView);
        Bundle bundle = getIntent().getExtras();

        if (bundle.getString("Percentage")!=null) {
            String per = (String)bundle.get("Percentage");
        }

        this.listView = (ListView)findViewById(R.id.listview);
        DatabaseAccess databaseAccess = DatabaseAccess.getInstance(this);
        databaseAccess.open();
        List<String> colleges = databaseAccess.getColleges();
        databaseAccess.close();

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,colleges);
        this.listView.setAdapter(adapter);
    }

}

数据库访问.java

/**
     * Read all colleges from the database.
     */

    public List<String> getColleges() {
        List<String> list = new ArrayList<>();
        Cursor cursor = database.rawQuery("SELECT * FROM Ahmednagar", null);
        cursor.moveToFirst();
        while (!cursor.isAfterLast()) {
            list.add(cursor.getString(1));
            list.add(cursor.getString(2));
            list.add(cursor.getString(3));
            list.add(cursor.getString(4));
            list.add(cursor.getString(5));
            cursor.moveToNext();
        }
        cursor.close();
        return list;
    }
nfs0ujit

nfs0ujit1#

在getcolleges()方法中传递per变量。类似学院(per);
并在db文件中更改方法和查询
就像-

public List<String> getColleges(String per) {
        List<String> list = new ArrayList<>();
        Cursor cursor = database.rawQuery("SELECT * FROM Ahmednagar Where Perentage="+per, null);
        cursor.moveToFirst();
        while (!cursor.isAfterLast()) {
            list.add(cursor.getString(1));
            list.add(cursor.getString(2));
            list.add(cursor.getString(3));
            list.add(cursor.getString(4));
            list.add(cursor.getString(5));
            cursor.moveToNext();
        }
        cursor.close();
        return list;
    }

相关问题