【问题标题】:Converting string to ArrayList<String>将字符串转换为 ArrayList<String>
【发布时间】:2014-05-16 21:18:42
【问题描述】:


我知道这是一个奇怪的问题,但我需要一件事:我收到一个字符串作为 http 操作的结果。实际上,这个字符串包含一个字符串数组,如下所示:

["Leon","Prova 2","TestData"]


基本上我想将此字符串转换为 ArrayList,因为我需要它作为适配器。这是java代码:

public class Archivio{

    static ArrayList<String> titoli = new ArrayList<String>();
    static ArrayList<String> art = new ArrayList<String>();
    static String response, response2 = null;
    HttpClient httpclient;
     HttpPost httppost,httppost2;
     List<NameValuePair> nameValuePairs,namePairs;
    @SuppressLint({ "NewApi", "NewApi", "NewApi" })
    Archivio() {

        try {

            StrictMode.ThreadPolicy policy = new
                    StrictMode.ThreadPolicy.Builder()
                    .permitAll().build();
                    StrictMode.setThreadPolicy(policy);

                    httpclient=new DefaultHttpClient();
                     httppost= new HttpPost("http://tripleleon.altervista.org/artiview2.php"); 
                     nameValuePairs = new ArrayList<NameValuePair>();
                      httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                     //Esecuzione richiesta HTTP Post e ricezione risultato operazione
                     //response=httpclient.execute(httppost);
                     ResponseHandler<String> responseHandler = new BasicResponseHandler();
                     response = httpclient.execute(httppost, responseHandler);
                     System.out.println("Response : " + response); 
                     httppost2= new HttpPost("http://tripleleon.altervista.org/artiview3.php"); 
                     namePairs = new ArrayList<NameValuePair>();
                      httppost2.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                     ResponseHandler<String> responseHandler2 = new BasicResponseHandler();
                     response2 = httpclient.execute(httppost2, responseHandler2);
                     System.out.println("Response : " + response2); 

        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    ArrayList<String> retTitle() {
        return response;
    }


    ArrayList<String> retArt() {
        return response2;   
    }
}

【问题讨论】:

  • 你不应该从主线程做网络操作。
  • 正如我在另一条评论中所说:这是一个为测试而设计的课程。我在实际项目中使用线程

标签: android string arraylist


【解决方案1】:

尝试使用以下代码(作为包含“数组”的stringArrayString):

ArrayList<String> myList = new ArrayList<String>(Arrays.asList(stringArray.substring(1, stringArray.length() - 1).replaceAll("\"", "").split(",")));

这将删除第一个和最后一个字符(分别为[])并由, 字符分割。然后,使用Arrays.asList 方法将其转换为ArrayList

【讨论】:

  • "类型不匹配:无法从 List 转换为 ArrayList"
  • 只需将其包装成new ArrayList&lt;String&gt;(...) 语句(更新我的答案)。
【解决方案2】:

您可以循环访问JSONArray 并将其添加到ArrayList

ArrayList<String> list = new ArrayList<String>();
JSONArray jr = new JSONArray("string");
for(int i=0;i<jr.length();i++)
{
   String value =(String) jr.get(i);
   list.add(value); 
}

但是您没有使用 ThreadAsynctask 从服务器获取 json。不要在生产阶段使用StrictMode

还要检查清单 @SuppressLint({ "NewApi", "NewApi", "NewApi" }) 中的 minsdk。您已禁止 lint 警告

【讨论】:

  • 这是一个为各种测试而设的临时类。当然我在实际项目中使用线程;)
猜你喜欢
  • 2013-11-19
  • 2021-07-12
  • 2023-03-06
  • 2013-08-14
  • 2012-10-04
  • 1970-01-01
  • 2011-03-28
  • 2019-09-04
  • 1970-01-01
相关资源
最近更新 更多