-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?

2
  • Please post what you have tried to receive more specific help Commented Sep 1, 2016 at 3:08
  • Refer to this link and this link Commented Sep 1, 2016 at 3:37

2 Answers 2

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?

Sign up to request clarification or add additional context in comments.

Comments

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.