1

I'm trying to create some layouts dynamically and then inflate them. Currently I have some layouts and I'm inflating them by using my own adapter class.

However now I need to create some cards based on data that is generated. I tried using the card class for this like this for example

for (int i = 1; i < 10; i++) {
            Card card = new Card(this);
            card.setText("Test " + i);
            mCards.add(card);
        }

I can't get this designed how I would want it tho. So is there a way for me to use the xml setup to do this since I have more design options this way?

NoSixties
  • 2,443
  • 2
  • 28
  • 65

1 Answers1

2

at the risk of getting downvoted again I'll post my solution

for now this is just a test to see if it would function the way I want to. Later on I will have the cards generated from an actual list with data inside of it.

First I created a xml layout with a textview inside it

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:id="@+id/test_content"
    android:textSize="47pt"/>

</LinearLayout>

after that I created a adapter that will inflate the layout and set the textview for each item in the list

public class xmlAdapter extends CardScrollAdapter {
private List<Integer> mCards;
private LayoutInflater mInflater;

public xmlAdapter(List<Integer> mCards, LayoutInflater inflater)
{
    this.mCards = mCards;
    this.mInflater = inflater;
}


@Override
public int getCount() {
    return mCards.size();
}

@Override
public Object getItem(int i) {
    return mCards.get(i);
}

@Override
public View getView(int i, View view, ViewGroup viewGroup) {
    View mView = view;

    view = mInflater.inflate(R.layout.xml_test, viewGroup, false);
    TextView text = (TextView) view.findViewById(R.id.test_content);
    text.setText("Test " + mCards.get(i));
    view.setTag(text);
    return view;
}

@Override
public int getPosition(Object o) {
    return this.mCards.indexOf(o);
}

}

and in my activity class I've done the following.

in the onCreate()

    CreateCards();

    mCardScroller = new CardScrollView(this);
    mCardScroller.setAdapter(new xmlAdapter(numberCards, getLayoutInflater()));

    mCardScroller.setOnItemClickListener(this);

take in mind that this is just a for loop to put some data in the list that is being send to the adapter.

public void CreateCards() {
    for (int i = 1; i < 10; i++) {
    numberCards.add(i);
    }
}
NoSixties
  • 2,443
  • 2
  • 28
  • 65