gabrielemariotti / cardslib

Android Library to build a UI Card
4.66k stars 1.19k forks source link

I couldn't set up the cardwithlist card #477

Closed fatihturgut closed 9 years ago

fatihturgut commented 9 years ago

Hi, this is great library. However, i could not get the card that you show on this link: https://github.com/gabrielemariotti/cardslib/blob/master/doc/CARDWITHLIST.md

I implemented every part of codes that you explain in that link. I got card on screen but only this card: untitled

*This is my card_with_list.xml: 1

*This is my CardWithListModel class:

import android.content.Context; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; import java.util.ArrayList; import java.util.List; import it.gmariotti.cardslib.library.internal.Card; import it.gmariotti.cardslib.library.internal.CardHeader; import it.gmariotti.cardslib.library.internal.base.BaseCard; import it.gmariotti.cardslib.library.prototypes.CardWithList; import it.gmariotti.cardslib.library.prototypes.LinearListView; public class CardWithListModel extends CardWithList { ``` public CardWithListModel(Context context) { super(context); } @Override protected CardHeader initCardHeader() { //Add Header CardHeader header = new CardHeader(getContext()); //Add a popup menu. This method set OverFlow button to visible header.setPopupMenu(R.menu.popup_item, new CardHeader.OnClickCardHeaderPopupMenuListener() { @Override public void onMenuItemClick(BaseCard card, MenuItem item) { switch (item.getItemId()){ case R.id.action_add: WeatherObject w1= new WeatherObject(CardWithListModel.this); w1.city ="Madrid"; w1.temperature = 24; w1.weatherIcon = R.drawable.ic_action_add; w1.setObjectId(w1.city); mLinearListAdapter.add(w1); break; case R.id.action_remove: mLinearListAdapter.remove(mLinearListAdapter.getItem(0)); break; } } }); header.setTitle("Weather"); //should use R.string. return header; } @Override protected void initCard() { setSwipeable(true); setOnSwipeListener(new OnSwipeListener() { @Override public void onSwipe(Card card) { Toast.makeText(getContext(), "Swipe on " + card.getCardHeader().getTitle(), Toast.LENGTH_SHORT).show(); } }); } @Override protected List initChildren() { List mObjects = new ArrayList(); WeatherObject w1= new WeatherObject(this); w1.city ="London"; w1.temperature = 16; w1.weatherIcon = R.drawable.ic_action_add; w1.setObjectId(w1.city); mObjects.add(w1); return mObjects; } @Override public View setupChildView(int childPosition, ListObject object, View convertView, ViewGroup parent) { TextView city = (TextView) convertView.findViewById(R.id.carddemo_weather_city); ImageView icon = (ImageView) convertView.findViewById(R.id.carddemo_weather_icon); TextView temperature = (TextView) convertView.findViewById(R.id.carddemo_weather_temperature); WeatherObject weatherObject= (WeatherObject)object; icon.setImageResource(weatherObject.weatherIcon); city.setText(weatherObject.city); temperature.setText(weatherObject.temperature + weatherObject.temperatureUnit); return convertView; } @Override public int getChildLayoutId() { return R.layout.carddemo_googlenowweather_inner_main; } // ------------------------------------------------------------- // Weather Object // ------------------------------------------------------------- public class WeatherObject extends DefaultListObject{ public String city; public int weatherIcon; public int temperature; public String temperatureUnit="°C"; public WeatherObject(Card parentCard){ super(parentCard); init(); } private void init(){ //OnClick Listener setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(LinearListView parent, View view, int position, ListObject object) { Toast.makeText(getContext(), "Click on " + getObjectId(), Toast.LENGTH_SHORT).show(); } }); //OnItemSwipeListener setOnItemSwipeListener(new OnItemSwipeListener() { @Override public void onItemSwipe(ListObject object, boolean dismissRight) { Toast.makeText(getContext(), "Swipe on " + object.getObjectId(), Toast.LENGTH_SHORT).show(); } }); } } ``` } *This is my activity: import android.support.v7.app.ActionBarActivity; import android.os.Bundle; public class deneme extends ActionBarActivity { ``` @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.card_with_list); CardWithListModel card = new CardWithListModel(getApplicationContext()); card.init(); } ``` } *I have also another .xml file which is carddemo_googlenowweather_inner_main.xml, exactly same in cardslib file. These codes does not give any error, but as i said im getting the card on screen in first picture. Could you help me to run this code please?
gabrielemariotti commented 9 years ago

You can check the code in the demo app: https://github.com/gabrielemariotti/cardslib/blob/master/demo/stock/src/main/java/it/gmariotti/cardslib/demo/fragment/nativeview/NativeCardWithListFragment.java#L91

In you case, you have to assign the model to the view:

    CardWithListModel card = new CardWithListModel(getApplicationContext());
    card.init();

     //Set card in the cardView
     CardViewNative cardView = (CardViewNative) getActivity().findViewById(R.id.carddemo);
     cardView.setCard(card);
fatihturgut commented 9 years ago

Great :+1: @gabrielemariotti , you solved the problem immediately. Thank you, so much :)