Domanda

I got an Android Problem I have no idea how to add an ArrayAdapter or ListAdapter to display my 2 dim Array.

I got:

 ArrayList<ArrayList<String>> laktoseList;
laktoseList = new ArrayList<ArrayList<String>>();

    laktoseList.add(new ArrayList<String>());
    laktoseList.get(0).add("Milch");
    laktoseList.get(0).add("15");
    laktoseList.add(new ArrayList<String>());
    laktoseList.get(1).add("Jogurht");
    laktoseList.get(1).add("11");
    laktoseList.add(new ArrayList<String>());
    laktoseList.get(2).add("Woot");
    laktoseList.get(2).add("19.5");


    ListView listViewName = (ListView) findViewById(R.id.listName);
    ListView listViewGramm = (ListView) findViewById(R.id.listGramm);


    MyCustomArrayAdapter nameAdapter = new MyCustomArrayAdapter(this, R.id.listName, laktoseList.get(0));
    MyCustomArrayAdapter grammAdapter = new MyCustomArrayAdapter(this, R.id.listGramm, laktoseList.get(1));

is that right until now? I got my 2 dim array for the name of the Item and for the gramm. I have 2 ListViews created in the ADT and this should go in.

but I dont know how to write an adapter for a ArrayList<ArrayList> . :(

to set the values to the List.

Thanks

EDIT

but I need 2 values. Here is a Picture pic-upload.de/view-23169548/app.jpg.html I need to say the First list the name and the second list the among of gramms

enter image description here

È stato utile?

Soluzione

You should use one listview and you should create pojo for your list. I wrote for you:)

Your Activity(you can use fragment):

public class YourActivity extends Activity {
    ArrayList<Item> laktoseList = new ArrayList<Item>();

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

        //add your objects to list
        laktoseList.add(new Item("Milch", "15"));
        laktoseList.add(new Item("Jogurht", "11"));
        laktoseList.add(new Item("Woot", "19.5"));

        ListView listView = (ListView) findViewById(R.id.your_listview);

        ItemListAdapter adapter = new ItemListAdapter(this, list_item_laktose, laktoseList);
        listView.setAdapter(adapter);
    }
}

Custom Adapter:

public class ItemListAdapter extends ArrayAdapter<Item> {

    private ArrayList<Item> mDatas;
    private Context mContext;
    private LayoutInflater mInflater;
    private int mListItemResourceID;

    public ItemListAdapter(
            Context context, int listItemResourceId, ArrayList<Item> items) {
        super(context, listItemResourceId, items);
        mDatas = items;
        mContext = context;
        mInflater = LayoutInflater.from(mContext);
        mListItemResourceID = listItemResourceId;
    }

    class ViewHolder {
        TextView mNameTextView;
        TextView mGrammTextView;
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        final Item item = mDatas.get(position);
        final ViewHolder holder;

        if (convertView == null) {
            convertView = mInflater.inflate(mListItemResourceID, parent, false);

            holder = new ViewHolder();
            holder.mNameTextView = (TextView) convertView.findViewById(R.id.textview_name);
            holder.mGrammTextView = (TextView) convertView.findViewById(R.id.textview_gramm);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (item != null) {
            holder.mNameTextView.setText(item.getName());
            holder.mGrammTextView.setText(item.getGramm());
        }

        return convertView;
    }
}

POJO:

public class Item {
    private String name;
    private String gramm;
    //you can also add different variables

    public Item(String name, String gramm) {
        this.name = name;
        this.gramm = gramm;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getGramm() {
        return gramm;
    }

    public void setGramm(String gramm) {
        this.gramm = gramm;
    }
}

list_item_laktose:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/textview_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/textview_gramm"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

Altri suggerimenti

You will need to use a Hashmap to store this kind of Data initialize one like this.

HashMap<String, String> hashMap = new HashMap<String, String>();

Insert the Data to the hashmap, for example

hashMap.put("key", value);

Create an Array list that can hold the hashmap,

ArrayList<HashMap<String, String>> arrayList = new ArrayList<HashMap<String, String>>();

insert the hash map into the arrayList

arrayList.add(hashMap);

Use simple Adapter to hold the Array

int[] ids = {android.R.id.yourtextid android.R.id.yourtextid};
String[] keys = {Create_key, Create_KEY};

SimpleAdapter adapter = new SimpleAdapter(this, arrayList, android.R.layout.simple_list_item_2, keys, ids);

finally, use only one listview

listview.setAdapter(adapter);

Check this ExpandableListAdapter and this ExpandableListView

This wdiget have that purpose for two dimensional array.

Hope its help.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top