samedi 26 décembre 2015

Populating ListView from Database

I'm using this one to populate data from my database, data is not populating from database to my listView, But I can't detect where is the problem.

public class listView extends AppCompatActivity {

final ArrayList<CountryItems> countryItems = new ArrayList<>();
CountryAdepter countryAdepter;
ListView listView;
private SQLiteDatabase database;

private static final String DB_NAME = "Country_Details.db";

//For Continent
private static final String TABLE_NAME = "Continent";
private static final String UID = "_id";
private static final String CONTINENT = "Continent";
private static final String AREA_KM = "Area_Km";
private static final String POPULATION = "Population";
private static final String DENSITY_KM = "Density_People_Per_Km";
private static final String DENSITY_MILE = "Density_People_Per_Mile";
private static final String MOST_POPULAS_CITY = "Most_Populas_city";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_list_view);
    listView = (ListView)findViewById(R.id.listView);
    Log.i("listView", "C1");

    ExternalDbOpenHelper dbOpenHelper = new ExternalDbOpenHelper(this, DB_NAME);
    database = dbOpenHelper.openDataBase();
    fillList();
    setUpList();

    Intent intent = getIntent();
    String data = intent.getExtras().getString("key");

    if(data.equals("1")){
        Toast.makeText(getApplicationContext(),"One",Toast.LENGTH_LONG).show();
    }
    if(data.equals("2")){
        Toast.makeText(getApplicationContext(),"Two",Toast.LENGTH_LONG).show();
    }

}


private void fillList() {
    //friends = new ArrayList<String>();  which was countryItems
    //String[] columns = {UID,CONTINENT,AREA_KM,POPULATION,DENSITY_KM,DENSITY_MILE,MOST_POPULAS_CITY};
    Cursor continentNameCorsor = database.query(TABLE_NAME,new String[]{CONTINENT},null, null, null, null,null);
    Log.i("listView", "C1");
    Cursor populationCorsor = database.query(TABLE_NAME,new String[]{POPULATION},null, null, null, null,null);
    Log.i("listView", "2");
    continentNameCorsor.moveToFirst();
    populationCorsor.moveToFirst();

    int continent_Index = continentNameCorsor.getColumnIndex(CONTINENT);
    int population_Index = populationCorsor.getColumnIndex(POPULATION);

    if(!continentNameCorsor.isAfterLast()) {
        do {
            String ContinentName = continentNameCorsor.getString(continent_Index);
            String PopulationNumber = populationCorsor.getString(population_Index);
            Log.i("listView", "Do loop");
            CountryItems details = new CountryItems(ContinentName,PopulationNumber);
            countryItems.add(details);

        } while (continentNameCorsor.moveToNext());
    }
    continentNameCorsor.close();
    populationCorsor.close();
}



private void setUpList() {
    /*setListAdapter(new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, friends));
    listView = getListView();*/
    countryAdepter = new CountryAdepter(this, countryItems);
    listView.setAdapter(countryAdepter);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {
            Toast.makeText(getApplicationContext(),
                    ((TextView) view).getText().toString(),
                    Toast.LENGTH_SHORT).show();
        }
    });
}


}

I'm using this one to populate data from my database, data is not populating from database to my listView, But I can't detect where is the problem.

Thanks in Advance

Aucun commentaire:

Enregistrer un commentaire