谷歌眼镜 - 创建选项菜单,单击项目时没有任何反应

时间:2014-04-25 18:13:31

标签: android menu menuitem google-glass google-gdk

我正在为Glass创建一个应用程序,显示已保存项目的列表(RememberItem)。每个项目都有其卡片,当您点击它时,会出现一个菜单,其中包含以下选项:获取路线(启动谷歌导航到保存项目的位置),删除(从列表中删除项目)。 但是,当我们点击菜单选项时,没有任何反应。

以下是代码:

package com.adrianavecc.findit;

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

import android.app.Activity;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.location.Location;
import android.os.Bundle;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Message;
import android.speech.RecognizerIntent;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import com.adrianavecc.findit.adapter.ScrollAdapter;
import com.adrianavecc.findit.db.SqlHelper;
import com.adrianavecc.findit.domain.RememberItem;
import com.adrianavecc.findit.util.RememberUtils;
import com.google.android.glass.app.Card;
import com.google.android.glass.app.Card.ImageLayout;
import com.google.android.glass.widget.CardScrollView;

public class Find extends Activity implements Callback {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ArrayList<String> voiceResults = getIntent().getExtras().getStringArrayList(RecognizerIntent.EXTRA_RESULTS);
    String tag= voiceResults.get(0);
    RememberItem item=SqlHelper.getInstance(this).findRememberItem(tag);
    if(item!=null){
        List<RememberItem> items=new ArrayList<RememberItem>();
        items.add(item);
        displayRememberItems(items);
    } else{
        if(RememberUtils.EVERY_THING.equalsIgnoreCase(tag)){
            List<RememberItem> items=SqlHelper.getInstance(this).findAllRememberItem();
            displayRememberItems(items);
        }
        else{
            displayFailureView();
        }
    }
}

private void displayFailureView() {
    Card fail = new Card(this);
    fail.setText(R.string.storefailhead);
    fail.setFootnote(R.string.storefailfoot);
    fail.setImageLayout(Card.ImageLayout.FULL);
    fail.addImage(R.drawable.storefailbackground);
    View failView = fail.getView();
    setContentView(failView);
}
private Card createCardOfRememberItem(RememberItem item) {
    Card card = new Card(this);
    card.setText(item.getTag());
    card.setFootnote(String.format("%tc", item.getAddedDate()));
    card.setImageLayout(ImageLayout.FULL);
    card.addImage(BitmapFactory.decodeFile(item.getImagePath()));
    return card;
}
private void launchGoogleMap(RememberItem item) {
    Location location=item.getLocation();
    Intent intent=RememberUtils.getGeoIntentFromLocation(location);
    Find.this.startActivity(intent);
}
private void displayRememberItems(final List<RememberItem> items) {
    List<Card> mCards = new ArrayList<Card>();
     for(RememberItem item: items){
         Card card=createCardOfRememberItem(item);
         mCards.add(card);
     }
    CardScrollView mCardScrollView = new CardScrollView(this);
    final ScrollAdapter adapter = new ScrollAdapter(mCards);
    mCardScrollView.setOnItemClickListener(new OnItemClickListener() {

                    @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            Card card=(Card)adapter.getItem(arg2);
            RememberItem item=SqlHelper.getInstance(getApplicationContext()).findRememberItem(card.getText().toString());
            openOptionsMenu();

        }
    });

    mCardScrollView.setAdapter(adapter);
    mCardScrollView.activate();
    setContentView(mCardScrollView);
}

private void showSucessDeleteCard(RememberItem item){
    Card card = new Card(this);
    card.setText(R.string.object_delete);
    card.setImageLayout(ImageLayout.FULL);
    card.addImage(R.drawable.finditlogobg);
    setContentView(card.getView());
    Handler handler=new Handler(this);
    handler.sendEmptyMessageDelayed(0, 3000);
}

@Override
public boolean handleMessage(Message msg) {
    finish();
    return false;
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.saveditemmenu, menu);
    return true;
}

public boolean onOptionsItemSelected(MenuItem menuitem, RememberItem item) {
    switch (menuitem.getItemId()) {
        case R.id.menu_getdirections:
            launchGoogleMap(item);
            return true;
        case R.id.menu_delete:
            SqlHelper.getInstance(getApplication()).deleteRememberItem(item);
            showSucessDeleteCard(item);
            return true;
        default:
            return super.onOptionsItemSelected(menuitem);
    }
}

}

2 个答案:

答案 0 :(得分:1)

您必须覆盖作为事件的公共布尔值onOptionsItemSelected(MenuItem item),然后从该方法调用onOptionsItemSelected(MenuItem menuitem,RememberItem item)。

答案 1 :(得分:1)

也许为时已晚。你能试试吗

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    // TODO Auto-generated method stub
    if(keyCode == KeyEvent.KEYCODE_DPAD_CENTER){
        Log.d("TOUCH","DPAD_CENTER");
        this.openOptionsMenu();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}