Android 将包含对象的信息显示到ListView

Android 将包含对象的信息显示到ListView,android,Android,我试图显示一个对象的信息,具体地说是一个客户。我的意思是姓名、姓氏、电话号码、地址等等。。。。这是对我的数据库进行查询的结果 我想在ListView上显示它,所以如果我的活动继承ListActivity,效果会更好,因为ListView在默认情况下是自包含的 我做下一个:我有一个名为Customer的类,它通过只返回一条记录(Customer)的查询(游标)保存所有信息,我不知道如何传递这个对象,我找不到最合适的适配器 我可以这样做吗?或者我必须将这个对象转换成一个包含所有信息的ArrayLis

我试图显示一个对象的信息,具体地说是一个客户。我的意思是姓名、姓氏、电话号码、地址等等。。。。这是对我的数据库进行查询的结果

我想在ListView上显示它,所以如果我的活动继承ListActivity,效果会更好,因为ListView在默认情况下是自包含的

我做下一个:我有一个名为Customer的类,它通过只返回一条记录(Customer)的查询(游标)保存所有信息,我不知道如何传递这个对象,我找不到最合适的适配器

我可以这样做吗?或者我必须将这个对象转换成一个包含所有信息的ArrayList,事实上,我的适配器使用ArrayAdapter

有人知道怎么做吗


谢谢。

一旦您的光标被查询,您应该执行以下操作

String[] from = new String[]{"FirstName"};
int[] to = new int[]{R.id.row};

SimpleCursorAdapter sca = new SimpleCursorAdapter(this, R.layout.row_item, cur, from, to);
setListAdapter(sca);
在这里查看文档以了解更多信息。 如果需要对视图执行更复杂的操作,则应实现自己的自定义光标适配器:

public class ExampleCursorAdapter extends CursorAdapter {
    public ExampleCursorAdapter(Context context, Cursor c) {
        super(context, c);
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        TextView summary = (TextView)view.findViewById(R.id.summary);
        summary.setText(cursor.getString(
                cursor.getColumnIndex(ExampleDB.KEY_EXAMPLE_SUMMARY)));
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View v = inflater.inflate(R.layout.item, parent, false);
        bindView(v, context, cursor);
        return v;
    }
}

最后,我可以找到解决办法。我要告诉你我是怎么做到的

我想通过以下方式显示客户的详细信息:

  • 第1行->名称:名称\u值
  • 第2行->姓氏:姓氏\u值
  • 第3行->电话:电话值
  • N.N行->N\u字段:N\u值

    描述要显示的值的字段,我指的是“Name:”、“Last Name:”、“Phone:”、。。。 我将所有这些值放在一个字符串数组中。我用一个字符串[]捕捉到了这一点

    值,在正确字段旁边显示正确的值。我把这些都放进了ArrayList里 接下来只需编写适当的适配器:

    public class ExampleCursorAdapter extends CursorAdapter {
        public ExampleCursorAdapter(Context context, Cursor c) {
            super(context, c);
        }
    
        @Override
        public void bindView(View view, Context context, Cursor cursor) {
            TextView summary = (TextView)view.findViewById(R.id.summary);
            summary.setText(cursor.getString(
                    cursor.getColumnIndex(ExampleDB.KEY_EXAMPLE_SUMMARY)));
        }
    
        @Override
        public View newView(Context context, Cursor cursor, ViewGroup parent) {
            LayoutInflater inflater = LayoutInflater.from(context);
            View v = inflater.inflate(R.layout.item, parent, false);
            bindView(v, context, cursor);
            return v;
        }
    }
    
    默认情况下,此活动继承ListActivity和ListView

    SimpleCursorAdapter不正确,因为我们必须将游标作为此适配器的参数传递,但我的游标只返回一个结果,只返回一条记录

    而游标适配器也因同样的原因被拒绝。下一步我能做什么?阵列适配器:

    公共类DetallClientAdapter扩展了ArrayAdapter
    {
    私人字符串[]campos_客户;
    私人ArrayList detalles_客户;
    公共数据列表客户端适配器(上下文上下文、int布局、ArrayList数据列表)
    {
    超级(上下文、布局、细节);
    //mInflater=LayoutInflater.from(contexto);
    //LOS VALORES DE LOS CAMPOS:
    detalles_cliente=detalles;
    //德塔莱斯营地:诺姆布雷、阿佩利多斯等。。。
    android.content.res.Resources res=getResources();
    campos_cliente=res.getStringArray(R.array.array_detalle_cliente);
    }
    @凌驾
    公共视图getView(int位置、视图转换视图、视图组父视图)
    {
    //TODO自动生成的方法存根
    //返回super.getView(position、convertView、parent);
    LayoutInflater充气机=getLayoutInflater();
    视图fila=充气机。充气(R.layout.detalles\u客户,父,假);
    TextView campo=(TextView)fila.findViewById(R.id.detalles_campo);
    TextView valor=(TextView)fila.findViewById(R.id.detalles_valor);
    campo.setText(campos_客户[职位]);
    valor.setText(detalles_cliente.get(位置));
    返回fila;
    }
    }
    
    现在我要编写一个更高效的适配器,使用ViewHolder。你对此有什么看法


    谢谢。

    我的第一个想法是将返回光标的记录信息存储在一个对象中,即对象客户。我想我应该在包含所有信息的ArrayList上转换它。ArrayList您可以使用它…………我想显示一个客户的信息,它只是一个客户。我的意思是,在屏幕上,name:name\u customer,lastname:lastname\u customer。。。。等在ListView的每一行上执行此操作。但我不知道如何执行此操作。。。对不起,我是新手,我的答案是否定的。你的意见是什么?谢谢你的回答。这可能是我的解决方案。但我想问一个问题。我想用这种方法来显示屏幕。。。。1row:Firstname:pepe-2row:LastName:martínez-3排电话:600000555。。。并以这种方式显示数据。。。。你建议我做些什么?