我有一个带有自定义ArrayAdapter的ListView。 ArrayAdapter的getView()
返回一个带有CardListView的视图。我正在尝试为ListView设置OnItemClickListener。它编译得很好,但点击一个项目(这是一个CardListView的视图)什么都不做。有办法解决这个问题吗?
这是我的getView()
:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
List<String> cardTextGroup = cardTextList.get(position);
List<Card> cardGroup = new ArrayList<Card>();
for (int i = 0; i < cardTextGroup.size(); i++) {
Card card = new Card(context);
MatchOngoing.HeaderInnerCard header = new MatchOngoing.HeaderInnerCard(context);
String cardText = cardTextGroup.get(i);
if (!cardText.equals(blackCardText)) {
header.setCardText(cardText);
card.addCardHeader(header);
card.setBackgroundResourceId(R.drawable.card_background);
cardGroup.add(card);
}
}
View view = convertView;
if (view == null) {
// Create inflater
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// Get item view from inflater
view = inflater.inflate(R.layout.match_ongoing_table_item, null);
}
CardListView cardGroupView =
(CardListView) view.findViewById(R.id.match_ongoing_table_played_cards_group);
CardArrayAdapter cardArrayAdapter = new CardArrayAdapter(context, cardGroup);
cardGroupView.setBackgroundColor(Color.parseColor("#e6e6e6"));
cardGroupView.setAdapter(cardArrayAdapter);
return view;
}
答案 0 :(得分:-1)
查看CustomCard.java适配器,您将了解如何使用替代onitemclicklistener
其中i是列表视图中点击的卡号
public class CustomCard extends Card {
String[] listData1;
protected TextView mTitle;
Intent mainIntent1;
Context context;
int i;
SpannableString s;
/**
* Constructor with a custom inner layout
* @param context
*/
public CustomCard(Context context,String[] listData1, int i) {
this(context, R.layout.adapter_text);
this.context=context;
this.i=i;
this.listData1=listData1;
}
/**
*
* @param context
* @param innerLayout
*/
public CustomCard(Context context, int innerLayout) {
super(context, innerLayout);
init();
}
/**
* Init
*/
private void init(){
//No Header
//Set a OnClickListener listener
setOnClickListener(new OnCardClickListener() {
@Override
public void onClick(Card card, View view) {
// TODO Auto-generated method stub
if(i==1)
{
mainIntent1 = new Intent(context,WeightBand.class);
mainIntent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(mainIntent1);
}
if(i==0)
{
mainIntent1 = new Intent(context,AerobicsSchedule.class);
mainIntent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(mainIntent1);
}
if(i==2)
{
mainIntent1= new Intent(context,AdditionalExercises.class);
mainIntent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(mainIntent1);
}
}
});
}
@Override
public void setupInnerViewElements(ViewGroup parent, View view) {
//Retrieve elements
}
}