如何使用JSON数据动态填充卡片内容+;cardslib android库
我对android编程非常陌生,实际上我正在尝试学习如何构建一个非常简单的应用程序,使其看起来像Google Play store的用户界面 我发现了一个非常酷的android库,名为cardslib(),它实际上在做我或多或少想做的事情 我现在的问题是如何从web服务创建具有动态内容的卡片,而不是静态图像和文本加载如何使用JSON数据动态填充卡片内容+;cardslib android库,android,user-interface,android-ui,android-library,android-volley,Android,User Interface,Android Ui,Android Library,Android Volley,我对android编程非常陌生,实际上我正在尝试学习如何构建一个非常简单的应用程序,使其看起来像Google Play store的用户界面 我发现了一个非常酷的android库,名为cardslib(),它实际上在做我或多或少想做的事情 我现在的问题是如何从web服务创建具有动态内容的卡片,而不是静态图像和文本加载 /** * This method builds a simple card */ private void initCard() { //Init an array
/**
* This method builds a simple card
*/
private void initCard() {
//Init an array of Cards
ArrayList<Card> cards = new ArrayList<Card>();
for (int i = 0; i < 200; i++) {
PicassoCard card = new PicassoCard(this.getActivity());
card.setTitle("A simple card loaded with Picasso " + i);
card.setSecondaryTitle("Simple text..." + i);
card.setCount(i);
cards.add(card);
}
CardArrayAdapter mCardArrayAdapter = new CardArrayAdapter(getActivity(), cards);
CardListView listView = (CardListView) getActivity().findViewById(R.id.carddemo_extra_list_picasso);
if (listView != null) {
listView.setAdapter(mCardArrayAdapter);
}
}
例如,如何修改该类,使创建的每个卡都包含来自上述web服务的上述数据
class PicassoCardThumbnail extends CardThumbnail {
public PicassoCardThumbnail(Context context) {
super(context);
}
@Override
public void setupInnerViewElements(ViewGroup parent, View viewImage) {
/*
* If your cardthumbnail uses external library you have to provide how to load the image.
* If your cardthumbnail doesn't use an external library it will use a built-in method
*/
//Here you have to set your image with an external library
//Only for test, use a Resource Id and a Url
//It is just an example !
if (((PicassoCard) getParentCard()).getCount() % 2 == 0) {
Picasso.with(getContext()).setDebugging(true); //only for debug tests
Picasso.with(getContext())
.load("https://plus.google.com/s2/photos/profile/114432517923423045208?sz=96")
.error(R.drawable.ic_error_loadingsmall)
.into((ImageView) viewImage);
} else {
Picasso.with(getContext()).setDebugging(true); //only for debug tests
Picasso.with(getContext())
.load(R.drawable.ic_na_img)
.resize(96, 96)
.into((ImageView) viewImage);
}
/*
viewImage.getLayoutParams().width = 96;
viewImage.getLayoutParams().height = 96;
*/
}
}
非常感谢您的指点和帮助。我也在尝试使用这个伟大的库(cardslib)和来自Google()的Volley项目,但是现在对我来说非常困难
如果有人已经做到了这一点,如果有机会学习一些示例代码,我们将不胜感激。您可以在异步任务中填充/更新适配器 在AsyncTask中,可以下载并解析json(在doBackground方法中),然后可以更新适配器
//Process json
ArrayList<Card> cards = new ArrayList<Card>();
for (//your json elements) {
MyCard card = new MyCard(this.getActivity());
card.setTitle(json.getxx);
card.setSecondaryTitle(json.getxxx);
cards.add(card);
}
if (mCardArrayAdapter != null) {
mCardArrayAdapter-addAll(cards);
mCardArrayAdapter.notifyDataSetChanged();
}
//Process json
ArrayList<Card> cards = new ArrayList<Card>();
for (//your json elements) {
MyCard card = new MyCard(this.getActivity());
card.setTitle(json.getxx);
card.setSecondaryTitle(json.getxxx);
cards.add(card);
}
if (mCardArrayAdapter != null) {
mCardArrayAdapter-addAll(cards);
mCardArrayAdapter.notifyDataSetChanged();
}
publc class MyCard extends Card{
public XXXX myField;
//....
}