gpt4 book ai didi

android - Cardslib - 自定义 CardArrayAdapter

转载 作者:行者123 更新时间:2023-11-29 00:18:03 26 4
gpt4 key购买 nike

我创建了一个扩展 CardArrayAdapter 的自定义适配器。只是玩它以获得舒适。有点把它当作一个带有观察器的自定义底座适配器等等。在测试应用程序中设置我认为正确的所有内容,应用程序打开,但没有填充任何内容。

感谢任何帮助。

Java 文件:

    package com.example.test;

import it.gmariotti.cardslib.library.internal.Card;
import it.gmariotti.cardslib.library.internal.CardArrayAdapter;
import it.gmariotti.cardslib.library.view.CardListView;
import it.gmariotti.cardslib.library.view.CardView;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_list);

CardListView listView = (CardListView) this.findViewById(R.id.myList);
List<Card> cards= new ArrayList<Card>();

for (int i = 0; i < 10; i++) {
String header = "Poop card" + String.valueOf(i);
String body = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut";

CardExample card = new CardExample(this, header, body);
cards.add(card);
}

MyCardArrayAdapter cardArrayAdapter = new MyCardArrayAdapter(this, cards);
if (listView != null) {
listView.setAdapter(cardArrayAdapter);
}
}

public class CardExample extends Card {

protected TextView headerView;
protected TextView titleView;

protected String mTitleHeader;
protected String mTitleMain;

public CardExample(Context context, String titleHeader, String titleMain) {
super(context, R.layout.row_card);
Log.d("CardExample",
String.valueOf(titleHeader) + ", "
+ String.valueOf(titleMain));

this.mTitleHeader = titleHeader;
this.mTitleMain = titleMain;
}

@Override
public void setupInnerViewElements(ViewGroup parent, View view) {
// TODO Auto-generated method stub
headerView = (TextView) view
.findViewById(R.id.card_main_inner_simple_header);
titleView = (TextView) view
.findViewById(R.id.card_main_inner_simple_title);

headerView.setText(mTitleHeader);
titleView.setText(mTitleMain);

}
}

public class MyCardArrayAdapter extends CardArrayAdapter {

Context mContext;
ViewHolder mHolder;
List<Card> cards;

public MyCardArrayAdapter(Context context, List<Card> cards) {
super(context, cards);
// TODO Auto-generated constructor stub
mContext = context;
this.cards = cards;

Log.d("MyCardArrayAdapter", "Cards size = " + String.valueOf(cards.size()));
}

@Override
public int getCount() {
// TODO Auto-generated method stub
return cards.size();
}

@Override
public Card getItem(int position) {
// TODO Auto-generated method stub
return cards.get(position);
}

@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.layout_cardview,
parent, false);
mHolder = new ViewHolder();

mHolder.cardView = (CardView) convertView
.findViewById(R.id.row_card);

convertView.setTag(mHolder);
} else {
mHolder = (ViewHolder) convertView.getTag();
}

mHolder.cardView.setCard(getItem(position));

return convertView;
}

public class ViewHolder {
private CardView cardView;
}
}
}

layout_list.xml:

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

<it.gmariotti.cardslib.library.view.CardListView
android:id="@+id/myList"
style="@style/list_card.thumbnail"
android:layout_width="match_parent"
android:layout_height="match_parent"
card:list_card_layout_resourceID="@layout/list_card_thumbnail_layout" />

</LinearLayout>

layout_cardview.xml:

<it.gmariotti.cardslib.library.view.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card="http://schemas.android.com/apk/res-auto"
android:id="@+id/row_card"
style="@style/list_card"
android:layout_width="190dp"
android:layout_height="wrap_content"
card:card_layout_resourceID="@layout/row_card" />

row_card.xml

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

<TextView
android:id="@+id/card_main_inner_simple_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp" />

<TextView
android:id="@+id/card_main_inner_simple_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp" />

最佳答案

您的 getView() 没有调用 super.getView();

关于android - Cardslib - 自定义 CardArrayAdapter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25089027/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com