【问题标题】:Converting from simpleAdapter to ArrayAdapter从 simpleAdapter 转换为 ArrayAdapter
【发布时间】:2013-03-09 09:53:40
【问题描述】:

我已经为自己构建了一个包含列表的应用程序。到目前为止,我一直使用SimpleAdapter 来填充列表,但我决定改用ArrayAdapter。问题是,我不知道如何以同样的方式填充ArrayAdapter!这是我使用SimpleAdapter的方式:

adapter=new SimpleAdapter(this, listItems, R.layout.custom_row_view, new String[]{"name", "current", "reset"},  new int[] {R.id.text1, R.id.text2, R.id.text3});

我的 listItems 变量实际上是这样设置的:

static final ArrayList<HashMap<String,String>> listItems = new ArrayList<HashMap<String,String>>();

现在,当尝试使用具有相同参数的 ArrayAdapter 构造函数时,它给了我一个错误。怎么可能做到这一点?

【问题讨论】:

    标签: android android-listview android-arrayadapter simpleadapter


    【解决方案1】:

    现在,当尝试使用相同的 ArrayAdapter 构造函数时 参数,它给了我一个错误。

    这是因为 ArrayAdapter 是为非常简单的场景设计的,其中数据是列表/数组的形式,只有一个小部件(通常是 TextView)要绑定到。由于您的布局中有三个小部件,您需要扩展 ArrayAdapter 类来绑定您的数据,因为默认实现无法单独执行此操作,如下所示:

    listView.setAdapter(
                new ArrayAdapter<HashMap<String, String>>(this, R.layout.custom_row_view,
                        R.id.text1, listItems) {
    
                            @Override
                            public View getView(int position, View convertView,
                                    ViewGroup parent) {
                                View rowView = super.getView(position, convertView, parent);
                                final HashMap<String, String> item = getItem(position);
                                TextView firstText = (TextView) rowView.findViewById(R.id.text1);
                                firstText.setText(item.get("corresponding_key"));
                                TextView secondText = (TextView) rowView.findViewById(R.id.text2);
                                secondText.setText(item.get("corresponding_key"));
                                TextView thirdText = (TextView) rowView.findViewById(R.id.text3);
                                thirdText.setText(item.get("corresponding_key"));
                                return rowView;
                            }
    
                });
    

    但最后还有一个问题,当SimpleAdapter更适合你的场景时,你为什么要使用ArrayAdapter

    【讨论】:

    • stackoverflow 上可能至少有 50 个问题,询问如何更改列表视图中的某些数据,但答案很少。您对这个问题的回答也回答了这 50 个问题。
    【解决方案2】:

    你应该像这样定义一个 ArrayAdapter:

    ListView listView = (ListView) findViewById(R.id.mylist);
    String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
      "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
      "Linux", "OS/2" };
    
    // Define a new Adapter
    // First parameter - Context
    // Second parameter - Layout for the row
    // Third parameter - ID of the TextView to which the data is written
    // Fourth - the Array of data
    
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
      android.R.layout.simple_list_item_1, android.R.id.text1, values);
    
    
    // Assign adapter to ListView
    listView.setAdapter(adapter); 
    

    【讨论】:

    • 我知道这一点,但正如您在上面看到的那样,我有自己的布局,并且不止一个R.id.text$。请查看我的更新,因为我的值也存储在 ArrayList&lt;HashMap&lt;String,String&gt;&gt; 中。
    • 啊,我明白了。我不认为你可以使用这样的 ArrayAdapter。您必须实现从 BaseAdapter 派生的自定义适配器。如果需要列,为什么不直接使用 GridView?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多