cómo personalizar listview fila android

tengo una lista de nombres y quiero cambiar el color de las filas según la letra con la que empiece el nombre. Esto es lo que estoy usando para mostrar ListView:

main.xml

    

lisviewrows.xml

    

main.java

 public class Main extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); List words = populateList(); List l = removeDoubles(words); Collections.sort(l); ListView lv = (ListView)findViewById(R.id.listview); ArrayAdapter adapter = new ArrayAdapter(getApplicationContext(), R.layout.listviewrows, l); lv.setAdapter(adapter); lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView arg0, View arg1, int arg2, long arg3) { } }); } 

¿Hay algún enlace útil que me pueda proporcionar o algún ejemplo?

¡¡Gracias!!

Para hacerlo, deberá crear su propio adaptador personalizado. En el método getView (que devuelve la Vista para mostrar para cada elemento de la lista), aplique el color que desee al fondo de su TextView. Lo bueno de los Adaptadores personalizados es que puedes hacer absolutamente cualquier cosa y mostrar Vistas mucho más complicadas para tus elementos de la lista, porque ya no estás restringido a TextViews, puedes cambiar el diseño XML de tu elemento de lista a cualquier tipo de Vista / Diseño. .

Algo como esto:

MyAdapter.java

 public class MyAdapter extends BaseAdapter{ private LayoutInflater inflater; private ArrayList data; public MyAdapter(Context context, ArrayList data){ // Caches the LayoutInflater for quicker use this.inflater = LayoutInflater.from(context); // Sets the events data this.data= data; } public int getCount() { return this.data.size(); } public String getItem(int position) throws IndexOutOfBoundsException{ return this.data.get(position); } public long getItemId(int position) throws IndexOutOfBoundsException{ if(position < getCount() && position >= 0 ){ return position; } } public int getViewTypeCount(){ return 1; } public View getView(int position, View convertView, ViewGroup parent){ String myText = getItem(position); if(convertView == null){ // If the View is not cached // Inflates the Common View from XML file convertView = this.inflater.inflate(R.id.my_row_layout, null); } // Select your color and apply it to your textview int myColor; if(myText.substring(0, 1) == "a"){ myColor = Color.BLACK; }else{ .... } convertView.findViewById(R.id.myTextViewId).setBackground(myColor); // Of course you will need to set the same ID in your item list XML layout. return convertView; } } 

y luego en su actividad configure el adaptador de esta manera:

 public class Main extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); List words = populateList(); List l = removeDoubles(words); Collections.sort(l); ListView lv = (ListView)findViewById(R.id.listview); MyAdapter adapter = new MyAdapter(getApplicationContext(), l); lv.setAdapter(adapter); lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView arg0, View arg1, int arg2, long arg3) { } }); } 
  MainActivity: import java.util.ArrayList; import android.app.Activity; import android.content.Intent; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.os.Bundle; import android.view.View; import android.widget.Adapter; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ListView; public class ListViewImagesActivity extends Activity { ListView list; public Integer[] mgriArray = { R.drawable.angry0, R.drawable.neutral0, R.drawable.fblack0, R.drawable.misc0, R.drawable.notok0, R.drawable.panel011, R.drawable.pleasure0, R.drawable.rage0, R.drawable.sad0, R.drawable.stupidity0, R.drawable.sa0, R.drawable.tree0, R.drawable.victorious0,R.drawable.image_53, R.drawable.image_13 }; ArrayList image_details; Adapter griAdapter; public static SharedPreferences pref; static Editor editor; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); pref = getSharedPreferences("share", 0); editor = pref.edit(); list = (ListView) findViewById(R.id.listV_main); image_details = GetSearchResults3(); list.setAdapter(new ItemListBaseAdapter(this, image_details, mgriArray)); list.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView parent, View v, int position, long id) { Object o = list.getItemAtPosition(position); ItemDetails obj_itemDetails = (ItemDetails) o; String namee = obj_itemDetails.getName(); editor.putString("name", namee); // commit changes editor.commit(); // switch.................. switch (position) { case 0: Intent i1 = new Intent(ListViewImagesActivity.this, AndroidGridLayoutActivity.class); startActivity(i1); break; case 1: Intent i2 = new Intent(ListViewImagesActivity.this, AndroidGridLayoutActivity2.class); startActivity(i2); break; case 2: Intent i3 = new Intent(ListViewImagesActivity.this, AndroidGridLayoutActivity3.class); startActivity(i3); break; case 3: Intent i4 = new Intent(ListViewImagesActivity.this, AndroidGridLayoutActivity4.class); startActivity(i4); break; case 4: Intent i5 = new Intent(ListViewImagesActivity.this, AndroidGridLayoutActivity5.class); startActivity(i5); break; case 5: Intent i6 = new Intent(ListViewImagesActivity.this, AndroidGridLayoutActivity6.class); startActivity(i6); break; case 6: Intent i7 = new Intent(ListViewImagesActivity.this, AndroidGridLayoutActivity7.class); startActivity(i7); break; case 7: Intent i8 = new Intent(ListViewImagesActivity.this, AndroidGridLayoutActivity8.class); startActivity(i8); break; case 8: Intent i9 = new Intent(ListViewImagesActivity.this, AndroidGridLayoutActivity9.class); startActivity(i9); break; case 9: Intent i10 = new Intent(ListViewImagesActivity.this, AndroidGridLayoutActivity10.class); startActivity(i10); break; case 10: Intent i11 = new Intent(ListViewImagesActivity.this, AndroidGridLayoutActivity11.class); startActivity(i11); break; case 11: Intent i12 = new Intent(ListViewImagesActivity.this, AndroidGridLayoutActivity12.class); startActivity(i12); break; case 12: Intent i13 = new Intent(ListViewImagesActivity.this, AndroidGridLayoutActivity13.class); startActivity(i13); break; case 13: Intent i14 = new Intent(ListViewImagesActivity.this, AndroidGridLayoutActivity14.class); startActivity(i14); break; case 14: Intent i15 = new Intent(ListViewImagesActivity.this, AndroidGridLayoutActivity15.class); startActivity(i15); break; } // ending......... } }); } // ............................... public ArrayList GetSearchResults3() { // TODO Auto-generated method stub ArrayList results = new ArrayList(); ItemDetails item_details = new ItemDetails(); item_details.setName("ANGRY"); item_details.setImageNumber(1); results.add(item_details); item_details = new ItemDetails(); item_details.setName("NEUTRAL"); item_details.setImageNumber(2); results.add(item_details); item_details = new ItemDetails(); item_details.setName("FEMALE"); item_details.setImageNumber(3); results.add(item_details); item_details = new ItemDetails(); item_details.setName("MISC"); item_details.setImageNumber(4); results.add(item_details); item_details = new ItemDetails(); item_details.setName("NOTOK"); item_details.setImageNumber(5); results.add(item_details); item_details = new ItemDetails(); item_details.setName("PANEL"); item_details.setImageNumber(6); results.add(item_details); item_details = new ItemDetails(); item_details.setName("PLEASURE"); item_details.setImageNumber(7); results.add(item_details); item_details = new ItemDetails(); item_details.setName("RAGE"); item_details.setImageNumber(8); results.add(item_details); item_details = new ItemDetails(); item_details.setName("SAD"); item_details.setImageNumber(9); results.add(item_details); item_details = new ItemDetails(); item_details.setName("STUPIDITY"); item_details.setImageNumber(10); results.add(item_details); item_details = new ItemDetails(); item_details.setName("SURPRISED "); item_details.setImageNumber(11); results.add(item_details); item_details = new ItemDetails(); item_details.setName("TREES"); item_details.setImageNumber(12); results.add(item_details); item_details = new ItemDetails(); item_details.setName("VICTORIOUS"); item_details.setImageNumber(13); results.add(item_details); item_details = new ItemDetails(); item_details.setName("LUCKY BEAR"); item_details.setImageNumber(14); results.add(item_details); item_details = new ItemDetails(); item_details.setName("LUCKY CHARM"); item_details.setImageNumber(15); results.add(item_details); return results; } } Adapter Class... public class ItemListBaseAdapter extends BaseAdapter { private static ArrayList itemDetailsrrayList; private Integer[] imgid; public static ArrayList results; private LayoutInflater l_Inflater; public ItemListBaseAdapter(Context context, ArrayList results, Integer[] imgid) { itemDetailsrrayList = results; l_Inflater = LayoutInflater.from(context); this.imgid = imgid; } public int getCount() { return itemDetailsrrayList.size(); } public Object getItem(int position) { return itemDetailsrrayList.get(position); } public long getItemId(int position) { return position; } public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder; ImageView imageView = null; if (convertView == null) { convertView = l_Inflater.inflate(R.layout.category, null); holder = new ViewHolder(); holder.txt_itemName = (TextView) convertView.findViewById(R.id.name); holder.itemImage = (ImageView) convertView.findViewById(R.id.photo); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } holder.txt_itemName.setText(itemDetailsrrayList.get(position).getName()); holder.itemImage.setImageResource(imgid[itemDetailsrrayList.get( position).getImageNumber() - 1]); return convertView; } static class ViewHolder { TextView txt_itemName; ImageView itemImage; } } XML ListView here......      "        

Su problema es que está enviando como parámetro a la función setText , un número (int) y no un String. Por lo tanto, en lugar de:

 holder.text2.setText(words.indexOf(position)); 

Prueba esto:

 holder.text2.setText("" + words.indexOf(position)); 

¡Buena suerte!