简体   繁体   中英

Google Glass LiveCard update listview from Service

I have a Google Glass LiveCard application, where background service fetch data from remote URL.

I added TextView and ListView in LiveCard activity:

<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>

I am unable to refresh ListView data from Service as I do for TextView. For the TextView I use the method:

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

I the Activity onCreate method I set the Adapter as follows:

   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);
   }

Any suggestion ? Thanks in advance.

You can implement the head scrollable listview with the help of sensor manager. It calculates the angle at which your head is tilted and scrolls the listview likewise. This is a more convenient method in a glass app rather than having buttons or even voice commands.

Here is the code snippet for that:

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);
    }
}

For more detailed code implementations refer to this Google's glass app code.

For list View : https://github.com/pscholl/glass_snippets/blob/master/lib/src/main/java/de/tud/ess/HeadListView.java

For Scroll View : https://github.com/pscholl/glass_snippets/blob/master/lib/src/main/java/de/tud/ess/HeadScrollView.java

Hope this would Help!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM