如何从数组中获取特定数据

时间:2016-12-30 09:03:19

标签: java android arrays sqlite

我的问题是当我从sqlite数据库将数据存储到数组中时,如何从特定位置获取它,比方说,我的数据库包含“食物,饮料,零食”我如何从数组中获取字符串“snack”。

 String CatNameQuery = "SELECT * FROM Category";
    db = new DBController(MainActivity.this);
    SQLiteDatabase db3 = db.getReadableDatabase();
    final Cursor cursor2 = db3.rawQuery(CatNameQuery, null);
    {
        List<String> array = new ArrayList<String>();

        while(cursor2.moveToNext()){
            String uname = cursor2.getString(cursor2.getColumnIndex("CategoryName"));
            array.add(uname); 

    }

5 个答案:

答案 0 :(得分:0)

您需要遍历列表才能找到您要查找的项目。

例如:

for (String s : array) {
    if (s.equals("snack")) {
        System.out.println("Found snack");
    }
}

您还可以使用contains方法检查列表是否包含&#34; snack。&#34;

if (array.contains("snack")) {
    System.out.println("Found snack");
}

资源:ArrayList

答案 1 :(得分:0)

var response = {"html":"response header <!doctype html>MY Intrest is this HTML Block<html></html>"}

    function get_only_html(response){
        html_content = response.html;
        return html_content.slice(html_content.indexOf("<!doctype html>"));
    }

  console.log(get_only_html(response));

答案 2 :(得分:0)

在SELECT查询中使用WHERE子句。例如:

//get some data from db to array
while ($row = mysqli_fetch_assoc($query)){
   $data[] = $row;
}

//display unique value
echo "<table>";
$uniq = array();
foreach ($data as $row) {
    if(!in_array($row['path'], $uniq)) {
        $uniq[] = $row['path'];
        echo "<tr>";
        echo "<td>";
        echo $row['path'];
        echo "</td>";
        echo "<td>";
        echo $row['relationship_id'];
        echo "</td>";
        echo "</tr>";
    }
}
echo "</table>";

这将使您的数组仅填充“零食”类别下的项目。

答案 3 :(得分:0)

你可以通过循环数组找到它

<body>
    <div ng-app="myApp" ng-controller="customersCtrl">
        <table>
            <tr ng-repeat="x in names">
                <td>{{ x.name }}</td>
                <td>{{ x.city }}</td>
                               </tr>
        </table>
    </div>
    <script>
var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) {
    $http.get('https://example.com/abc', {
        headers: { 'Authorization': 'abc:abc', 'Access-Control-Allow-Origin': '*' }
    }).then(function (response) {$scope.names = response.data.records;});
});
    </script>
</body>

答案 4 :(得分:0)

if (array.size() > 0) {
    int index = 0;
    if (array.contains("Snacks")) {
        index = array.indexOf("Snacks");
        System.out.println(array.get(index));
    }
}