Tristan Roman
Tristan Roman

Reputation: 1

Easy Way To Add Huge Amount of predefined strings to Spinner widget?

is there a way to make inputting this text Alfa Romeo Aston Martin Audi Bentley Benz BMW Bugatti Cadillac Chevrolet Chrysler Citroen Corvette DAF Dacia Daewoo Daihatsu Datsun De Lorean Dino Dodge Farboud Ferrari Fiat Ford Honda Hummer Hyundai Jaguar Jeep KIA Koenigsegg Lada Lamborghini Lancia Land Rover Lexus Ligier ... All the car companies;

into the strings.xml easily/fast...for example i dont want to write <item></item> around each car make individually.

or do i?....

Any help much appreciated

Upvotes: 0

Views: 32

Answers (1)

Sarthak Gandhi
Sarthak Gandhi

Reputation: 2170

Well you can keep a text file in the raw folder with comma(,) separated values and read them in onCreate of any activity:

 private List<String> mFileItems;

and then

int textFile = R.raw.keywords;
mFileItems = Arrays.asList(Utils.getStringFromRaw(this, textFile).replace("\"", "").split("\\s*,\\s*"));

and

public class Utils {


public static String getStringFromRaw(Context mContext, int resource) throws IOException {
    Resources r = mContext.getResources();
    InputStream is = r.openRawResource(resource);
    String statesText = convertStreamToString(is);
    is.close();
    return statesText;
}

private static String convertStreamToString(InputStream is) throws IOException {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    int i = is.read();
    while (i != -1) {
        byteArrayOutputStream.write(i);
        i = is.read();
    }
    return byteArrayOutputStream.toString();
}
}

This will compile you a list of all the items in the list which are comma separated. Then you can set it in the spinner adapter.

Hope this helps.

Upvotes: 1

Related Questions