如何向ListView添加数组项?及TextDrawable数组绑定ListView实现
Hey there! Let's work through your two Android tasks one by one—getting your titles array into a ListView, and adding those TextDrawable icons based on each item's first character.
While you could use a basic ArrayAdapter to populate the ListView with your string array, we’ll jump straight to a custom adapter since we need to pair each text item with a TextDrawable. First, make sure you have a ListView in your layout (e.g., activity_main.xml):
<ListView android:id="@+id/list_view" android:layout_width="match_parent" android:layout_height="match_parent"/>
You already have the code to extract the first character—great start! Let’s expand that to create the TextDrawable[] array, then build a custom adapter to bind everything to the ListView.
Step 1: Populate the TextDrawable Array
First, initialize your TextDrawable[] array and fill it in your loop. Note that buildRound() requires a String, so we’ll convert the extracted char to a string. We’ll also add color variation (you can use fixed colors or random ones):
public static final String[] titles = new String[]{"Strawberry", "Banana", "Orange", "Mixed"}; private TextDrawable[] imageArray; // Fixed color palette matching your fruit items (adjust as needed) private static final int[] COLORS = {Color.RED, Color.YELLOW, Color.parseColor("#FFA500"), Color.GRAY}; // In your Activity/Fragment's initialization (e.g., onCreate()): imageArray = new TextDrawable[titles.length]; for (int i = 0; i < titles.length; i++) { String title = titles[i]; char firstChar = title.charAt(0); // Convert char to String for TextDrawable String charText = String.valueOf(firstChar); // Pick a color (cycle through the palette if you have more items than colors) int color = COLORS[i % COLORS.length]; // Build the circular TextDrawable imageArray[i] = TextDrawable.builder() .buildRound(charText, color); }
Step 2: Create a Custom List Item Layout
Make a layout file list_item.xml for each row in the ListView. It includes an ImageView for the TextDrawable and a TextView for the title:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="16dp" android:gravity="center_vertical" android:orientation="horizontal"> <ImageView android:id="@+id/icon" android:layout_width="40dp" android:layout_height="40dp" android:marginEnd="16dp"/> <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="18sp"/> </LinearLayout>
Step 3: Build a Custom BaseAdapter
Create an adapter class that extends BaseAdapter to bind your titles and TextDrawables to each list item. We’ll use the ViewHolder pattern to optimize performance:
private class CustomListAdapter extends BaseAdapter { private Context context; private String[] itemTitles; private TextDrawable[] itemDrawables; public CustomListAdapter(Context context, String[] titles, TextDrawable[] drawables) { this.context = context; this.itemTitles = titles; this.itemDrawables = drawables; } @Override public int getCount() { return itemTitles.length; } @Override public Object getItem(int position) { return itemTitles[position]; } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder; if (convertView == null) { // Inflate the list item layout convertView = LayoutInflater.from(context).inflate(R.layout.list_item, parent, false); holder = new ViewHolder(); holder.iconImageView = convertView.findViewById(R.id.icon); holder.titleTextView = convertView.findViewById(R.id.title); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } // Bind data to the views holder.titleTextView.setText(itemTitles[position]); holder.iconImageView.setImageDrawable(itemDrawables[position]); return convertView; } // ViewHolder to avoid repeated findViewById calls private class ViewHolder { ImageView iconImageView; TextView titleTextView; } }
Step 4: Attach the Adapter to Your ListView
Finally, in your Activity/Fragment, initialize the ListView and set the custom adapter:
ListView listView = findViewById(R.id.list_view); CustomListAdapter adapter = new CustomListAdapter(this, titles, imageArray); listView.setAdapter(adapter);
- For random colors instead of a fixed palette, replace the
COLORSarray with this helper method:
private int getRandomColor() { Random random = new Random(); return Color.rgb(random.nextInt(256), random.nextInt(256), random.nextInt(256)); }
Then use int color = getRandomColor(); in your loop.
- Don’t forget to add the TextDrawable dependency to your module-level
build.gradle:
implementation 'com.amulyakhare:com.amulyakhare.textdrawable:1.0.1'
内容的提问来源于stack exchange,提问作者Sandeep

