列表视图,在Android的ArrayList中,简单的适配器适配器、视图、简单、列表

2023-09-06 09:34:06 作者:对天空许愿

我尽量表现的东西到使用ArrayList和简单的适配器列表视图。 我想是这样,但下方在我的结果表明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,机器人,在列表视图黑客。 什么应该是更有效的方法来做到that.I是一个初学者,所以你可以提出一个更好的方式,也应该我按照

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