无法获得卡片索引

时间:2014-03-03 07:00:37

标签: java google-glass

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

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。

1 个答案:

答案 0 :(得分:0)

从CardScrollView实现onClickListener。在你内部,你将能够确定选择了哪个项目。