我如何以编程方式在listView中替换颜色行?

时间:2016-09-11 14:28:37

标签: c# listview xamarin.android row

我需要在listView中添加一些替代颜色:

我的实际代码

public class Agenda
{    
    List<Schedule> tabelaAgenda = new List<Schedule>();
    public ListView _listView;
    public ArrayAdapter _adapter;

    protected override void OnCreate(Bundle bundle)
    {
        SetContentView(Resource.Layout.Pro_AgendaDia);        
        _listView = FindViewById<ListView>(Resource.Id.ListView_Horas);

        ArrayList listaArray =
            populaHorarios(dataInicial.Year, dataInicial.Month, dataInicial.Day);

        _listView.Adapter = _adapter;    
        _listView.ItemClick += _listView_ItemClick;    
    }

    private ArrayList populaHorarios(int ano, int mes, int dia)
    {
         DateTime testDate = new DateTime(ano, mes, dia, horaInicial, 00, 00);    

        tabelaAgenda = Schedule.carregaDadosAgenda_Exemplos();    
        ArrayList ArrayResultante = new ArrayList();

        for (var hora = 1; hora < hoursLeft; hora++)
        {
            var pHora = testDate.Hour;
            var pMinuto = testDate.Minute;
            var result = tabelaAgenda.Where(x => x.horaini == pHora && x.minutoini == pMinuto).FirstOrDefault();
            if (result != null)
            {    
                ArrayResultante.Add(testDate.ToShortTimeString() + " Scheduled");
                _adapter = new ArrayAdapter(this, Resource.Layout.Custom_hours_ocupied, ArrayResultante);
                // set background color GRAY
            }
            else
            {
                ArrayResultante.Add(testDate.ToShortTimeString());
                _adapter = new ArrayAdapter(this, Resource.Layout.Custom_hours_free, ArrayResultante);
                // set color other 
            }

            testDate = testDate.AddMinutes(30);

        } // for 

        return ArrayResultante;
    }
}

XML自定义

<?xml version="1.0" encoding="utf-8" ?>
<TextView 
  xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/textItensHoras1"          
    android:textSize="20dp"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@color/material_teal_100"
    android:textColor="@color/verde_trevo"
    android:gravity="center"
    android:paddingTop="10dp"
    android:paddingBottom="10dp"    
  />

1 个答案:

答案 0 :(得分:1)

我解决了! 我从arrayList更改为BaseAdapter。 基于此附带的印刷品。

感谢@Orkhan Alikhanov和@ user1230268:)

Screen 1 Screen 2