带有arraylist的listview,android中的简单适配器 [英] listview with arraylist,simple adapter in android

查看:30
本文介绍了带有arraylist的listview,android中的简单适配器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用 arraylist 和简单的适配器在列表视图中显示一些内容.我尝试了类似下面的内容,但在我的结果中显示了数组列表的姓氏.我不明白我的错误是什么.

I try to show something into listview using arraylist and simple adapter. I tried something like below but in my result shows the last names of the arraylist. What is my wrong i cant understand.

final ListView listView = (ListView) findViewById(R.id.mylist);

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

        HashMap<String, String> b = new HashMap<String, String>();

        String[] from = { "php_key","c_key","android_key","hacking_key" };
        String[] name_of_bookmarks = { "php","c","android","hacking" };

            for(int i=0;i<4;i++)
            {
              b.put(from[i],name_of_bookmarks[i]);   
              list_of_bookmarks.add(b);
            }

         };

            int[] to = { R.id.txt1,R.id.txt1,R.id.txt1,R.id.txt1};

            SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), list_of_bookmarks, R.layout.list_layout, from, to);         
            listView.setAdapter(adapter);

我只想在列表视图中显示php"、c"、android"、hacking".什么应该是更有效的方法来做到这一点.我是一个初学者,所以你可能会建议我应该遵循的更好的方法

I just want to show "php","c","android","hacking" in a listview. And what should be more efficient way to do that.I am a beginner so you may suggest a better way which should i follow

推荐答案

Main.xml

<LinearLayout  

    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="60dp"   >


    <ListView
        android:id="@+id/zone_list"
        android:layout_marginBottom="70dp"
        android:background="@drawable/batteryborder"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

</LinearLayout>

setlanguage.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="60dp">

    <TextView
        android:id="@+id/tvName"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:textSize="18dp"
        android:gravity="center_vertical" />

</LinearLayout>

在您的活动文件中添加onCreate()

ListView listView;

String[] from = { "php_key","c_key","android_key","hacking_key" };

ArrayAdapter arrayAdapter;

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

arrayAdapter = new ArrayAdapter<>(this,R.layout.setlanguage, R.id.tvName, from);

listView.setAdapter(arrayAdapter);

这篇关于带有arraylist的listview,android中的简单适配器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆