Skip to content

Instantly share code, notes, and snippets.

Created June 14, 2017 02:28
Show Gist options
  • Save anonymous/b07c8c02357afb9f8d75b01848bea5b2 to your computer and use it in GitHub Desktop.
Save anonymous/b07c8c02357afb9f8d75b01848bea5b2 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
namespace GR3_chat_client
{
class customadaptadorburbuja : BaseAdapter
{
public List<Actividadchat.textomasid> textoyid = new List<Actividadchat.textomasid>();
Context context;
List<string> listillaa = new List<string>();
string nombre = "";
public customadaptadorburbuja(Context context, List<Actividadchat.textomasid> listilla, string nombree)
{
this.context = context;
textoyid = listilla;
nombre = nombree;
}
public override Java.Lang.Object GetItem(int position)
{
return textoyid[position].texto;
}
public override long GetItemId(int position)
{
return textoyid[position].id;
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
if (textoyid[position].texto.Split('¹')[0] == nombre)
{
var view = convertView;
customadaptadorburbujaViewHolder holder = null;
if (view != null)
{
holder = view.Tag as customadaptadorburbujaViewHolder;
}
if (holder == null)
{
holder = new customadaptadorburbujaViewHolder();
var inflater = context.GetSystemService(Context.LayoutInflaterService).JavaCast<LayoutInflater>();
//replace with your item and your holder items
//comment back in
view = inflater.Inflate(Resource.Layout.chatsitosderecha, parent, false);
holder.Title = view.FindViewById<TextView>(Resource.Id.textView1);
view.Tag = holder;
}
string texto = textoyid[position].texto.Split('¹')[1];
holder.Title.Text = "Yo:" + texto;
return view;
}
else {
var view = convertView;
customadaptadorburbujaViewHolder holder = null;
if (view != null)
{
holder = view.Tag as customadaptadorburbujaViewHolder;
}
if (holder == null)
{
holder = new customadaptadorburbujaViewHolder();
var inflater = context.GetSystemService(Context.LayoutInflaterService).JavaCast<LayoutInflater>();
//replace with your item and your holder items
//comment back in
view = inflater.Inflate(Resource.Layout.chatsitos, parent, false);
holder.Title = view.FindViewById<TextView>(Resource.Id.textView1);
view.Tag = holder;
}
string texto = textoyid[position].texto.Split('¹')[1];
holder.Title.Text = textoyid[position].texto.Split('¹')[0] + ":" + texto;
return view;
}
//fill in your items
}
//Fill in cound here, currently 0
public override int Count
{
get
{
return textoyid.Count;
}
}
}
class customadaptadorburbujaViewHolder : Java.Lang.Object
{
//Your adapter views to re-use
public TextView Title { get; set; }
}
}
@ajpinedam
Copy link

ajpinedam commented Jun 14, 2017

Unas cuantas sugerencias:

  • Utiliza el casing adecuado. Ejemplo: CustomAdaptadorBurbujaViewHolder, CustomAdaptadorBurbuja
  • Formatea mejor tu código para que sea mas legible. Demasiadas lineas en blanco y mal indentanción. Tanto VS como XS hacen esto por ti. Edit + Format
  • Crea una entidad para manejar los mensajes, en vez de usar un string que estas haciendo Splits. Ej:

`
class Mensaje
{
public string Usuario {get; set;}
public string Texto {get; set;}
public DateTime Fecha {get; set;}
}

`

  • Evita repetir una misma operación. Ej estas usando en varios lugares textoyid[position] mejor crea una variable con el valor de este ejemplo var item = textoyid[position]; así le evitas al procesador tener que buscar ese item en tu collection las 700 veces que lo usas. Lo mismo con el Split().
  • BaseAdapter tiene una implementación que usa Generics te permite "tipiar" (de type) tus items. En tu caso no es tan importante porque estas trabajando con un string pero si creas una entidad (la 3ra sugerencia) puedes hacer que tu BaseAdapter maneje tus items sin tener que estar haciendo casting a Java.Object. Ej BaseAdapter<Mensaje>.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment