【问题标题】:ArrayList convert to StringArrayList 转换为字符串
【发布时间】:2014-07-30 08:55:15
【问题描述】:

我正在尝试在我的 android 应用程序中创建一个搜索字段。我需要将 ArrayList 转换为 String 数组以显示值和搜索。我有:

 inputSearch = (EditText) findViewById (R.id.searchText);
        myAdapter = new ArrayAdapter<String>(this, R.layout.plates, R.id.title, mPlatesList);
        lv.setAdapter(myAdapter);

        inputSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
                Lista.this.myAdapter.getFilter().filter(cs);
            }
            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                                          int arg3) {
            }
            @Override
            public void afterTextChanged(Editable arg0) {
            }
        });

mPlatesList = new ArrayList<HashMap<String, String>>();
mPlates = json.getJSONArray(TAG_POSTS);

            // looping through all posts according to the json object returned
            for (int i = 0; i < mPlates.length(); i++) {
                JSONObject c = mPlates.getJSONObject(i);

                // gets the content of each tag
                String targa = c.getString(TAG_TARGA);

                // creating new HashMap
                HashMap<String, String> map = new HashMap<String, String>();

                map.put(TAG_TARGA, targa);
                // adding HashList to ArrayList
                mPlatesList.add(map);

我收到上述错误。我是 android 新手,所以我不知道如何从一种类型转换为另一种类型。

  Error:(32, 38) error: no suitable constructor found for ArrayAdapter(Lista,int,int,ArrayList<HashMap<String,String>>)
        constructor ArrayAdapter.ArrayAdapter(Context,int,int,String[]) is not applicable
        (argument mismatch; ArrayList<HashMap<String,String>> cannot be converted to String[])
        constructor ArrayAdapter.ArrayAdapter(Context,int,int,List<String>) is not applicable
        (argument mismatch; ArrayList<HashMap<String,String>> cannot be converted to List<String>)

【问题讨论】:

  • 如果您需要 ArrayList 和 HashMap,那么您已经为您的列表制作了自定义适配器。
  • 在这里查看我的答案:stackoverflow.com/questions/24954285/…
  • 你能指定你想要的字符串数组吗?我的意思是 ArrayList 是 hashmap。你想要键或值的字符串数组?
  • 那么您究竟想从该数据结构中显示什么?
  • @Preethi Rao 我想要价值观。执行搜索

标签: java android arraylist android-studio


【解决方案1】:

您可以通过调用 toArray 方法将 ArrayList 转换为 String[]:

ArrayList<String> list = new ArrayList<String>();
String [] table = list.toArray(new String[list.size()]);

在您的示例中,您没有任何ArrayList&lt;String&gt;。这是一个哈希图列表。我真的不知道你最后想把哪些字符串放在表中。它是所有hashmaps值集的联合吗?

编辑:好的。然后,您可以通过这样做来构建一个集合,它是所有值集合的并集:

Set<String> unionSet = new HashSet<String>();
for(HashMap<String, String> hashMap : mPlatesList){
    unionSet.addAll(hashMap.values());
}

然后,您可以从您的 Set 中创建一个数组:

String [] table = unionSet.toArray(new String[unionSet.size()]);

EDIT2:将Map&lt;String, String&gt; hashMap 更改为HashMap&lt;String, String&gt; hashMap EDIT3:改为数组调用

EDIT4:这是一个完整的例子:

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

public class Test {

    /**
     * @param args
     */
    public static void main(String[] args) {

        // instantiate mPlatesList
        ArrayList<HashMap<String, String>> mPlatesList = new ArrayList<HashMap<String, String>>();

        // fill it with 3 HashMaps
        mPlatesList.add(new HashMap<String, String>());
        mPlatesList.add(new HashMap<String, String>());
        mPlatesList.add(new HashMap<String, String>());

        // put some values in the 3 HashMaps
        mPlatesList.get(0).put("Key1", "Value1");
        mPlatesList.get(0).put("Key2", "Value2");
        mPlatesList.get(1).put("Key3", "Value3");
        mPlatesList.get(2).put("Key4", "Value4");

        // build the table from mPlatesList
        Set<String> unionSet = new HashSet<String>();
        for (HashMap<String, String> hashMap : mPlatesList) {
            unionSet.addAll(hashMap.values());
        }
        String[] table = unionSet.toArray(new String[unionSet.size()]);

        // print the table
        for(int i=0; i<table.length; i++)
            System.out.println(table[i]);

    }

}

运行 main 方法会产生以下显示:

Value3
Value4
Value1
Value2

编辑 5:如果您只想保留键为 TAG_TARGA 的值,则将 // build the table from mPlateList 块替换为:

    // build the table from mPlatesList
    Set<String> unionSet = new HashSet<String>();
    for (HashMap<String, String> hashMap : mPlatesList) {
        for(String key : hashMap.keySet())
            if(key.equals(TAG_TARGA))
                unionSet.add(hashMap.get(key));
    }
    String[] table = unionSet.toArray(new String[unionSet.size()]);

【讨论】:

  • 我想要这些值。如何将值转换为字符串数组?
  • 有什么我想要的答案吗?
  • 为此应该包含哪些库?因为它显示为红色
  • 此行字符串 [] table = unionSet.toArray();显示需要的不兼容类型 java.lang.String[] 返回 java.lang.Object[]
  • 参见 EDIT 5 以仅过滤带有键 TAG_TARGA 的值
【解决方案2】:

您可以创建一些 DTO 来代替 HashMap。

【讨论】:

    猜你喜欢
    • 2019-09-04
    • 2012-04-28
    • 1970-01-01
    • 2011-09-13
    • 2015-07-22
    • 2014-09-19
    • 2014-05-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多