Created
October 17, 2018 21:31
-
-
Save fernandolopes/d91a7b17c6883cf80e80cec53a23276b to your computer and use it in GitHub Desktop.
Adapter
This file contains hidden or 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 com.example.fernando.myapplication; | |
import android.content.Context; | |
import android.graphics.Color; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.BaseAdapter; | |
import android.widget.Button; | |
import android.widget.TextView; | |
import com.example.fernando.myapplication.models.Partida; | |
import com.example.fernando.myapplication.models.Valor; | |
import java.util.List; | |
/** | |
* Created by Fernando on 02/09/2018. | |
*/ | |
public class MyAdapter extends BaseAdapter implements View.OnClickListener { | |
private int[] btn_id = {R.id.btn0, R.id.btn1, R.id.btn2, R.id.btn3, | |
R.id.btn4, R.id.btn5, R.id.btn6, R.id.btn7, | |
R.id.btn8, R.id.btn9, R.id.btn10, R.id.btn11}; | |
private static LayoutInflater inflater = null; | |
private Context context; | |
private List<Partida> data; | |
public MyAdapter(Context context, List<Partida> data) { | |
this.context = context; | |
this.data = data; | |
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); | |
} | |
@Override | |
public int getCount() { | |
return data.size(); | |
} | |
@Override | |
public Object getItem(int i) { | |
return data.get(i); | |
} | |
@Override | |
public long getItemId(int i) { | |
return data.get(i).getId(); | |
} | |
@Override | |
public View getView(int er, View vi, ViewGroup viewGroup) { | |
Partida p = data.get(er); | |
vi = inflater.inflate(R.layout.activity_main, null); | |
TextView time1 = vi.findViewById(R.id.time1); | |
TextView time2 = vi.findViewById(R.id.time2); | |
TextView horario = vi.findViewById(R.id.horario); | |
time1.setText(p.getTime1()); | |
time2.setText(p.getTime2()); | |
horario.setText(p.getHorario()); | |
for(int i = 0; i < 12; i++){ | |
Button btn = (Button) vi.findViewById(btn_id[i]); | |
btn.setText(String.format("%.2f", p.getValores()[i].getValor())); | |
btn.setTextColor(Color.rgb(49, 50, 51)); | |
btn.setBackgroundColor(Color.rgb(207, 207, 207)); | |
btn.setOnClickListener(this); | |
btn.setTag(er); | |
if(p.getValores()[i].isChecked()){ | |
btn.setTextColor(Color.rgb(255, 255, 255)); | |
btn.setBackgroundColor(Color.rgb(3, 106, 150)); | |
} | |
} | |
return vi; | |
} | |
@Override | |
public void onClick(View v) { | |
Button btn = (Button) v; | |
int i = (int) v.getTag(); | |
Partida p = data.get(i); | |
for (Valor valor: p.getValores()) { | |
float val1 = Float.parseFloat(btn.getText().toString().replace(",", ".")); | |
float val2 = valor.getValor(); | |
valor.setChecked(false); | |
if(val1 == val2){ | |
valor.setChecked(true); | |
} | |
} | |
this.notifyDataSetChanged(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment