0

I'm working with this library: https://github.com/gabrielemariotti/cardslib

I'm trying to add some text views into cardslib layout item.

Custom text view in layout for single item:

<?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"
    android:padding="10dp" >

    <TextView
        android:id="@+id/card_main_inner_simple_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/card_main_inner_secondary_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TEST"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/card_main_inner_name_to_call"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="THIS TEXT I WANT DYNAMICALLY CHANGE"
        android:textColor="@color/list_gray"
        android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>

I would like to ask, how can i dynamically set and change this textview with id card_main_inner_name_to_call directly from the code?

CardsLIb Library offers only setTitle methods for header and card but nothing like the setTitleById or something similar.

How can I simple do it?

Thanks for any advice

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
redrom
  • 11,502
  • 31
  • 157
  • 264

2 Answers2

2

If you would like to build a card with this layout you can do something like this. You have to extend your class and override the setupInnerViewElements method

public MyCard extends Card{

  public String title1; //just an example... use gettes and setters 
  public String title2;

  public MyCard(Context context){
   super(context, R.layout.your_layout);
  }

  @Override
  public void setupInnerViewElements(ViewGroup parent, View view) {
      TextView tx= (TextView)view.findById(R.id.card_main_inner_simple_title);
      tx.setText(title1);


      //.... set the other ui elements
  }

}

public class MyActivity extends Activity {


   @Override
   protected void onCreate(Bundle savedInstanceState) {
       //......
       MyCard card = new Mycard(this);
       card.title1 = "....";
       card.title2 = "....";

      //Set card in the cardView
       CardView cardView = (CardView) getActivity().findViewById(R.id.carddemo);

       cardView.setCard(card);
   }

}

If you are working with a List, you can use the same card class:

 public class MyActivity extends Activity {


       @Override
       protected void onCreate(Bundle savedInstanceState) {

       //......
           ArrayList<Card> cards = new ArrayList<Card>();
           for (int i=0;i<50;i++){
                MyCard card = new Mycard(this);
                card.title1 = "....";
                card.title2 = "....";
                cards.add(card);
           }

           CardArrayAdapter mCardArrayAdapter = new CardArrayAdapter(this,cards);

           CardListView listView = (CardListView) this.findViewById(R.id.myList);
           if (listView!=null){
             listView.setAdapter(mCardArrayAdapter);
           }
     }
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
-1

I've never used this library but from documentation I can suspect that it should be handled just like normal list. In adapter you should fill view and by notifydatasetchanged() you can refresh it.

https://github.com/gabrielemariotti/cardslib/blob/master/doc/QUICKUSAGE.md

EDIT: I've created very primitive sample below.

Activity:

public class MyActivity extends Activity {

private static final int MAX_LENGTH = 20;
private ArrayList<Card> cards = new ArrayList<Card>();
private CardAdapter arrayAdapter;
private HashMap<Integer, String> optionalValues = new HashMap<Integer, String>();

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

    arrayAdapter = new CardAdapter(getBaseContext(), cards, optionalValues);

    for(int i = 0; i < 10; i++) {
        Card card = new Card(getBaseContext());
        CardHeader header = new CardHeader(getBaseContext());
        header.setTitle("ASDF");
        card.setInnerLayout(R.layout.card1_layout);
        card.addCardHeader(header);
        cards.add(card);
    }

    CardListView listView = (CardListView) findViewById(R.id.card_list);
    listView.setAdapter(arrayAdapter);

    Button btn = (Button) findViewById(R.id.btn_rand);
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String rnd = random();
            Toast.makeText(getBaseContext(), "Generated for 2nd card: " + rnd, Toast.LENGTH_SHORT).show();
            optionalValues.put(1, rnd);
            arrayAdapter.notifyDataSetChanged();
        }
    });
}

public static String random() {
    Random generator = new Random();
    StringBuilder randomStringBuilder = new StringBuilder();
    int randomLength = generator.nextInt(MAX_LENGTH);
    char tempChar;
    for (int i = 0; i < randomLength; i++){
        tempChar = (char) (generator.nextInt(96) + 32);
        randomStringBuilder.append(tempChar);
    }
    return randomStringBuilder.toString();
}}

Adapter:

public class CardAdapter extends CardArrayAdapter {

private final LayoutInflater mInflater;
private HashMap<Integer, String> opts;

public CardAdapter(Context context, List<Card> cards, HashMap<Integer, String> optionalValues) {
    super(context, cards);
    this.mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    this.opts = optionalValues;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View v = super.getView(position, convertView, parent);
    TextView tv = (TextView) v.findViewById(R.id.text1);

    if(position == 1 && opts.get(1) != null) {
        tv.setText(opts.get(1));
    } else {
        //items are recycled so let's get back to default values
        tv.setText("default");
    }

    return v;
}}

Main activity 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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MyActivity">

<Button
    android:id="@+id/btn_rand"
    android:layout_centerHorizontal="true"
    android:text="Randomize 2nd text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<it.gmariotti.cardslib.library.view.CardListView
    android:id="@+id/card_list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="12dp"
    android:layout_marginRight="12dp"
    android:layout_marginTop="60dp"
/>

Card inner layout ("card1_layout.xml"):

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/carddemo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:layout_marginTop="12dp"
android:orientation="vertical">

<TextView
    android:id="@+id/text1"
    android:text="default"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<TextView
    android:id="@+id/text2"
    android:text="default2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />


</LinearLayout>
3mpty
  • 1,354
  • 8
  • 16
  • Could You please provide some simple example? On given link i did not find any useful. – redrom Sep 08 '14 at 11:09
  • Try sample from above. It's showing usage with Card List – 3mpty Sep 08 '14 at 12:24
  • 1
    It can work, but in the library logic, should be the card to set the text elements instead of the adapter. By the way android:layout_height="wrap_content" is not a good choice in Android (all ListViews). – Gabriele Mariotti Sep 08 '14 at 22:40