android how can i format a arrayList<String>

407 Views Asked by At

i have a problem with format arrayList.I have one parameter it have value

Licence_car:[[คย1453 กรุงเทพมหานคร], [รง2344 กรุงเทพมหานคร], [รน4679 กรุงเทพมหานคร]] (Data is a ThaiLanguage)

I use this parameter to set entry of list preference but it will show like this enter image description here

I want to delete character is "[" and "]" to make a variable like this Licence_car:[คย1453 กรุงเทพมหานคร, รง2344 กรุงเทพมหานคร, รน4679 กรุงเทพมหานคร] how can i do that?

This is my code set entry to list preference.

@SuppressWarnings("unchecked")
public void showCar(Context context,ArrayList<String> currentCars){
    SharedPreferences MYprefs = context.getSharedPreferences(PREFERENCES, PREFERENCE_MODE);

    if (null == currentCars) {
            currentCars = new ArrayList<String>();
        }
        try {
            currentCars = (ArrayList<String>) ObjectSerializer.deserialize(MYprefs.getString("car_licence_", ObjectSerializer.serialize(new ArrayList<String>())));
            //String[] car_list = currentCars.toCharArray;
            Log.d(TAG,"Licence_car:"+currentCars);  
            final CharSequence[] charSequenceCarEntry = currentCars.toArray(new CharSequence[currentCars.size()]);                    

            mCarDefault.setEntries(charSequenceCarEntry);
            mCarDefault.setEntryValues(charSequenceCarEntry); 
            mCarDelete.setEntries(charSequenceCarEntry);
            mCarDelete.setEntryValues(charSequenceCarEntry);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
}

I get a preference value in arrayList and format to CharSequence[] for set entry to list preference i think that i do format from this point but i don't know how can do it.

Thank for any answer and sorry for my English.

2

There are 2 best solutions below

1
On

Hello Developer, You can foramt your charsequence before storing into array list ,hete i am giving the sample code please use it so here it is-

CharSequence[] charSequenceCarEntry = new CharSequence[10];         
        int startindex=charSequenceCarEntry.toString().indexOf("[");
        int endindex=charSequenceCarEntry.toString().indexOf("]");
        CharSequence cs =charSequenceCarEntry.toString().substring(startindex, endindex);

so in your case use it like-

currentCars = (ArrayList<String>) ObjectSerializer.deserialize(MYprefs.getString("car_licence_", ObjectSerializer.serialize(new ArrayList<String>())));
final CharSequence[] charSequenceCarEntry = currentCars.toArray(new CharSequence[currentCars.size()]);                    
int startindex=charSequenceCarEntry.toString().indexOf("[");
int endindex=charSequenceCarEntry.toString().indexOf("]");
CharSequence cs =charSequenceCarEntry.toString().substring(startindex, endindex);
mCarDefault.setEntries(cs);
mCarDefault.setEntryValues(cs); 
mCarDelete.setEntries(cs);
mCarDelete.setEntryValues(cs);
0
On

I have solve this problem. I create input variable is type list<string> car_entry; to input a car_licence and output result is [คย1453 กรุงเทพมหานคร] so i will try to change type variable to String and the output is คย1453 กรุงเทพมหานคร as a result of charSequenceCarEntry is Licence_car:[คย1453 กรุงเทพมหานคร, รง2344 กรุงเทพมหานคร, รน4679 กรุงเทพมหานคร].Ok now It is done thank for any answer again. :)