问题
Well my android app has trouble getting data from a table in my mobile service.
It connects to the azure mobile service server using the MobileserviceClient with no errors what so ever. But when i try to get a table and take the data out of that table that app freezes on that 1 line and gives me a white screen.
So some help would be great cause I have no clue what I am doing wrong. Also there is already 50 rows of data in the restaurantdata table.
private MobileServiceClient mClient;
private MobileServiceTable<RestaurantData> table;
private MobileServiceList<RestaurantData> list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.activityMainToolbar);
setSupportActionBar(toolbar);
coordinatorLayout = (CoordinatorLayout) findViewById(R.id.activityMainCoordinatorLayout);
viewGroup = (ViewGroup) ((ViewGroup) this.findViewById(android.R.id.content)).getChildAt(0);
layoutInflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = layoutInflater.inflate(R.layout.activity_new_restaurants, viewGroup, false);
coordinatorLayout.addView(view);
try {
Log.e("Log", "Log1");
mClient = new MobileServiceClient("Service doamin", "application key here", getApplicationContext());
Log.e("Log", "Log2");
table = mClient.getTable(RestaurantData.class);
Log.e("Log", "Log3");
list = table.execute().get();//App freezes here with white screen
Log.e("Log", "Log4");
} catch (Exception e) {
e.printStackTrace();
Log.e("ERROR2", " " + e);
}
Log.e("Log", "Log6");
}
回答1:
Per your code snippet you provided, it seems you just get the data via list = table.execute().get()
but do not present your data to the view.
Usually, we can leverage list view and adapter to present our data in list.
Here are the code snippet on Azure Mobile Service Android Client sample:
public class ToDoItemAdapter extends ArrayAdapter<ToDoItem> {
/**
* Adapter context
*/
Context mContext;
/**
* Adapter View layout
*/
int mLayoutResourceId;
public ToDoItemAdapter(Context context, int layoutResourceId) {
super(context, layoutResourceId);
mContext = context;
mLayoutResourceId = layoutResourceId;
}
/**
* Returns the view for a specific item on the list
*/
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
final ToDoItem currentItem = getItem(position);
if (row == null) {
LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
row = inflater.inflate(mLayoutResourceId, parent, false);
}
row.setTag(currentItem);
return row;
}
}
define variables :
private ToDoItemAdapter mAdapter;
mAdapter = new ToDoItemAdapter(this, R.layout.row_list_to_do);
ListView listViewToDo = (ListView) findViewById(R.id.listViewToDo);
listViewToDo.setAdapter(mAdapter);
add list item in listview:
for (ToDoItem item : list) {
mAdapter.add(item);
}
来源:https://stackoverflow.com/questions/34346425/app-not-retrieving-data-from-azure-mobile-service-table