项目未添加或更新到ListView

时间:2018-02-12 19:05:33

标签: android azure listview

我当前的问题是我存储在数据库中的项目未加载到列表视图中。我正在使用带有java的android studio,我也使用带有SQL语句的Azure数据库作为我的查询方法。 Azure无法使用特定命令来撤消信息。

/**
     * Refresh the inventory list with the items in the Table
     */
    private void refreshItemsToInventoryList() {

        // Get the items that weren't marked as completed and add them in the
        // adapter

        AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>(){
            @Override
            protected Void doInBackground(Void... params) {

                try {
                    final List<Item> results = refreshItemsFromInventoryTable();

                    //Offline Sync
                    //final List<ToDoItem> results = refreshItemsFromMobileServiceTableSyncTable();

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            mInventoryAdapter.clear();

                            for (Item item : results) {
                                mInventoryAdapter.add(item);
                            }
                        }
                    });
                } catch (final Exception e){
                    Log.e("Error: ", e.getMessage());
                }

                return null;
            }
        };

        runAsyncTask(task);
    }

    /**
     * Refresh the list with the items in the Inventory Table
     */

    @Nullable
    private List<Item> refreshItemsFromInventoryTable() throws ExecutionException, InterruptedException {

        ResultSet rs;
        List<Item> inventoryItems = null;
        try {
            con = connectionclass(username, password);
            String query = "SELECT * FROM Inventory; ";
            Statement stmt = con.createStatement();
            rs = stmt.executeQuery(query);

            while (rs.next()) {
                inventoryItems.add(new Item(rs.getString("ItemDescription")));
            }
        }
        catch (SQLException sql)
        {
            createAndShowDialog(sql.getMessage(), "Error");
        }

        return inventoryItems;
                //.where()
                //.execute()
               // .get();
    }

任何人都可以找出为什么我的商品没有加载到我的列表中以及为什么它们没有出现在列表视图中?请记住,我正在使用Android Studio。

1 个答案:

答案 0 :(得分:1)

完成添加新数据后,您需要在适配器上调用if u'Brazil' in results[1]['data']: ,否则您的列表视图不会反映新的更改

public enum DelimitedFileHeaderOptions
{
    IncludeHeader,
    DoNotIncludeHeader
}

public class DelimitedFileResult<T> : FileResult where T : class
{
    private const String DefaultFileName = "file.csv";
    private const String DefaultContentType = "text/csv";
    private const String DefaultDelimiter = ",";

    public DelimitedFileResult(IEnumerable<T> data, DelimitedFileHeaderOptions headerOption)
        : this(data, DefaultDelimiter, DefaultFileName, DefaultContentType, headerOption) { }

    public DelimitedFileResult(IEnumerable<T> data, String delimiter = DefaultDelimiter, String fileName = DefaultFileName, String contentType = DefaultContentType,
        DelimitedFileHeaderOptions headerOption = DelimitedFileHeaderOptions.IncludeHeader)
        : base(contentType)
    {
        _data = data;
        _delimiter = delimiter;
        FileDownloadName = fileName;
        _headerOption = headerOption;
    }

    protected override void WriteFile(HttpResponseBase response)
    {
        var builder = new StringBuilder();

        var properties = (from p in typeof (T).GetProperties() select p).ToList();
        var propertyNames = (from p in properties select p.GetDisplayName()).ToList();

        if (_headerOption == DelimitedFileHeaderOptions.IncludeHeader)
            builder.AppendLine(String.Join(_delimiter, propertyNames));

        foreach (T item in _data)
        {
            foreach (var property in properties)
            {
                builder.Append(property.GetValue(item, null));
                builder.Append(_delimiter);
            }

            builder.AppendLine();
        }

        response.Write(builder.ToString());
    }

    private readonly IEnumerable<T> _data;
    private readonly String _delimiter;
    private readonly DelimitedFileHeaderOptions _headerOption;
}