2016-08-18 45 views
0

我有一個JSON文件,它看起來像這樣。我想知道是否有可能在Android應用程序中爲我的管理員用戶創建1個單一視圖,以便在1頁內查看短信日誌的完整列表(來自SMS和toSMS),並在日期和時間中相應地安排它們?因爲現在我使用了2種不同的佈局,2種不同的java類在兩個不同的視圖中顯示給SMS和來自SMS(簡而言之,admin用戶必須點擊toSMS按鈕才能查看所有toSMS細節,從SMS按鈕可以查看所有來自SMS的細節)。任何指導或建議將深受讚賞。1 Android的ListView的2個不同的JSONObjects

{ 
    "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; 
    } 
} 

回答

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的 修改它並將其設置爲適配器。

和適配器

@覆蓋 公共查看getView(INT位置,查看convertView,ViewGroup以及母公司) { 查看行內; 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; 
} 
+0

嗨Rah,我不太明白這一點,所以我們只是在我的Java類創建一個新的類,然後做一個布爾值?有沒有這方面的指導?我還是相當新的Java – iOSAndroid

+0

你可以告訴我你的適配器類嗎? – Rahul

+0

嗨Rah,請參考上面編輯過的帖子。 – iOSAndroid

相關問題