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
2
  • You can directly access image array value in listview adapter using title array position only you need to pass image array in listview adapter. Commented Jan 4, 2018 at 9:19
  • Do some code pls Commented Jan 4, 2018 at 9:23

1 Answer 1

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;
}
Sign up to request clarification or add additional context in comments.

6 Comments

For that you can use ArrayAdapter or BaseAdapter . There are plenty of tutorials for that . Have a look on This.
I need to know how to set "drawable" in TextDrawable[]
and then i need to set TextDrwable[] object in listview adapter
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 .
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" .
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.