2016-08-18 16 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

相关问题