I am a new to android world. I have a problem with the code. My listview is not displaying ArrayList method. It is returning blank as the value in the listview. What could be the issue to this problem.
here is my db file which has the ArrayList execute select events. This will fetch the data from the database and then display it to listview.
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 universityFinder (univId integer primary key not null, 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 executeEventInsert(String name, String score){
String query="INSERT INTO universityFinder(univName, score) VALUES('"+name+"','"+score+"');";
db.execSQL(query);
}
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;
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String query = "DROP TABLE IF EXISTS "+TABLE_NAME;
db.execSQL(query);
}
}
and the listview class where it'll display the data from the database.
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);
myDB = new UniversityFinderDB(this);
finderListview = (ListView) findViewById(R.id.finderListView);
myDB.executeEventInsert("UCI","101");
myDB.executeEventInsert("UeI","100");
myDB.executeEventInsert("UgI","105");
myDB.executeEventInsert("UjI","107");
myDB.executeEventInsert("ewI","109");
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();
}
}
}