ArrayList转换为String

时间:2014-07-30 08:55:15

标签: java android arraylist android-studio

我正在尝试在我的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>)

2 个答案:

答案 0 :(得分:0)

您可以创建一些DTO而不是HashMap。

答案 1 :(得分:0)

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

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

在您的示例中,您没有ArrayList<String>。这是一个哈希映射列表。我真的不知道你想在表格中放入哪些字符串。它是所有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<String, String> hashMap更改为HashMap<String, String> 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()]);