如何在android中打印对象列表

时间:2014-04-04 13:44:38

标签: android listview object printing

我正在使用此代码显示我的应用程序中的未接来电,而我每次使用Call类定义对象然后将其存储在测试中如何在应用程序中打印测试列表中的所有对象?

 List<Call> callList = new ArrayList<Call>();
        while (c.moveToNext()) {
            Call call = new Call();
            call.setNumber(c.getString(c.getColumnIndex(CallLog.Calls.NUMBER)));
           /* if (c.getInt(c.getColumnIndex(CallLog.Calls.TYPE)) == CallLog.Calls.OUTGOING_TYPE) {
                call.setOut(true);
            } else {
                call.setOut(false);
            }*/

            call.setName(c.getString(c.getColumnIndex(CallLog.Calls.CACHED_NAME)));
            call.setTime(c.getLong(c.getColumnIndex(CallLog.Calls.DATE)));
            call.setDuration(c.getLong(c.getColumnIndex(CallLog.Calls.DURATION)));

            if ( c.getString(c.getColumnIndex(CallLog.Calls.CACHED_NAME))!= null) 
            {
                continue;
            }

            callList.add(call);
        }
        c.close();
        }

3 个答案:

答案 0 :(得分:4)

您可以在toString()类上覆盖Call方法,例如:

    @Override
    public String toString() {

        return Name+" "+Time+" "+Duration;
    }

并在for语句中打印您的列表,如:

for (int i = 0 ; i < callList.size() ; i++)
    Log.d("value is" , callList.get(i).toString()); 

答案 1 :(得分:2)

您可以遍历列表并打印元素:

List<Call> callList = new ArrayList<Call>();
     for(Call call : callList){
        System.out.print("Name - " +call.getName() +
        "Number - "+call.getNumber() +
        "Time - "+call.getTime() +
        "Duration - "call.getDuration());
     }

答案 2 :(得分:0)

您必须像这样在Call类中覆盖toString

 @Override
  public String toString() {
    return "Call{" +
      "Name=" + Name+
      ", Time='" + Time+ '\'' +
      ", Duration='" + Duration+ '\'' +
      '}';
  }

然后使用它

System.out.println("callList: " +Arrays.toString(callList.toArray()));