I can't find why my list doesn't print all of the items, it is printing only the first one, I checked my database and there is three items there, but this is what my log says:
10-03 14:46:07.116 833-833/br.com. I/System.out﹕ Q2: SELECT * FROM enderecos WHERE enderecos_id = 3
10-03 14:46:07.117 833-833/br.com. I/System.out﹕ END: Rua ujhuo
10-03 14:46:07.124 833-833/br.com. I/System.out﹕ Q2: SELECT * FROM telefones WHERE telefones_id = 5
10-03 14:46:07.125 833-833/br.com. I/System.out﹕ LIsta De TELEFone [br.com.database.models.TelefoneModel@422e8d58]
TelefoneModel@422e8d58 - this is the first one, the others are not showing.
Function:
public List<TelefoneModel> getTelefonesDoCliente(Integer clientes_id) throws SQLException {
String query1 = "SELECT * FROM " + tabelaCLIENTES_HAS_TELEFONES + " WHERE " + ClienteHasTelefoneModel.Coluna.CLIENTES_ID + " = " + clientes_id;
Cursor mCursor1 = bd.rawQuery(query1, null);
List<TelefoneModel> listaDeRegistros = new ArrayList<TelefoneModel>();
if (mCursor1.getCount() > 0) {
if (mCursor1.moveToFirst()) {
do {
Integer TelefoneId = mCursor1.getInt(mCursor1.getColumnIndexOrThrow("telefones_id"));
String query = "SELECT * FROM " + tabelaTELEFONES + " WHERE " + TelefoneModel.Coluna.TELEFONES_ID + " = " + TelefoneId;
System.out.println("Q2: " + query);
Cursor mCursor2 = bd.rawQuery(query, null);
try {
if (mCursor2.getCount() > 0) {
if (mCursor2.moveToFirst()) {
do {
TelefoneModel mTelefoneModel = new TelefoneModel();
mTelefoneModel.setTelefones_id(mCursor2.getInt(mCursor2.getColumnIndex(TelefoneModel.Coluna.TELEFONES_ID)));
mTelefoneModel.setId_rm(mCursor2.getInt(mCursor2.getColumnIndex(TelefoneModel.Coluna.ID_RM)));
mTelefoneModel.setNumero(mCursor2.getString(mCursor2.getColumnIndex(TelefoneModel.Coluna.NUMERO)));
mTelefoneModel.setTipo(mCursor2.getString(mCursor2.getColumnIndex(TelefoneModel.Coluna.TIPO)));
mTelefoneModel.set_criado(mCursor2.getString(mCursor2.getColumnIndex(TelefoneModel.Coluna._CRIADO)));
mTelefoneModel.set_modificado(mCursor2.getString(mCursor2.getColumnIndex(TelefoneModel.Coluna._MODIFICADO)));
mTelefoneModel.set_status(mCursor2.getString(mCursor2.getColumnIndex(TelefoneModel.Coluna._STATUS)));
listaDeRegistros.add(mTelefoneModel);
System.out.println("TELEFONES AQUI " + mTelefoneModel);
} while (mCursor2.moveToNext());
}
}
return listaDeRegistros;
} catch (Exception e) {
e.printStackTrace();
}
}while (mCursor1.moveToNext());
}
}
return null;
}
Adapter:
public class TelefoneViewAdapter extends BaseAdapter {
private ClienteDetalheActivity detalhe;
private List<TelefoneModel> tele;
private static LayoutInflater inflater=null;
public TelefoneViewAdapter(Context context, ClienteDetalheActivity clienteDetalhe, List<TelefoneModel> telefone) {
this.inflater = LayoutInflater.from( context );
this.tele = telefone;
detalhe = clienteDetalhe;
}
public int getCount() {
return tele.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
ViewHolder holder;
if(convertView == null){
vi = inflater.inflate(R.layout.fragment_cliente_detalhe, null);
holder=new ViewHolder();
holder.id = (TextView)vi.findViewById(R.id.enderecoId);
holder.numero = (TextView)vi.findViewById(R.id.telefone);
vi.setTag(holder);
}else{
holder = (ViewHolder)vi.getTag();
}
TelefoneModel item = new TelefoneModel();
item = tele.get(position);
holder.id.setText(String.valueOf(item.getTelefones_id()));
holder.numero.setText(item.getNumero());
return vi;
}
public class ViewHolder
{
TextView id;
TextView numero;
}
}
You are printing an object that contains multiple objects.
From what I've seen you are trying to print a list of phones. But remember that you can't just print the list.toString()
because that will only print one item. The list itself. If you want to print all the phones you have to iterate the list and print it's containers one by one. You can archive that by doing a simple for each
for(TelefoneModel phone : listaDeRegistros)
System.out.println("Phone " + phone);
Replace your System.out.println
for this line and you it'll iterate all the phones within your list and print them.
EDIT
Also remember that your code is only using the first element of mCursor1
. If this returns more than one object and you want to get the phones for that cursor too you have to do a do/while
for this cursor, just like you did for the phones. Otherwise your code looks like this -> "Get all address with id=3 -> Get all the phones with the id of the first item in addresses.