Created
October 11, 2019 21:28
-
-
Save hesenger/b5ee8194cca1e11a48a8753deacb8692 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package br.com.loumar.vendas.utils; | |
import android.content.Context; | |
import android.util.Log; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.ArrayAdapter; | |
import android.widget.TextView; | |
import androidx.annotation.NonNull; | |
import androidx.annotation.Nullable; | |
import java.lang.reflect.Field; | |
import java.lang.reflect.Method; | |
import java.math.BigDecimal; | |
import java.util.Arrays; | |
import java.util.Date; | |
import java.util.List; | |
import br.com.loumar.vendas.BaseActivity; | |
import br.com.loumar.vendas.R; | |
public class DescricaoValorAdapter extends ArrayAdapter { | |
private final String[] props; | |
public DescricaoValorAdapter(@NonNull Context context, @NonNull List objects, String[] props) { | |
super(context, R.layout.descricaovalor_list, objects); | |
this.props = props; | |
} | |
@NonNull | |
@Override | |
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) { | |
View view = convertView; | |
if(view == null) | |
view = LayoutInflater.from(getContext()).inflate(R.layout.descricaovalor_list, parent, false); | |
Object obj = getItem(position); | |
TextView descricao = view.findViewById(R.id.descricao); | |
TextView valor = view.findViewById(R.id.valor); | |
TextView detalhe = view.findViewById(R.id.detalhe); | |
setText(descricao, obj, props[0]); | |
setText(valor, obj, props[1]); | |
setText(detalhe, obj, props[2]); | |
return view; | |
} | |
private void setText(TextView text, Object obj, String prop) { | |
Object val = null; | |
try { | |
if (prop == null || "".equals(prop)) | |
return; | |
boolean campo = false; | |
for (Field fld : obj.getClass().getDeclaredFields()) { | |
if (fld.getName().equals(prop)) { | |
campo = true; | |
break; | |
} | |
} | |
if (campo) { | |
Field fld = obj.getClass().getDeclaredField(prop); | |
fld.setAccessible(true); | |
val = fld.get(obj); | |
} else { | |
Method method = obj.getClass().getDeclaredMethod(prop); | |
method.setAccessible(true); | |
val = method.invoke(obj); | |
} | |
} catch (Exception e) { | |
Log.i("DVADAPTER", "setText", e); | |
} | |
if (val instanceof Date) | |
val = BaseActivity.DATE_FORMAT.format(val); | |
if (val instanceof BigDecimal) | |
val = BaseActivity.DECIMAL_FORMAT.format(val); | |
text.setText(val + ""); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment