无法获取卡索引 [英] Unable to get the card index

查看:58
本文介绍了无法获取卡索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用此代码获取在屏幕上处于活动状态的卡的索引.

I am trying to get the index of the card which is active on the screen with this code.

import java.util.ArrayList;
import java.util.Arrays;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import com.google.android.glass.app.Card;
import com.google.android.glass.touchpad.Gesture;
import com.google.android.glass.touchpad.GestureDetector;
import com.google.android.glass.widget.CardScrollAdapter;
import com.google.android.glass.widget.CardScrollView;
import com.google.api.services.mirror.model.TimelineItem;

public class MainActivity extends Activity {
    private GestureDetector mGestureDetector;
    String identifysource;
    int cardindex;
    private static ArrayList<Card> sourceCard = new ArrayList<Card>();
    private ArrayList<String> sourceText = new ArrayList<String>(Arrays.asList("PCWorld", "TechCrunch"));

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

       for(int i=0;i<sourceText.size();i++)
       {
           System.out.println("whats up");
        Card card = new Card(this);
        identifysource=sourceText.get(i);
        card.setText(identifysource);
        sourceCard.add(card);


       }
       System.out.println(this);
       mGestureDetector = createGestureDetector(this);
       CardScrollView csvCardsView = new CardScrollView(this);
       csaAdapter cvAdapter = new csaAdapter();
       csvCardsView.setAdapter(cvAdapter);
       csvCardsView.activate();
       setContentView(csvCardsView);
       cardindex=csvCardsView.getSelectedItemPosition();

    }

private static class csaAdapter extends CardScrollAdapter
    {
        @Override
        public int findIdPosition(Object id)
        {
            return -1;
        }

        @Override
        public int findItemPosition(Object item)
        {
            return sourceCard.indexOf(item);
        }

        @Override
        public int getCount()
        {
            return sourceCard.size();
        }

        @Override
        public Object getItem(int position)
        {
            return sourceCard.get(position);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent)
        {
            return sourceCard.get(position).toView();
        }

    }
}

这将导致每次输出相同的cardindex,即0 我尝试使用cvAdapter.findItemPosition()获取位置,但每次都会返回-1.

This results in the same cardindex output every time i.e 0 I tried getting the position with cvAdapter.findItemPosition() but that also returns -1 everytime.

推荐答案

从CardScrollView中实现onClickListener.在其中,您可以确定选择了哪个项目.

Implement the onClickListener from the CardScrollView. Within you'll be able to determine which item was selected.

这篇关于无法获取卡索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆