1个2个不同JSONObjects的Android Listview

时间:2016-08-18 03:48:17

标签: android arrays json android-studio jsonobject

我有一个JSON文件,它看起来像这样。我想知道是否有可能在Android应用程序中为我的管理员用户创建1个单一视图,只需1页就可以查看短信日志的完整列表(fromSMS和toSMS)并在日期和时间中相应地进行排列?因为现在我使用2个不同的布局,2个不同的java类在两个不同的视图中显示toSMS和fromSMS(简而言之,管理员用户必须单击toSMS按钮查看所有toSMS详细信息,从SMS按钮查看所有fromSMS详细信息)。任何指导或建议将深表感谢。

{
  "fromSMS": [
    {
      "smsid": 46,
      "from_user": "User2",
      "to_user": "User3",
      "message": "What's up!",
      "date_time": "2015-12-23T02:12:00.000Z",
      "created_at": "2015-12-23T02:13:02.929Z",
      "updated_at": "2015-12-23T02:13:02.929Z"
    }
  ],
  "toSMS": [
    {
      "smsid": 39,
      "from_user": "User11",
      "to_user": "User22",
      "message": "Hihi",
      "date_time": "2015-12-23T01:31:00.000Z",
      "created_at": "2015-12-23T01:31:52.314Z",
      "updated_at": "2015-12-23T01:31:52.314Z"
    }
  ]
}

SMSHistoryAdapter.Java

public class SMSHistoryAdapter extends ArrayAdapter
{
    List list = new ArrayList();

    public SMSHistoryAdapter(Context context, int resource)
    {
        super(context,resource);
    }

    public void add(SMSHistory object)
    {
        super.add(object);
        list.add(object);
    }

    @Override
    public int getCount()
    {
        return list.size();
    }

    @Override
    public Object getItem(int position)
    {
        return list.get(getCount() - position - 1);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        View row;
        row = convertView;

        SMSHistoryHolder smsHistoryHolder;

        if(row == null)
        {
            LayoutInflater layoutInflater = (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = layoutInflater.inflate(R.layout.sms_history_rowlayout,parent,false);
            smsHistoryHolder = new SMSHistoryHolder();

            smsHistoryHolder.tAccount = (TextView)row.findViewById(R.id.tAccount);
            smsHistoryHolder.tMessage = (TextView)row.findViewById(R.id.tMessage);
            smsHistoryHolder.tDateTime = (TextView)row.findViewById(R.id.tDateTime);
            row.setTag(smsHistoryHolder);
        }
        else
        {
            smsHistoryHolder = (SMSHistoryHolder)row.getTag();
        }

        SMSHistory smsHistory = (SMSHistory)this.getItem(position);
        smsHistoryHolder.tAccount.setText(smsHistory.gettAccount());
        smsHistoryHolder.tMessage.setText(smsHistory.gettMessage());
        smsHistoryHolder.tDateTime.setText(smsHistory.gettDateTime());
        return row;
    }

    static class SMSHistoryHolder
    {
        TextView tAccount,tMessage,tDateTime;
    }
}

2 个答案:

答案 0 :(得分:0)

当然,在组合两个json数组后,请使用https://developer.android.com/reference/java/util/Collections.html#sort(java.util.List, java.util.Comparator)

Collections.sort(list, comparator)

按日期创建比较器,以Sort objects in ArrayList by date?

开头

答案 1 :(得分:0)

这可以通过多种方式解决, 1.使用一些标志创建新模型

Class SmsModel{
      String smsid;
      String from_user;
      String to_user;
      String Message;
      String date_time;
      String created_at;
      String updated_at;
      boolean fromSms;  //true for fromSMS and false for toSMS
}

你已经有一个arraylist只需用ArrayList修改它 并将其设置为适配器。

并在您的适配器内

@覆盖     public View getView(int position,View convertView,ViewGroup parent)     {         查看行;         row = convertView;

    SMSHistoryHolder smsHistoryHolder;

    if(row == null)
    {
        LayoutInflater layoutInflater = (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = layoutInflater.inflate(R.layout.sms_history_rowlayout,parent,false);
        smsHistoryHolder = new SMSHistoryHolder();

        smsHistoryHolder.tAccount = (TextView)row.findViewById(R.id.tAccount);
        smsHistoryHolder.tMessage = (TextView)row.findViewById(R.id.tMessage);
        smsHistoryHolder.tDateTime = (TextView)row.findViewById(R.id.tDateTime);
        row.setTag(smsHistoryHolder);
    }
    else
    {
        smsHistoryHolder = (SMSHistoryHolder)row.getTag();
    }

    SMSHistory smsHistory = (SMSHistory)this.getItem(position);
    smsHistoryHolder.tAccount.setText(smsHistory.gettAccount());
    smsHistoryHolder.tMessage.setText(smsHistory.gettMessage());
    smsHistoryHolder.tDateTime.setText(smsHistory.gettDateTime());
    if(smsHistory.getFromSms){
        // - fromSms
    } else {
        // - toSms
    }
    return row;
}