如何将多个字符串转换为arraylist?

时间:2016-10-27 05:46:24

标签: java android arrays string arraylist

我是android的新手。我想将这些String变量添加到ArrayList,我现在在做什么。 假设我有四个字符串变量.....

{[{"unit_id":null,"unit_code":"m","unit_name":"meter",audit_user_id":None,"audit_ts":null},[]]}

这是我的java代码.......

String number = "9876543211";
String type = "incoming";
String date = "1/1/2016";
String duration = "45 sec";

2 个答案:

答案 0 :(得分:0)

按如下方式创建JavaBean类




 公共类DataBean {

字符串编号;
字符串类型;
字符串日期;
字符串持续时间;

 public DataBean(String number,String type,String date,String duration){
 this.number = number;
 this.type = type;
 this.date = date;
 this.duration = duration;
 }

 public String getNumber(){
返回号码;
 }

 public void setNumber(String number){
 this.number = number;
 }

 public String getType(){
返回类型;
 }

 public void setType(String type){
 this.type = type;
 }

 public String getDate(){
返回日期;
 }

 public void setDate(String date){
 this.date = date;
 }

 public String getDuration(){
返回时间;
 }

 public void setDuration(String duration){
 this.duration = duration;
 }
}
  




现在按如下方式创建Arryalist


& #xA;
 <代码>的ArrayList&LT;&公司Databean GT; dataBeanList = new ArrayList&lt;&gt;();&#xA; DataBean dataBean = new DataBean(number,type,date,duration);&#xA; dataBeanList.add(dataBean); //添加bean对象arrylist&#xA; < / code> 
&#xA;&#xA;

对于检索数据,请执行以下操作

&#xA;&#xA;

//迭代arraylist如下

&#xA;&#xA;
  for(DataBeand d:dataBeanList){&#xA; if(d.getName()!= null&amp;&amp; d.getDate()!= null)&#xA; //这里有什么&#xA; }&#XA;  
&#XA;

答案 1 :(得分:0)

正如4castle建议的那样,你应该有一个自定义类 - 查看你感兴趣的属性,你可能想要命名&#34; CallLogEntry&#34;然后拥有此类的属性。以下是此类自定义类的示例。

public class CallLogEntry {
  String number;
  String type;
  String date;
  String duration; //this should ideally be of type Long (milliseconds)  

      public CallLogEntry(){
       //do stuff if necessary for no-params constructor
      }

      public CallLogEntry(String number, String type, String date, String duration){
        this.number = number;
        this.type = type;
        this.date = date;
        this.duration = duration;
      }

      //getters and setters go here..
      getNumber(){ return this.number;}
      setNumber(String number){ this.number = number;}
      //...the rest of them...
    }

有一个像这样的CallHistoryEntry项的ArrayList更有意义:

//...declare list of call-log-entries:
List<CallLogEntry> callLogEntryList = new  ArrayList<CallLogEntry>();
//...add entries 
CallLogEntry entry = new CallLogEntry(number, type, date, duration);
callLogEntryList.add(entry);

这样做的好处,特别是在Android应用程序中,以后您可以将此列表传递给某个列表适配器以获取列表视图。

我希望这会有所帮助。