Change color of Listview item in xamarin according to bank data

1

I need to change the color of the listview item according to the data that comes from the database, I have a visited field that is receiving a "*" would like to change the color of the person that has already been visited.

I'm bringing the search this way:

  players = new ArrayList();
        var path = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), abrir.Nome_banco);
        _connection = new SQLiteConnection(new SQLitePlatformAndroid(), path);
        sqldb = SQLiteDatabase.OpenDatabase(path, null, DatabaseOpenFlags.OpenReadwrite);

        sqldb = SQLiteDatabase.OpenOrCreateDatabase(path, null);

        Android.Database.ICursor sqldb_cursor = null;
        sqldb_query = "SELECT visitado,nome FROM pessoa where grupo_id ='"+grupo_id+"'order by nome asc";
        sqldb_cursor = sqldb.RawQuery(sqldb_query, null);
        if (!(sqldb_cursor != null))
        {


        }

        if (sqldb_cursor.MoveToFirst())
        {
            do
            {
                //formando aqui

                players.Add(sqldb_cursor.GetString(0) + sqldb_cursor.GetString(1));

            } while (sqldb_cursor.MoveToNext());

        }

Adapter code: adapter = new ArrayAdapter(this, Android.Resource.Layout.SimpleListItem1, players); lv.Adapter = adapter;

    
asked by anonymous 02.05.2017 / 15:59

1 answer

1

I think you should customize the Adapter and do something like this in GetView:

public override View GetView(int position, View convertView, ViewGroup parent)
{
    if(convertView == null)
    {
        convertView = LayoutInflater.From(this.context).Inflate('Your layout axml', parent, false);
    }

    convertView.SetBackgroundColor(Android.Graphics.Color.Aqua);

    return convertView;
}

Source: StackOverflow

It also has this form where you do not need to customize the adapter, rather overlap the method:

ListView listView = (ListView) this.findViewById(R.id.listView);
listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, MobileMuni.getBookmarkStore().getRecentLocations()) {
    @Override
    public View getView(int position, View convertView, ViewGroup parent) 

{
        TextView textView = (TextView) super.getView(position, convertView, parent);
    String currentLocation = RouteFinderBookmarksActivity.this.getResources().getString(R.string.Current_Location);
    int textColor = textView.getText().toString().equals(currentLocation) ? R.color.holo_blue : R.color.text_color_btn_holo_dark;
    textView.setTextColor(RouteFinderBookmarksActivity.this.getResources().getColor(textColor));

    return textView;
}

});

Source: Stackoverflow

    
02.05.2017 / 19:43