0

I have a JSON file and it looks something like this. I was wondering if it is possible to create 1 single view for my admin users in the android app to view the full list of sms logs(fromSMS and toSMS) in just 1 page and arrange it accordingly in the date and time? Because for now I am using 2 different layouts, 2 different java classes to show toSMS and fromSMS in two different views(in short the admin user have to click toSMS button to view all toSMS details, fromSMS button to view all fromSMS details). Any guidance or advice will be deeply appreciated.

{
  "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;
    }
}
iOSAndroid
  • 17
  • 1
  • 11

2 Answers2

0

Sure, after you combine your two json arrays, use https://developer.android.com/reference/java/util/Collections.html#sort(java.util.List, java.util.Comparator)

Collections.sort(list, comparator)

Create a comparator by date, start with Sort objects in ArrayList by date?

Community
  • 1
  • 1
TWL
  • 6,228
  • 29
  • 65
0

This can be solved using multiple ways, 1. create new Model using some flag

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
}

and you already have a arraylist just modify it with ArrayList and set it to adapter.

and inside your adapter

@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());
    if(smsHistory.getFromSms){
        // - fromSms
    } else {
        // - toSms
    }
    return row;
}
Rahul
  • 1,380
  • 1
  • 10
  • 24
  • Hi Rah, I don't quite get this, so we just create a new class at my Java class then do a boolean? Is there any guides for this? I am still quite new to Java – iOSAndroid Aug 18 '16 at 05:09
  • my SmsModel refers to SMSHistory class as you are already using. first of all create only one list combined of (fromsms and to sms) – Rahul Aug 18 '16 at 05:31
  • Hi Rah, so I just set the following variables and do a boolean check? Because i implemented those watching some youtube tutorials, so I create one full list of setting and getting of the SMShistory and change the view layouts? – iOSAndroid Aug 18 '16 at 05:38