簡體   English   中英

通過ArrayList對象填充ListView

[英]Populate ListView by ArrayList object

我的Android應用程序需要使用包含5個字段的對象的ArrayList中的數據來按數組適配器填充ListView

我做這件事很麻煩。 有人可以幫我提供代碼嗎?

嘗試這個

void myData(){
    List<String> array = new ArrayList<String>();
    array.add("kk");
    array.add("bb");

    ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,array);

    listView.setAdapter(adapter);
}

然后在onCreate上調用myData()方法

有關更復雜的示例,請參見此處

使用ArrayList填充ListView?

嘗試這個:

import android.app.*;
import android.widget.*;
import android.os.*;
import java.util.*;
import android.view.*;

public class MainActivity extends Activity {

ListView nameListView;
ArrayList<HashMap<String, String>> nameList =new ArrayList<HashMap<String, String>>();

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


        setContentView(R.layout.main);

        nameListView = (ListView) findViewById(R.id.nameListView);
        HashMap<String, String> map=new HashMap<String, String>();

        map.put("name","jack");
        map.put("value1", "100");
        map.put("value2", "200");
        map.put("value3", "300");
        map.put("value4", "400");
        nameList.add(map);

    if (nameList.size() != 0) {

        final ListAdapter nameAdapter = new SimpleAdapter(this, nameList, R.layout.view_entry, new String[] {
                                                              "name", "value1" ,"value2","value3","value4"}, new int[] { R.id.name, R.id.v1, R.id.v2 , R.id.v3, R.id.v4 });

        nameListView.setAdapter(nameAdapter);

        nameListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
                    HashMap<String, String> map  = (HashMap<String, String>)nameAdapter.getItem(position);

                    String name=map.get("name");
                    Toast.makeText(getApplicationContext(), name, Toast.LENGTH_LONG).show();





                }
            });
    }
}
}

view_entry.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="wrap_content"
android:orientation="horizontal">

<TextView
    android:id="@+id/name"
    android:layout_height="wrap_content"
    android:text="Text"
    android:layout_width="wrap_content"/>
<TextView
    android:id="@+id/v1"
    android:layout_height="wrap_content"
    android:text="Text"
    android:layout_width="wrap_content"/>
<TextView
    android:id="@+id/v2"
    android:layout_height="wrap_content"
    android:text="Text"
    android:layout_width="wrap_content"/>
<TextView
    android:id="@+id/v3"
    android:layout_height="wrap_content"
    android:text="Text"
    android:layout_width="wrap_content"/>
<TextView
    android:id="@+id/v4"
    android:layout_height="wrap_content"
    android:text="Text"
    android:layout_width="wrap_content"/>

</LinearLayout>

main.xml:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">

<ListView
    android:id="@+id/nameListView"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"/>

</LinearLayout>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM