应用程序不从Azure移动服务表中检索数据

时间:2015-12-17 23:53:03

标签: java android azure azure-mobile-services

我的Android应用程序无法从我的移动服务中的表中获取数据。

它使用MobileserviceClient连接到azure移动服务服务器,没有任何错误。但是,当我试图获取一张桌子并从该表中取出数据时,该应用程序在该第一行冻结并给我一个白色屏幕。

所以一些帮助会很好,因为我不知道我做错了什么。此外,restaurantdata表中已有50行数据。

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 个答案:

答案 0 :(得分:0)

根据您提供的代码段,您似乎只是通过list = table.execute().get()获取数据,但不会将数据显示给视图。

通常,我们可以利用list viewadapter在列表中显示我们的数据。

以下是Azure移动服务Android客户端示例的代码段:

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

}

定义变量:

private ToDoItemAdapter mAdapter;


mAdapter = new ToDoItemAdapter(this, R.layout.row_list_to_do);
ListView listViewToDo = (ListView) findViewById(R.id.listViewToDo);
listViewToDo.setAdapter(mAdapter);

在listview中添加列表项

for (ToDoItem item : list) {
    mAdapter.add(item);
}