将文本保存到Azure的列表中

时间:2016-03-22 19:03:49

标签: java android azure android-studio

我已经创建了一个应用程序,用户将文本输入到文本框中,一旦单击按钮,该文本框就会被发送到azure。用户单击按钮后,他们将进入单独的java活动,其中文​​本框中的文本将添加到列表框中。但是,一旦我关闭应用程序或更改屏幕,即使文本保存在Azure中,文本也不会永久保存到列表框中。任何人都可以帮助我永久地将文本从azure添加到我的列表框中。列表框位于我的ListDeadlines活动中。

我的代码就像休耕一样

AddDeadline Activity

 private MobileServiceClient mClient;
private EditText title;
public EditText editText;
public EditText editDate;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add_deadline);

    try {
        mClient = new MobileServiceClient(
                "https://craigsapp.azure-mobile.net/",
                "BTkcgnFQvevAdmmRteHCmhHPzdGydq84",
                this
        );
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }
    editText = (EditText)findViewById(R.id.editText2);
    editDate = (EditText)findViewById(R.id.editText3);

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
   final ListDeadlines lst= new ListDeadlines();

    lst.txtInput=(EditText)findViewById(R.id.txtinput);

    ImageView btAdd=(ImageView)findViewById(R.id.btnAdd);

    btAdd.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            title= (EditText) findViewById(R.id.txtinput);

            Item item = new Item();
            item.Text = title.getText().toString();
            Intent returnIntent = getIntent();
            returnIntent.putExtra("result", item.Text = title.getText().toString());
            setResult(RESULT_OK, returnIntent);
            mClient.getTable(Item.class).insert(item, new TableOperationCallback<Item>() {
                public void onCompleted(Item entity, Exception exception, ServiceFilterResponse response) {
                    if (exception == null) {
                        // Insert succeeded
                    } else {
                        // Insert failed
                    }
                }
            });
            finish();
        }
    });

ListDeadline活动

public ArrayList<String> arrayList;
public ArrayAdapter<String> adapter;
public ArrayAdapter<String> newad;
public EditText txtInput;
public EditText txtDate;
public int ADD_DEADLINE_REQUEST=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_list_deadlines);
    final ListView listView = (ListView)findViewById(R.id.listv);
    String[] items= {""};
    arrayList=new ArrayList<>(Arrays.asList(items));
    adapter=new ArrayAdapter<String>(this,R.layout.list_item,R.id.txtitem,arrayList);
    listView.setAdapter(adapter);

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);



}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    Item newitem = new Item();
    if (requestCode == ADD_DEADLINE_REQUEST){
        if (resultCode == RESULT_OK) {
            String item = data.getStringExtra("result");
            arrayList.add(item);
            adapter.notifyDataSetChanged();
            Toast toast = Toast.makeText(getApplicationContext(),
                    "Deadline has been added", Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL, 100, 0);
            toast.show();
        }
    }
}

2 个答案:

答案 0 :(得分:0)

您必须查询azure文本,然后填充列表框。或者,如果您想在本地保存文本,则可以使用共享首选项来执行此操作。

答案 1 :(得分:0)

正如@pooja所说,您可以尝试通过查询Azure Mobile Service获取文本或从本地存储获取文本。但是,为了在本地和云之间保持一致,您需要将离线数据同步添加到Android移动服务应用,请参阅官方文档https://azure.microsoft.com/en-us/documentation/articles/mobile-services-android-get-started-offline-data/

下面有两个演示视频。

  1. https://channel9.msdn.com/Shows/Cloud+Cover/Episode-155-Offline-Storage-with-Donna-Malayeri
  2. http://azure.microsoft.com/documentation/videos/azure-mobile-services-offline-enabled-apps-with-donna-malayeri/(对于Windows,但功能讨论适用于所有平台。)
  3. 您可以参考GitHub上的离线数据同步的Android示例,请参阅https://github.com/Azure/mobile-services-samples/tree/master/TodoOffline/Android