如何将数组放入listview

时间:2014-08-05 07:24:32

标签: java android arrays listview

好的,所以我成功地从我的数据库中获取了我的名字,现在我已将它们存储在数组结果[]中,并且在代码的末尾我写了返回结果;现在我的问题是,如何从我的结果[]中获取名称以显示在列表视图中?注意:我不想在与listview one相同的类上连接mysql数据库。

我将如何做到这一点?

我也想知道如何制作带有名称和侧面图片的自定义列表视图。另外如果你能告诉我如何将图片添加到listviews中,那就太棒了:)我将使用bmp,从数据库中将它们作为byte []重新转换,然后将它们转换为bmp。

注意:java和android开发相当新颖

1 个答案:

答案 0 :(得分:0)

你可以在互联网上找到很多例子。 一个简单的例子是:

ListView listView = (ListView) findViewById(R.id.listOne);

    String[] name={"HELLO","THIS","IS","NABIN"};
    String[] phone={"12","24","36","48"};

    ArrayList<HashMap<String,String>> obj= new ArrayList<HashMap<String,String>>();

    for(int i=0;i<name.length;i++){
        HashMap<String,String> toFill = new HashMap<String,String>();
        toFill.put("name", name[i]);
        toFill.put("phone", phone[i]);
        obj.add(toFill);
    }

    //to define adapter

    ListAdapter adapter = new SimpleAdapter(MainActivity.this, obj, R.layout.contact, new String[] {"name", "phone"},new int[] {R.id.etName,R.id.etPhone});
    listView.setAdapter(adapter);

<强>被修改

要显示的图像:(考虑到您的图像位于可绘制文件夹中)

您必须制作自定义适配器

对于自定义适配器的imageView,请执行以下操作:

String imageName = ......// get text from your array which would be the name of image.
int resID = getResources().getIdentifier(imageName , "drawable", getPackageName());

ImageView imageView = (ImageView) findViewById(R.id.image);
imageView.setImageResource(resId);