【问题标题】:How to convert multiple string into arraylist?如何将多个字符串转换为arraylist?
【发布时间】:2016-10-27 05:46:24
【问题描述】:

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

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

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

while (managedCursor.moveToNext()) {
    String number = managedCursor.getString(number1);
    String type2 = managedCursor.getString(type1);
    String date = managedCursor.getString(managedCursor.getColumnIndexOrThrow("date")).toString();
    java.util.Date date1 = new java.util.Date(Long.valueOf(date));
    String duration = managedCursor.getString(duration1);
    String type = null;

    String fDate = date1.toString();

    int callcode = Integer.parseInt(type2);

    sb.append("\nPhone Number:--- " + number + "");
    sb.append(" \nCall Type:--- " + type + " ");
    sb.append("\nCall Date:--- " + fDate + "");
    sb.append("\nCall duration in sec :--- " + duration);
    sb.append("\n----------------------------------");
}

【问题讨论】:

  • 如果这些数据不相关,您应该创建一个类来封装这些值,而不是ArrayList
  • 使用HashMap hashmap=new Map(); hashmap.put("数字",数字); hashmap.put("type",type); hashmap.put("日期",日期); hashmap.put("持续时间",持续时间);使用 key 很容易获得你的值。或使用模型类

标签: java android arrays string arraylist


【解决方案1】:

正如 4castle 所建议的那样,您确实应该有一个自定义类 - 查看您感兴趣的属性,您可能想要命名为“CallLogEntry”,然后为该类创建属性。这是此类自定义类的示例。

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 应用中,您可以稍后将此列表传递给某个列表适配器以获取列表视图。

我希望这会有所帮助。

【讨论】:

  • 感谢@ishmaelMakitla 的回复....我已经做好了arraylist 我不会将它插入mysql 数据库。
  • 好的,插入数据库超出了您当前问题的范围 - 我的建议是您要么接受其中一个答案(因此您的问题被标记为已回答),要么您将其删除。然后,您可以将数据插入数据库作为单独的问题。
【解决方案2】:

如下创建JavaBean类

public class DataBean {

    String number;
    String type;
    String date;
    String duration;

    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() {
        return number;
    }

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

    public String getType() {
        return type;
    }

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

    public String getDate() {
        return date;
    }

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

    public String getDuration() {
        return duration;
    }

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

现在按如下方式创建 Arryalist

ArrayList<DataBean> dataBeanList = new ArrayList<>();
DataBean dataBean=new DataBean(number,type,date,duration);
dataBeanList.add(dataBean);//added bean object arrylist

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

// 遍历arraylist如下

 for(DataBeand d: dataBeanList ){
        if(d.getName() != null && d.getDate()!=null)
           //something here
    }

【讨论】:

猜你喜欢
  • 2011-11-12
  • 1970-01-01
  • 2019-09-04
  • 2014-03-06
  • 2016-09-12
  • 2012-01-20
  • 2012-04-28
  • 2012-07-15
  • 2015-09-01
相关资源
最近更新 更多