0

我有一个名为 current 的变量,它保存最后点击的列表视图项目数据。该数据的 id 将用于再次查询数据库以查找相似的电影。

current = id=17985, title=the matrix, year=1999 当我只需要实际的 id 号时的问题。我试图使用子字符串来只使用字符串的正确位置。这适用于矩阵,因为它的 id 正好是 5 位数长,但是一旦我尝试点击其 id 中位数较高/较低的电影,它当然会跳闸。

字符串 id = current.substring(4,9).trim().toString(); 不适用于所有电影。

继承人一些代码:

// 搜索方法:这是必要的,因为这是第一次使用的方法,并且设置了变量 current。这也是唯一使用三行获取数据的方法 - id、title 和 year。

 protected void search() {
        data = new ArrayList<Map<String, String>>();
        list = new ArrayList<String>();
        EditText searchstring = (EditText) findViewById(R.id.searchstring);
        String query = searchstring.getText().toString().replace(' ', '+');
        String text;

        text = searchquery(query);
        try {
            JSONObject res = new JSONObject(text);

            JSONArray jsonArray = res.getJSONArray("movies");

            for (int i = 0; i < jsonArray.length(); i++) {

                JSONObject jsonObject = jsonArray.getJSONObject(i);
                item = new HashMap<String, String>(2);
                item.put("id",jsonObject.getString("id"));
                item.put("title",jsonObject.getString("title"));
                item.put("year", jsonObject.getString("year"));
                data.add(item);

            }
        } catch (Exception e) {
            e.printStackTrace();
        }                   

        aa = new SimpleAdapter(SearchTab.this, data,
                R.layout.mylistview,
                new String[] {"title", "year"},
                new int[] {R.id.text1,
                R.id.text2});
        ListView lv = (ListView) findViewById(R.id.listView1);
        lv.setAdapter(aa);
        lv.setDividerHeight(5); 

        lv.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {

                Map<String, String> s = data.get((int) id);
                current = s.toString();
            }});
    }

// 类似的方法:这试图从“当前”字符串中获取的数据构建一个列表。如您所见,我尝试使用 4,9 来仅获得正确的数字,但其他人却失败了。最好的办法是只能获得我似乎做不到的 ID。toast 只是为了查看正在获取的内容,就像系统输出的一样。

 protected void similar() {
        data = new ArrayList<Map<String, String>>();
        list = new ArrayList<String>();
        String id = current.substring(4,9).trim().toString();
        Toast.makeText(getApplicationContext(), id, Toast.LENGTH_SHORT).show();
        String text;

        text = similarquery(id);
        try {
            JSONObject res = new JSONObject(text);

            JSONArray jsonArray = res.getJSONArray("movies");

            for (int i = 0; i < jsonArray.length(); i++) {

                JSONObject jsonObject = jsonArray.getJSONObject(i);
                item = new HashMap<String, String>(2);
                item.put("title",jsonObject.getString("title"));
                item.put("year", jsonObject.getString("year"));
                data.add(item);

            }
        } catch (Exception e) {
            e.printStackTrace();
        }                   

        aa = new SimpleAdapter(SearchTab.this, data,
                R.layout.mylistview2,
                new String[] {"title", "year"},
                new int[] {R.id.text1,
                R.id.text2});
        ListView lv = (ListView) findViewById(R.id.listView1);
        lv.setAdapter(aa);
        lv.setDividerHeight(5); 

    }
4

2 回答 2

1

如果您想通过子字符串解决它,那么您不应该使用预定义的.substring(4,9). 使用类似的东西:

String item = "id=17985, title=the matrix, year=1999";
String id = item.substring(item.indexOf("id=") + 3, item.indexOf(" ", item.indexOf("id=") + 3) - 1);
System.out.println("Your ID is: " + id);
// Works also for "test=asdf, id=17985, title=the matrix, year=1999"

它获取“id=”和下一个“”(空格)之间的字符串。

于 2012-10-23T19:36:00.423 回答
1

利用current.split(",")[0].split("=")[1]

编辑-当然假设 id 始终是逗号分隔列表中的第一个,并且始终是由 '=' 分隔的名称值对

于 2012-10-23T19:38:35.147 回答