-
Notifications
You must be signed in to change notification settings - Fork 617
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added GetListData action ("get_list_data").
Returns an array with all the items in the list adapter. See doc for usage sample.
- Loading branch information
Juan Delgado
committed
Oct 29, 2012
1 parent
a1fa0b7
commit b4d2091
Showing
1 changed file
with
85 additions
and
0 deletions.
There are no files selected for viewing
85 changes: 85 additions & 0 deletions
85
...nstrumentation-backend/src/sh/calaba/instrumentationbackend/actions/list/GetListData.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package sh.calaba.instrumentationbackend.actions.list; | ||
|
||
import java.util.ArrayList; | ||
|
||
import sh.calaba.instrumentationbackend.InstrumentationBackend; | ||
import sh.calaba.instrumentationbackend.Result; | ||
import sh.calaba.instrumentationbackend.actions.Action; | ||
import android.widget.ListView; | ||
|
||
/** | ||
* Provides access to a list through its adapter. | ||
* | ||
* args: | ||
* <ul> | ||
* <li>1-based list index (first list is used if not specified)</li> | ||
* </ul> | ||
* | ||
* eg: (all items of the 1st list) <code>performAction( 'get_list_data' )</code> | ||
* eg: (all items of the 2nd list) <code>performAction( 'get_list_data', 2 )</code> | ||
* | ||
* @return <code>bonusInformation</code>: a JSON-formatted Array<String> that is an | ||
* Array of objects with a single property "value". The value of this property is generated by calling | ||
* <code>list.getAdapter().getItem().toString()</code>, so make sure you implement the toString() method | ||
* on the objects that go to the adapter AND that it returns a value valid for JSON. | ||
* | ||
* In ruby we can then parse the response: | ||
* <pre> | ||
* result = performAction('get_list_data') | ||
* bonusInfo = result['bonusInformation'][0] | ||
* JSON.parse(bonusInfo).each do |item| | ||
* puts "#{item["value"]}" | ||
* end | ||
* </pre> | ||
* | ||
* @author Juan Delgado (juan@ustwo.co.uk) | ||
*/ | ||
|
||
public class GetListData implements Action { | ||
|
||
@Override | ||
public Result execute(String... args) { | ||
|
||
int listIndex; | ||
|
||
if( args.length == 0 ) { | ||
listIndex = 0; | ||
} else { | ||
listIndex = (Integer.parseInt(args[0]) - 1); | ||
} | ||
|
||
ArrayList<ListView> listViews = InstrumentationBackend.solo.getCurrentListViews(); | ||
|
||
if( listViews == null || listViews.size() <= listIndex ) { | ||
return new Result(false, "Could not find list #" + (listIndex + 1)); | ||
} | ||
|
||
ListView list = listViews.get(listIndex); | ||
Result result = new Result(true); | ||
|
||
StringBuilder json = new StringBuilder("["); | ||
|
||
int count = list.getAdapter().getCount(); | ||
for( int i = 0; i < count; i++ ) { | ||
json.append("{\"value\": \""); | ||
json.append(list.getAdapter().getItem(i).toString()); | ||
json.append("\"},"); | ||
} | ||
|
||
if(count > 0) | ||
{ | ||
json.deleteCharAt( json.length() - 1 ); // remove the last comma | ||
} | ||
|
||
json.append("]"); | ||
|
||
result.addBonusInformation(json.toString()); | ||
|
||
return result; | ||
} | ||
|
||
@Override | ||
public String key() { | ||
return "get_list_data"; | ||
} | ||
} |