简体   繁体   中英

How to fetch Sqlite data using the user input?

Currently i am working with an app that recommends the colleges to the students by taking the input as their aggregate Percentage. In the first activity I have one edit text used for percentage. In second activity I want to recommend the colleges according to input percentage for that i am using explicit intent (to pass the percentage from first activity to second) but i am not familiar with how to use that percentage to recommend the colleges list which suits the entered percentage.

MainActivity.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);
                    }
                }
        );

    }
}

DisplayList.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);
    }

}

DatabaseAccess.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;
    }
  • Pass the per variable in getColleges() method. like- getColleges(per);

and change the method and query in db file

like -

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;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM