0

I have been trying to learn how to develop an Android app using the "Master/Detail Flow" in Eclipse.

My problem is that I cannot understand how to create my own ArrayAdapter (So I can change the colors of each line, etc etc) that pretty much looks the same but will take "LibHome.ITEMS" (An Object? as opposed to a simple array.

I can't really understand how to make this work. I also do not wish to blindly adapt another solution rather than learn.

setListAdapter(
        new ArrayAdapter<LibHome.GenItem>(
            getActivity(),
            android.R.layout.simple_list_item_activated_1,
            android.R.id.text1,
            LibHome.ITEMS
        )
    );

LibHome.java:

package com.example.prac2.lib;

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

public class LibHome {

    /**
     * Create an Array
     */
    public static List<GenItem> ITEMS = new ArrayList<GenItem>();

    /**
     * A map of sample (dummy) items, by ID.
     */
    public static Map<String, GenItem> ITEM_MAP = new HashMap<String, GenItem>();

    /**
     * Add Items to the array
     */
    static {
        // Items!
        addItem(new GenItem("1", "Title1", "File1"));
        addItem(new GenItem("2", "Title2", "File2"));
        addItem(new GenItem("2", "Title3", "File3"));

    private static void addItem(GenItem item) {
        ITEMS.add(item);
        ITEM_MAP.put(item.id, item);
    }

    /**
     * A dummy item representing a piece of content.
     */
    public static class GenItem {
        public String id;
        public String content;
        public String file;

        public GenItem(String id, String content, String file) {
            this.id = id;
            this.content = content;
            this.file = file;
        }

        @Override
        public String toString() {
            return content;
        }
    }

}
halfer
  • 19,824
  • 17
  • 99
  • 186

1 Answers1

2

For this to work, you would need to create a custom Adapter class that takes LibHome items. To do this, you would do something like this,

public class LibAdapter extends ArrayAdapter<LibHome> {
public LibAdapter(Context context, int textViewResourceId, ArrayList<LibHome> objects) {
    super(context, textViewResourceId, objects);
    this.objects = objects;
}


public View getView(int position, View convertView, ViewGroup parent){
            //Here is where you would change the colors of the text. 
            return v;

}

}

There are a few good tutorials out there that will help you.. Here are some..

Shirwa Mohamed
  • 191
  • 1
  • 9