0

I have an app in which i have String[] from which i have to find out each element from String[] first char and set it into another array and then set into listview. How do i do that

code:-

 public static final String[] titles = new String[]{"Strawberry",
        "Banana", "Orange", "Mixed"};

From above i find out char at poisition 0

   for (int i=0;i<titles.length;i++){
        String test = titles[i];
        Log.e(TAG,"Items****"+test);
        char firstChar = test.charAt(0);
        Log.e(TAG,"Char"+firstChar);

    }

Now i have to set these char in TextDrawable

static TextDrawable drawable = TextDrawable.builder() .buildRound("+1", Color.GREEN);

and Now i have to set these drawable into another array

TextDrawable[] image = //here i want to set
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Sandeep
  • 13
  • 4

1 Answers1

1

You can use a POJO class with desired properties.

 class TextModel{
    String text;
    TextDrawable drawable;
}

Just add it to a Collection .

ArrayList<TextModel> list=new ArrayList<>();
    for (int i=0;i<titles.length;i++){
        char firstChar = titles[i].charAt(0);
        TextModel textModel=new TextModel();
        textModel.text=test;
        textModel.drawable=TextDrawable.builder()
                .buildRound(firstChar, Color.GREEN);
        list.add(textModel);
    }

Now you can use list to set Adapter on ListView. You have to customize adapter obviously.

This is just one way of doing it . You can also build TextDrawable in Adapter and save them at globally(to Prevent form creating again).

Update

To set drawable in array you can do as.

TextDrawable[] images=new TextDrawable[titles.length];
    for (int i=0;i<titles.length;i++){
        char firstChar = titles[i].charAt(0);
        images[i]=TextDrawable.builder() .buildRound(firstChar, Color.GREEN);
    }

Now you can use images to set Adapter.

Update
To get initials from a name you can use method below . Modify it as per your need .

public static String getInitials(String name) {
    try {
        String initials = "";
        String[] str = name.split(" ");
        for (int i = 0; i < str.length; i++) {
            initials += str[i].charAt(0);
            if (initials.length() == 3) {
                break;
            }
        }
        return initials.toUpperCase();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return name;
}
ADM
  • 20,406
  • 11
  • 52
  • 83
  • For that you can use `ArrayAdapter` or `BaseAdapter` . There are plenty of tutorials for that . Have a look on [This](https://stackoverflow.com/questions/8166497/custom-adapter-for-list-view). – ADM Jan 04 '18 at 09:30
  • I need to know how to set "drawable" in TextDrawable[] – Sandeep Jan 04 '18 at 09:32
  • and then i need to set TextDrwable[] object in listview adapter – Sandeep Jan 04 '18 at 09:33
  • For this See the updated answer. And You can directly set TextDrwable[] to ListView with default implementation of `ArrayAdapter` you Have to create a custom adapter . – ADM Jan 04 '18 at 09:38
  • onw more thing if i have "sandeep" and then "Sandeep Kumar" then i have to find if sandeep then get only S and if got two name then get "SK" . – Sandeep Jan 04 '18 at 09:40
  • See the updated answer . Use it as per your need . I have break the loop on 3 digits most. Thx – ADM Jan 04 '18 at 09:46