来自Service的Google Glass LiveCard更新列表视图

时间:2015-01-26 10:09:43

标签: android listview android-listview google-glass google-gdk

我有一个Google Glass LiveCard应用程序,其中后台服务从远程URL获取数据。

我在LiveCard活动中添加了TextView和ListView:

<TextView
        android:id="@+id/home_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#fff"
        android:text="@string/hello_world" />
    <ListView
        android:id="@+id/order_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    </ListView>

我无法像使用TextView那样从Service刷新ListView数据。 对于TextView,我使用方法:

remoteView.setTextViewText(R.id.home_text, "Hello world !");

我是Activity onCreate方法我按如下方式设置了适配器:

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

      String[] OrdersArray = new String[];
      ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.order_list, android.R.id.text1, OrdersArray);
   }

有什么建议吗? 提前谢谢。

1 个答案:

答案 0 :(得分:1)

您可以在传感器管理器的帮助下实现头部可滚动列表视图。它会计算您的头部倾斜的角度,并同样滚动列表视图。这是玻璃应用程序中更方便的方法,而不是按钮甚至语音命令。

以下是代码段:

public class MainActivity extends Activity implements SensorEventListener {

private SensorManager mSensorManager;
private Sensor mOrientation;

ListView listView ;
ArrayAdapter<String> adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
    mOrientation = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
    mSensorManager.registerListener(this, mOrientation, SensorManager.SENSOR_DELAY_GAME);

    // Get ListView object from xml
    listView = (ListView) findViewById(R.id.list);

    // Defined Array values to show in ListView
    String[] values = new String[] { "Glass App list View", 
            "Using Adapter to inflate",
            "Basic Coding example",
            "How was your day", 
            "I love coding in Android", 
            "List Views all over the place", 
            "Services to fetch the data", 
            "And bind up in a list view" 
    };



    adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, values);

    // Assign adapter to ListView
    listView.setAdapter(adapter); 
}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {}

@Override
public void onSensorChanged(SensorEvent event) {
    /* *** need only pitch angle (head bouncing backwards and forward)*** */
    float pitch_angle = event.values[1];

    /* *** first interval head angle degrees *** */
    int top_head_angle_limit = -110;
    int bot_head_angle_limit = -70;

    /* *** second interval *** */
    int top_list_item_limit = 0;
    int bot_list_item_limit = adapter.getCount();

    /* *** linear mapping/ map interval [-110,-70](head angle degrees) -> [0, 8] list items *** */
    /* *** linear mapping formula: (val - A)*(b-a)/(B-A) + a *** */
    if(pitch_angle >= top_head_angle_limit && pitch_angle <= bot_head_angle_limit){
        int selection = (int) ((pitch_angle - top_head_angle_limit)*(bot_list_item_limit - top_list_item_limit)/(bot_head_angle_limit - top_head_angle_limit) + top_list_item_limit);
        Log.e("selection: ", String.valueOf(selection));
        listView.setSelection(selection);
    }
}

有关更详细的代码实现,请参阅此Google的玻璃应用代码。

列表 查看:https://github.com/pscholl/glass_snippets/blob/master/lib/src/main/java/de/tud/ess/HeadListView.java

滚动视图:https://github.com/pscholl/glass_snippets/blob/master/lib/src/main/java/de/tud/ess/HeadScrollView.java

希望这会有所帮助!!