Change color of Listview item in Xamarin according to database data

Asked

Viewed 559 times

0

I need to change the color of the listview item according to the data coming from the database, I have a field visited that this receiving a "*" would like to change the color of the person who 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());

        }

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

  • The color change should be done on the Adapter.

  • That, I would have some example?

  • Put the Adapter code in the question.

  • ramaral put the Adapter code.

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

There’s also this shape where you don’t need to customize the Adapter but override 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

  • 1

    By Adapter I did not understand how I pass the values and make the comparison. You could tell me ?

  • Inside the Adapter you already have getItem() , create a rule based on it (e.g. if it is a value field and it is negative the color should be red).

  • 1

    Is there an example, I can check the value that is inside the Adapter with getItem? Why am I searching for two columns, but they concatenate.

  • This depends on how getItem is implemented (in case it is a custom Adapter). Search by Custom Adapter Android, you should find several cases of customized Adapters that can better clarify the operation.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.