-1

My Android app needs to populate the ListView by array adapter using the data from an ArrayList of object which contains 5 fileds

I have trouble doing this. Can someone please help me with the code?

Shabbir Dhangot
  • 8,089
  • 9
  • 55
  • 75
Masud Ran
  • 1
  • 1
  • Please post what you have tried to receive more specific help – cafebabe1991 Sep 01 '16 at 03:08
  • Refer to [this link](http://stackoverflow.com/questions/15439062/how-to-populate-listview-from-arrayadapter) and [this link](http://www.vogella.com/tutorials/AndroidListView/article.html) – user3678528 Sep 01 '16 at 03:37

2 Answers2

0

Try this

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

then call myData() method on your onCreate

For a bit more complexed example, refer here

Populating a ListView using an ArrayList?

Community
  • 1
  • 1
Fay Zan
  • 165
  • 1
  • 14
0

Try this:

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>