【问题标题】:SimpleAdapter on HashMap does not work properlyHashMap 上的 SimpleAdapter 无法正常工作
【发布时间】:2015-02-17 10:46:44
【问题描述】:

我有一个这样的 JSON:

 [{"message":"Hello","id":1,"name":"Hello"},{"message":"James","id":2,"name":"Smith"}]

我用来填充ArrayList<HashMap<String, String>> 以便将json 的项目显示到ListView 中。
这是我目前使用的代码:

   private void createListView(){
        for(int i = 0 ; i < json.length() ; i++){
            try{
                JSONObject temp = json.getJSONObject(i);
                HashMap<String, String> map = new HashMap<>();
                map.put("name", temp.getString("name"));
                map.put("message", temp.getString("message"));
                data.add(map);
            }catch (JSONException e){
                e.printStackTrace();
            }
        }

        String[] from = new String[]{"name", "message"};
        int to[] = new int[]{android.R.id.text1};

        ListAdapter adapter = new SimpleAdapter(getBaseContext(), data, android.R.layout.simple_list_item_1, from, to);
        setListAdapter(adapter);
        Log.d("Debug", json.toString());

    }

这段代码几乎可以工作,但不是我想要的方式。它仅显示 name 字段。

如何修改它以同时显示 messagename 字段?

【问题讨论】:

  • 它只显示名称字段,因为你给它显示 2 个项目并且你只声明 1 个位置来显示...

标签: java android json android-listview simpleadapter


【解决方案1】:

它只显示消息字段。

因为使用 android.R.layout.simple_list_item_1 的行只包含在具有 android.R.id.text1 id 的 TextView 上

如何修改它以同时显示消息和名称 领域?

使用android.R.layout.simple_list_item_2 作为行的布局并更改to 数组:

int to[] = new int[]{android.R.id.text1,android.R.id.text2};

【讨论】:

  • 谢谢!像魅力一样工作:)
【解决方案2】:

改变这一行

int to[] = new int[]{android.R.id.text1};

int to[] = new int[]{android.R.id.text1,android.R.id.text2};

希望对你有帮助。

【讨论】:

    【解决方案3】:

    这一行出错

    int to[] = new int[]{android.R.id.text1};
    

    这里你只提到了一个控件。我想那是为了消息。您需要为Name 添加另一个控件。

    【讨论】:

      【解决方案4】:

      使用“android.R.layout.simple_list_item_2”而不是“android.R.layout.simple_list_item_1”

      ListAdapter adapter = new SimpleCursorAdapter(
                       this,
                       android.R.layout.simple_list_item_2,
                       mCursor,     // Pass in the cursor to bind to.
                       new String[] {People.NAME, People.COMPANY}, // Array of cursor columns to bind to.
                       new int[] {android.R.id.text1, android.R.id.text2});  // Parallel array of which template objects to bind to those columns.
      
               // Bind to our new adapter.
               setListAdapter(adapter);
      

      【讨论】:

        【解决方案5】:
        SimpleAdapter playlistadapter = new SimpleAdapter(getActivity().getApplicationContext(), songsList, R.layout.file_view,
                        new String[] { "songTitle","songAlbum", "songartist","songPath" }, new int[] { R.id.checkTextView, R.id.text2, R.id.text3, R.id.text4 });
        
        playlistadapter.setViewBinder(new SimpleAdapter.ViewBinder(){
            boolean setViewValue(View view, Object data, String textRepresentation){
                    if(view.getId () == R.id.songartist){
                        view.setText("("+textRepresentation+")");
                    }
            }
        });
        

        【讨论】:

          猜你喜欢
          • 2014-05-13
          • 1970-01-01
          • 2016-12-01
          • 2014-10-23
          • 2017-07-15
          • 2014-01-04
          • 2013-08-06
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多