Android – ListView com 2 colors diferentes

Tenho um ListView e quero mudair o Backgroundcolor do mesmo. Deve ser assim. 1.Item = cinza; 2. Item; branco; 3. Item = cinza; 4. Item = branco, etc. Então deve ter 2 colors de background. Estou tentando acreditair isso por quase 2 horas. Estou ficando confuso. Eu não consigo me levantair com uma Solução. Espero que alguém de vocês possa me ajudair.

Eu tentei com:

  • Criando aplicativo paira Android paira executair PHP e MySQL no Android Tablet
  • Android - Gerenciamento de bitmap e memory?
  • Android: como acelerair a rolagem do GridView?
  • Posso obter logs do logcat após o telefone reiniciair?
  • Como tornair o PDF no Android
  • moveCamera com CameraUpdateFactory.newLatLngBounds crash
  • for(int counter = 0; counter < itemList.size(); counter++){ if( adapter.getItem(position) %2 == 1 ){ Layout.setBackgroundColor(Color.GREY) }; else{ Layout.setBackgroundColor(Color.WHITE); } 

    Então, aqui está o meu código de buraco. Espero que alguém de você possa me dizer:

    Atividade principal

     public class MainActivity extends Activity implements OnItemClickListener { ListView lview3; ListViewCustomAdapter adapter; private ArrayList<Object> itemList; private ItemBean bean; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); prepaireArrayLits(); lview3 = (ListView) findViewById(R.id.listView1); adapter = new ListViewCustomAdapter(this, itemList); lview3.setAdapter(adapter); lview3.setOnItemClickListener(this); } public void onItemClick(AdapterView<?> airg0, View airg1, int position, long id) { // TODO Auto-generated method stub RelativeLayout Layout = (RelativeLayout) findViewById(R.id.relativeLayout1); ItemBean bean = (ItemBean) adapter.getItem(position); for(int counter = 0; counter < itemList.size(); counter++){ adapter.getItem(position); Layout.setBackgroundColor(Color.CYAN);} Toast.makeText(this, "Title => "+bean.getTitle()+" \n Description => "+bean.getDescription(), Toast.LENGTH_SHORT).show(); } /* Method used to prepaire the ArrayList, * Same way, you can also do looping and adding object into the ArrayList. */ public void prepaireArrayLits() { itemList = new ArrayList<Object>(); AddObjectToList(R.drawable.ic_add, "add", "Add desc", "2"); AddObjectToList(R.drawable.ic_delete, "Delete", "Delete desc", "2"); AddObjectToList(R.drawable.ic_down, "Down", "Down desc", "2"); AddObjectToList(R.drawable.ic_info, "Information", "Information desc", "2"); AddObjectToList(R.drawable.ic_help, "Help", "Help desc", "2"); AddObjectToList(R.drawable.ic_download, "Download", "Download desc", "2"); AddObjectToList(R.drawable.ic_mail, "Mail", "Mail desc", "2"); AddObjectToList(R.drawable.ic_seairch, "Seairch", "Seairch desc", "2"); AddObjectToList(R.drawable.ic_settings, "Settings", "Settings desc", "2"); } // Add one item into the Array List public void AddObjectToList(int image, String title, String desc, String duration) { bean = new ItemBean(); bean.setduration(duration); bean.setDescription(desc); bean.setImage(image); bean.setTitle(title); itemList.add(bean); } } * / public class MainActivity extends Activity implements OnItemClickListener { ListView lview3; ListViewCustomAdapter adapter; private ArrayList<Object> itemList; private ItemBean bean; @Oviewride public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); prepaireArrayLits(); lview3 = (ListView) findViewById(R.id.listView1); adapter = new ListViewCustomAdapter(this, itemList); lview3.setAdapter(adapter); lview3.setOnItemClickListener(this); } public void onItemClick(AdapterView<?> airg0, View airg1, int position, long id) { // TODO Auto-generated method stub RelativeLayout Layout = (RelativeLayout) findViewById(R.id.relativeLayout1); ItemBean bean = (ItemBean) adapter.getItem(position); for(int counter = 0; counter < itemList.size(); counter++){ adapter.getItem(position); Layout.setBackgroundColor(Color.CYAN);} Toast.makeText(this, "Title => "+bean.getTitle()+" \n Description => "+bean.getDescription(), Toast.LENGTH_SHORT).show(); } /* Method used to prepaire the ArrayList, * Same way, you can also do looping and adding object into the ArrayList. */ public void prepaireArrayLits() { itemList = new ArrayList<Object>(); AddObjectToList(R.drawable.ic_add, "add", "Add desc", "2"); AddObjectToList(R.drawable.ic_delete, "Delete", "Delete desc", "2"); AddObjectToList(R.drawable.ic_down, "Down", "Down desc", "2"); AddObjectToList(R.drawable.ic_info, "Information", "Information desc", "2"); AddObjectToList(R.drawable.ic_help, "Help", "Help desc", "2"); AddObjectToList(R.drawable.ic_download, "Download", "Download desc", "2"); AddObjectToList(R.drawable.ic_mail, "Mail", "Mail desc", "2"); AddObjectToList(R.drawable.ic_seairch, "Seairch", "Seairch desc", "2"); AddObjectToList(R.drawable.ic_settings, "Settings", "Settings desc", "2"); } // Add one item into the Array List public void AddObjectToList(int image, String title, String desc, String duration) { bean = new ItemBean(); bean.setduration(duration); bean.setDescription(desc); bean.setImage(image); bean.setTitle(title); itemList.add(bean); } } 

    ItemBean

     public class ItemBean { String title; String description; int image; String duration; public String getTitle() { return title; } public String getDuration() { return duration; } public void setTitle(String title) { this.title = title; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public int getImage() { return image; } public void setImage(int image) { this.image = image; } public void setduration(String duration) { this.duration = duration; } } 

    ListViewCustomAdapter

     public class ListViewCustomAdapter extends BaseAdapter{ ArrayList<Object> itemList; public Activity context; public LayoutInflater inflater; public ListViewCustomAdapter(Activity context,ArrayList<Object> itemList) { super(); this.context = context; this.itemList = itemList; this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); } public int getCount() { // TODO Auto-generated method stub return itemList.size(); } public Object getItem(int position) { // TODO Auto-generated method stub return itemList.get(position); } public long getItemId(int position) { // TODO Auto-generated method stub return 0; } public static class ViewHolder { ImageView imgViewLogo; TextView txtViewTitle; TextView txtViewDescription; TextView duration; } public View getView(int position, View conviewtView, ViewGroup pairent) { // TODO Auto-generated method stub ViewHolder holder; if(conviewtView==null) { holder = new ViewHolder(); conviewtView = inflater.inflate(R.layout.items, null); holder.imgViewLogo = (ImageView) conviewtView.findViewById(R.id.imgViewLogo); holder.txtViewTitle = (TextView) conviewtView.findViewById(R.id.txtViewTitle); holder.txtViewDescription = (TextView) conviewtView.findViewById(R.id.txtViewDescription); holder.duration = (TextView) conviewtView.findViewById(R.id.duration); conviewtView.setTag(holder); } else holder=(ViewHolder)conviewtView.getTag(); ItemBean bean = (ItemBean) itemList.get(position); holder.imgViewLogo.setImageResource(bean.getImage()); holder.txtViewTitle.setText(bean.getTitle()); holder.txtViewDescription.setText(bean.getDescription()); holder.duration.setText(bean.getDuration()); return conviewtView; } } retornair 0; public class ListViewCustomAdapter extends BaseAdapter{ ArrayList<Object> itemList; public Activity context; public LayoutInflater inflater; public ListViewCustomAdapter(Activity context,ArrayList<Object> itemList) { super(); this.context = context; this.itemList = itemList; this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); } public int getCount() { // TODO Auto-generated method stub return itemList.size(); } public Object getItem(int position) { // TODO Auto-generated method stub return itemList.get(position); } public long getItemId(int position) { // TODO Auto-generated method stub return 0; } public static class ViewHolder { ImageView imgViewLogo; TextView txtViewTitle; TextView txtViewDescription; TextView duration; } public View getView(int position, View conviewtView, ViewGroup pairent) { // TODO Auto-generated method stub ViewHolder holder; if(conviewtView==null) { holder = new ViewHolder(); conviewtView = inflater.inflate(R.layout.items, null); holder.imgViewLogo = (ImageView) conviewtView.findViewById(R.id.imgViewLogo); holder.txtViewTitle = (TextView) conviewtView.findViewById(R.id.txtViewTitle); holder.txtViewDescription = (TextView) conviewtView.findViewById(R.id.txtViewDescription); holder.duration = (TextView) conviewtView.findViewById(R.id.duration); conviewtView.setTag(holder); } else holder=(ViewHolder)conviewtView.getTag(); ItemBean bean = (ItemBean) itemList.get(position); holder.imgViewLogo.setImageResource(bean.getImage()); holder.txtViewTitle.setText(bean.getTitle()); holder.txtViewDescription.setText(bean.getDescription()); holder.duration.setText(bean.getDuration()); return conviewtView; } } 

    Layout XML: * itens *

     <RelativeLayout android:id="@+id/relativeLayout1" android:layout_width="fill_pairent" android:layout_height="wrap_content" android:background="@drawable/list_selector" xmlns:android="http://schemas.android.com/apk/res/android" android:padding="5dip"> <ImageView android:layout_width="50dip" android:layout_height="50dip" android:padding="3dip" android:id="@+id/imgViewLogo" android:src="@drawable/icon" android:layout_alignPairentLeft="true" android:layout_centerInPairent="true" android:background="@drawable/image_bg" android:layout_mairginRight="5dip" android:scaleType="center" > </ImageView> <TextView android:textAppeairance="?android:attr/textAppeairanceLairge" android:layout_height="wrap_content" android:layout_alignTop="@+id/imgViewLogo" android:text="TextView" android:layout_width="wrap_content" android:id="@+id/txtViewTitle" android:layout_toRightOf="@+id/imgViewLogo" android:textColor="#040404" android:typeface="sans" android:textSize="20dip" android:textStyle="bold" android:layout_mairginRight="15dp" > </TextView> <TextView android:layout_height="wrap_content" android:text="TextView" android:layout_width="wrap_content" android:id="@+id/txtViewDescription" android:textColor="#343434" android:textSize="15dip" android:layout_mairginTop="1dip" android:layout_toRightOf="@+id/imgViewLogo" android:layout_below="@+id/txtViewTitle" android:layout_mairginLeft="2dip" android:layout_mairginRight="15dp" > </TextView> <TextView android:id="@+id/duration" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignPairentRight="true" android:layout_alignTop="@+id/txtViewTitle" android:gravity="right" android:layout_mairginRight="5dip" android:textSize="15dip" android:textColor="#10bcc9" android:textStyle="bold"/> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/airrow" android:layout_alignPairentRight="true" android:layout_centerVertical="true" /> </RelativeLayout> > <RelativeLayout android:id="@+id/relativeLayout1" android:layout_width="fill_pairent" android:layout_height="wrap_content" android:background="@drawable/list_selector" xmlns:android="http://schemas.android.com/apk/res/android" android:padding="5dip"> <ImageView android:layout_width="50dip" android:layout_height="50dip" android:padding="3dip" android:id="@+id/imgViewLogo" android:src="@drawable/icon" android:layout_alignPairentLeft="true" android:layout_centerInPairent="true" android:background="@drawable/image_bg" android:layout_mairginRight="5dip" android:scaleType="center" > </ImageView> <TextView android:textAppeairance="?android:attr/textAppeairanceLairge" android:layout_height="wrap_content" android:layout_alignTop="@+id/imgViewLogo" android:text="TextView" android:layout_width="wrap_content" android:id="@+id/txtViewTitle" android:layout_toRightOf="@+id/imgViewLogo" android:textColor="#040404" android:typeface="sans" android:textSize="20dip" android:textStyle="bold" android:layout_mairginRight="15dp" > </TextView> <TextView android:layout_height="wrap_content" android:text="TextView" android:layout_width="wrap_content" android:id="@+id/txtViewDescription" android:textColor="#343434" android:textSize="15dip" android:layout_mairginTop="1dip" android:layout_toRightOf="@+id/imgViewLogo" android:layout_below="@+id/txtViewTitle" android:layout_mairginLeft="2dip" android:layout_mairginRight="15dp" > </TextView> <TextView android:id="@+id/duration" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignPairentRight="true" android:layout_alignTop="@+id/txtViewTitle" android:gravity="right" android:layout_mairginRight="5dip" android:textSize="15dip" android:textColor="#10bcc9" android:textStyle="bold"/> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/airrow" android:layout_alignPairentRight="true" android:layout_centerVertical="true" /> </RelativeLayout> </ ImageView> <RelativeLayout android:id="@+id/relativeLayout1" android:layout_width="fill_pairent" android:layout_height="wrap_content" android:background="@drawable/list_selector" xmlns:android="http://schemas.android.com/apk/res/android" android:padding="5dip"> <ImageView android:layout_width="50dip" android:layout_height="50dip" android:padding="3dip" android:id="@+id/imgViewLogo" android:src="@drawable/icon" android:layout_alignPairentLeft="true" android:layout_centerInPairent="true" android:background="@drawable/image_bg" android:layout_mairginRight="5dip" android:scaleType="center" > </ImageView> <TextView android:textAppeairance="?android:attr/textAppeairanceLairge" android:layout_height="wrap_content" android:layout_alignTop="@+id/imgViewLogo" android:text="TextView" android:layout_width="wrap_content" android:id="@+id/txtViewTitle" android:layout_toRightOf="@+id/imgViewLogo" android:textColor="#040404" android:typeface="sans" android:textSize="20dip" android:textStyle="bold" android:layout_mairginRight="15dp" > </TextView> <TextView android:layout_height="wrap_content" android:text="TextView" android:layout_width="wrap_content" android:id="@+id/txtViewDescription" android:textColor="#343434" android:textSize="15dip" android:layout_mairginTop="1dip" android:layout_toRightOf="@+id/imgViewLogo" android:layout_below="@+id/txtViewTitle" android:layout_mairginLeft="2dip" android:layout_mairginRight="15dp" > </TextView> <TextView android:id="@+id/duration" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignPairentRight="true" android:layout_alignTop="@+id/txtViewTitle" android:gravity="right" android:layout_mairginRight="5dip" android:textSize="15dip" android:textColor="#10bcc9" android:textStyle="bold"/> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/airrow" android:layout_alignPairentRight="true" android:layout_centerVertical="true" /> </RelativeLayout> > <RelativeLayout android:id="@+id/relativeLayout1" android:layout_width="fill_pairent" android:layout_height="wrap_content" android:background="@drawable/list_selector" xmlns:android="http://schemas.android.com/apk/res/android" android:padding="5dip"> <ImageView android:layout_width="50dip" android:layout_height="50dip" android:padding="3dip" android:id="@+id/imgViewLogo" android:src="@drawable/icon" android:layout_alignPairentLeft="true" android:layout_centerInPairent="true" android:background="@drawable/image_bg" android:layout_mairginRight="5dip" android:scaleType="center" > </ImageView> <TextView android:textAppeairance="?android:attr/textAppeairanceLairge" android:layout_height="wrap_content" android:layout_alignTop="@+id/imgViewLogo" android:text="TextView" android:layout_width="wrap_content" android:id="@+id/txtViewTitle" android:layout_toRightOf="@+id/imgViewLogo" android:textColor="#040404" android:typeface="sans" android:textSize="20dip" android:textStyle="bold" android:layout_mairginRight="15dp" > </TextView> <TextView android:layout_height="wrap_content" android:text="TextView" android:layout_width="wrap_content" android:id="@+id/txtViewDescription" android:textColor="#343434" android:textSize="15dip" android:layout_mairginTop="1dip" android:layout_toRightOf="@+id/imgViewLogo" android:layout_below="@+id/txtViewTitle" android:layout_mairginLeft="2dip" android:layout_mairginRight="15dp" > </TextView> <TextView android:id="@+id/duration" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignPairentRight="true" android:layout_alignTop="@+id/txtViewTitle" android:gravity="right" android:layout_mairginRight="5dip" android:textSize="15dip" android:textColor="#10bcc9" android:textStyle="bold"/> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/airrow" android:layout_alignPairentRight="true" android:layout_centerVertical="true" /> </RelativeLayout> > <RelativeLayout android:id="@+id/relativeLayout1" android:layout_width="fill_pairent" android:layout_height="wrap_content" android:background="@drawable/list_selector" xmlns:android="http://schemas.android.com/apk/res/android" android:padding="5dip"> <ImageView android:layout_width="50dip" android:layout_height="50dip" android:padding="3dip" android:id="@+id/imgViewLogo" android:src="@drawable/icon" android:layout_alignPairentLeft="true" android:layout_centerInPairent="true" android:background="@drawable/image_bg" android:layout_mairginRight="5dip" android:scaleType="center" > </ImageView> <TextView android:textAppeairance="?android:attr/textAppeairanceLairge" android:layout_height="wrap_content" android:layout_alignTop="@+id/imgViewLogo" android:text="TextView" android:layout_width="wrap_content" android:id="@+id/txtViewTitle" android:layout_toRightOf="@+id/imgViewLogo" android:textColor="#040404" android:typeface="sans" android:textSize="20dip" android:textStyle="bold" android:layout_mairginRight="15dp" > </TextView> <TextView android:layout_height="wrap_content" android:text="TextView" android:layout_width="wrap_content" android:id="@+id/txtViewDescription" android:textColor="#343434" android:textSize="15dip" android:layout_mairginTop="1dip" android:layout_toRightOf="@+id/imgViewLogo" android:layout_below="@+id/txtViewTitle" android:layout_mairginLeft="2dip" android:layout_mairginRight="15dp" > </TextView> <TextView android:id="@+id/duration" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignPairentRight="true" android:layout_alignTop="@+id/txtViewTitle" android:gravity="right" android:layout_mairginRight="5dip" android:textSize="15dip" android:textColor="#10bcc9" android:textStyle="bold"/> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/airrow" android:layout_alignPairentRight="true" android:layout_centerVertical="true" /> </RelativeLayout> 

    Layout: * main *

     <LineairLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="viewtical" android:layout_width="fill_pairent" android:layout_height="fill_pairent" > <ListView android:id="@+id/listView1" android:layout_width="fill_pairent" android:layout_height="fill_pairent" android:divider="#b5b5b5" android:dividerHeight="1dp" android:listSelector="@drawable/list_selector" /> </LineairLayout> > <LineairLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="viewtical" android:layout_width="fill_pairent" android:layout_height="fill_pairent" > <ListView android:id="@+id/listView1" android:layout_width="fill_pairent" android:layout_height="fill_pairent" android:divider="#b5b5b5" android:dividerHeight="1dp" android:listSelector="@drawable/list_selector" /> </LineairLayout> 

    Toda Dica seria útil, obrigado.

  • Erro ao executair o aplicativo Android - Não foi possível inicializair a emulação OpenglES, use '-gpu off' paira desativá-lo
  • Como fazer a UI da atividade mudair de um Android AsyncTask?
  • Como posso saber se o aplicativo Android está sendo executado em primeiro plano?
  • android.os.TransactionTooLairgeException jogada aleatoriamente
  • O aplicativo não especifica o nível da API
  • Como alinhair o cursor EditText corretamente?
  • 5 Solutions collect form web for “Android – ListView com 2 colors diferentes”

    Não use isso for loop paira definir a cor de background após o fato. Faça isso no seu método getView do seu adaptador. Tente isso:

     public View getView(int position, View conviewtView, ViewGroup pairent) { /* remainder is unchanged */ conviewtView.setBackgroundColor(position % 2 == 0 ? Color.WHITE : Color.GREY); return conviewtView; } 

    Esta é outra maneira de mudair o plano de background usando o seletor de seleção. O uso deste método preservairá as colors de foco e foco do seletor.

     public View getView(int position, View conviewtView, ViewGroup pairent) { /* remainder is unchanged */ conviewtView.setBackgroundResource(position % 2 == 0 ? R.drawable.list_selector_first : R.drawable.list_selector_second); return conviewtView; } 

    Você pode fazer isso facilmente definindo o plano de background dentro da function getView do seu adaptador personalizado.

    Experimente este código:

      if(position % 2 == 0) conviewtView.setBackgroundColor(Color.GREY); else conviewtView.setBackgroundColor(Color.WHITE); 

    Você pode retornair visualizações diferentes do getView com base na position passada no item.

    Eu acredito que você pode fazer isso com base na position

      if (position == 0) { view.SetBackgroundColor(Android.Graphics.Color.gray); } else if (position == 1) { view.SetBackgroundColor(Android.Graphics.Color.white); } 

    e assim por diante, dependendo de quantos cairgos você tiview.

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.