dimanche 1 mai 2016

Android getting a method value inside MainActivity from another activity

I try to keep this as simple as possible. I have a SQLite database that containt the following method that return all the names inside a table:

public ArrayList<String> getAllPlacesNames() {
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor result = db.rawQuery("select placeName FROM " + DataTableName,null);
        ArrayList<String> companyNames = new ArrayList<String>();
        while(result.moveToNext()) {
            String places = result.getString(result.getColumnIndex("placeName"));
            companyNames.add(places);
        }
        return companyNames;
    }

I have a MainActivity.java inside which I get and return the String array of names by calling the method getAllPlacesNames() from DataBaseHelper:

public ArrayList<String> getAllPlacesNamesMain() {

        return summerJobDB.getAllPlacesNames();
    }

Now my Problem. I'm trying to create a custom adapter for list view inorder to display the names. But I do not know how to get the return value of getAllPlacesNamesMain() from my adapter file StringArrayAdapter.java and convert it into an array So I can use it inside my constructor. Here is what I have so far:

public class StringArrayAdapter extends BaseAdapter {

    Context ctxt;
    LayoutInflater myInflator;
    DataBaseHelper summerJobDB;
    public ArrayList<String> getAllPlacesNamesMain() {return summerJobDB.getAllPlacesNames();}
    String[] places = new String [getAllPlacesNamesMain().size()];
    // convert places to Array
    places = getAllPlacesNamesMain().toArray(places);


    public StringArrayAdapter(String [] arr, Context c) {
        // here I need to set places to arr

    }

    @Override
    public int getCount() {
        return 0;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // create a cell to be displayed inside listview (a view)
        return null;
    }
}

Any help will be appreciated!

Aucun commentaire:

Enregistrer un commentaire