Android光标只显示数组的第一项

时间:2014-10-03 17:55:00

标签: android cursor android-arrayadapter android-cursor

我无法找到为什么我的列表没有打印所有项目,它只打印第一个,我检查了我的数据库,那里有三个项目,但这就是我的日志所说的:

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 - 这是第一个,其他人没有显示。

功能:

   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;
    }

适配器:

公共类TelefoneViewAdapter扩展了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;


}

}

1 个答案:

答案 0 :(得分:1)

您正在打印包含多个对象的对象。

据我所见,您正在尝试打印手机列表。但请记住,您不能只打印list.toString(),因为它只会打印一个项目。列表本身。如果你想打印所有手机,你必须迭代列表并逐个打印它的容器。您可以通过执行简单的for each

对其进行归档
for(TelefoneModel phone : listaDeRegistros)
    System.out.println("Phone " + phone);

替换此行的System.out.println,您将遍历列表中的所有手机并打印它们。

编辑

另请注意,您的代码仅使用mCursor1的第一个元素。如果这会返回多个对象并且您想要获取该光标的电话,则必须为此光标执行do/while,就像您对手机所做的那样。否则你的代码看起来像这样 - &gt; “获取id = 3的所有地址 - &gt;获取所有地址中第一项ID的手机。