How can I make ADT ArrayAdapter use any layout? It only lets me use textViewLayout, when I want to add other buttons

StackOverflow https://stackoverflow.com/questions/23510921

Question

Here is my first file FriendsList.java package com.example.swagmessenger2;

    import java.util.ArrayList;
    import java.util.HashMap;
import java.util.List;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class FriendsList extends Activity {

    ListView listview;
    ArrayList<String> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_friends_list);

        listview = (ListView) findViewById(R.id.listView1);
        String[] names = {"Timothy Choi", "Orion Ou", "Bryan Ly", "Elizabeth Lin", "Johnson Gong"}; 
        //names

        list = new ArrayList<String>();
        for(int i=0; i < names.length; i++){
            list.add(names[i]); //add names into the list
        }

        MyArrayAdapter adapter = new MyArrayAdapter(this, R.layout.activity_friends_list, list);

        listview.setAdapter(adapter);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.friends_list, menu);
        return true;
    }

    //imported
    private class MyArrayAdapter extends ArrayAdapter<String> {

        HashMap<String, Integer> mIdMap = new HashMap<String, Integer>();

        public MyArrayAdapter(Context context, int textViewResourceId,
            List<String> objects) {
          super(context, textViewResourceId, objects);
          for (int i = 0; i < objects.size(); ++i) {
            mIdMap.put(objects.get(i), i);
          }
        }

        @Override
        public long getItemId(int position) {
          String item = getItem(position);
          return mIdMap.get(item);
        }

        @Override
        public boolean hasStableIds() {
          return true;
        }

      }
}

Here's my second activity_freinds_list.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".FriendsList" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="Friends List"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="Swag+" />

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/button1"
        android:layout_below="@+id/button1" >

    </ListView>

</RelativeLayout>

Is there any way I can change the adapter So I can use any layout?

I want a few other buttons in this layout. I'm making a friendslist right now Where There is a list of friends, a button where I can add friend. And a text that says friends list. Later I play to modify the arrayadapter so it can have online or offline signals. green dot for online and grey dot for like offline. But for now I would like to know How to connect the array adapter to the list view layout. Then I'm assuming I can add other listviews in the same layout. Or be able to edit it .

Was it helpful?

Solution

You have to Override the getView(...) method in your MyArrayAdapter class. The View that is passed back from this method basically is one item of the list. This way, you can create your custom ViewGroup to be used as a List element. I recommend to you to read this Tutorial: ListView Tutorial.

Quote from Android API here:

To use something other than TextViews for the array display, for instance, ImageViews, or to have some of data besides toString() results fill the views, override getView(int, View, ViewGroup) to return the type of view you want.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top