Home | History | Annotate | Download | only in fragments
      1 /*
      2  * Copyright (C) 2015 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.example.android.wearable.datalayer.fragments;
     18 
     19 import android.app.Fragment;
     20 import android.content.Context;
     21 import android.os.Bundle;
     22 import android.support.annotation.Nullable;
     23 import android.view.LayoutInflater;
     24 import android.view.View;
     25 import android.view.ViewGroup;
     26 import android.widget.ArrayAdapter;
     27 import android.widget.ListView;
     28 import android.widget.TextView;
     29 
     30 import com.example.android.wearable.datalayer.R;
     31 
     32 /**
     33  * A fragment that shows a list of DataItems received from the phone
     34  */
     35 public class DataFragment extends Fragment {
     36 
     37     private DataItemAdapter mDataItemListAdapter;
     38     private TextView mIntroText;
     39     private boolean mInitialized;
     40 
     41     @Nullable
     42     @Override
     43     public View onCreateView(LayoutInflater inflater, ViewGroup container,
     44             Bundle savedInstanceState) {
     45         View view = inflater.inflate(R.layout.data_fragment, container, false);
     46         ListView dataItemList = (ListView) view.findViewById(R.id.dataItem_list);
     47         mIntroText = (TextView) view.findViewById(R.id.intro);
     48         mDataItemListAdapter = new DataItemAdapter(getActivity(),
     49                 android.R.layout.simple_list_item_1);
     50         dataItemList.setAdapter(mDataItemListAdapter);
     51         mInitialized = true;
     52         return view;
     53     }
     54 
     55     public void appendItem(String title, String text) {
     56         if (!mInitialized) {
     57             return;
     58         }
     59         mIntroText.setVisibility(View.INVISIBLE);
     60         mDataItemListAdapter.add(new Event(title, text));
     61     }
     62 
     63     private static class DataItemAdapter extends ArrayAdapter<Event> {
     64 
     65         private final Context mContext;
     66 
     67         public DataItemAdapter(Context context, int unusedResource) {
     68             super(context, unusedResource);
     69             mContext = context;
     70         }
     71 
     72         @Override
     73         public View getView(int position, View convertView, ViewGroup parent) {
     74             ViewHolder holder;
     75             if (convertView == null) {
     76                 holder = new ViewHolder();
     77                 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
     78                         Context.LAYOUT_INFLATER_SERVICE);
     79                 convertView = inflater.inflate(android.R.layout.two_line_list_item, null);
     80                 convertView.setTag(holder);
     81                 holder.text1 = (TextView) convertView.findViewById(android.R.id.text1);
     82                 holder.text2 = (TextView) convertView.findViewById(android.R.id.text2);
     83             } else {
     84                 holder = (ViewHolder) convertView.getTag();
     85             }
     86             Event event = getItem(position);
     87             holder.text1.setText(event.title);
     88             holder.text2.setText(event.text);
     89             return convertView;
     90         }
     91 
     92         private class ViewHolder {
     93 
     94             TextView text1;
     95             TextView text2;
     96         }
     97     }
     98 
     99     private class Event {
    100 
    101         String title;
    102         String text;
    103 
    104         public Event(String title, String text) {
    105             this.title = title;
    106             this.text = text;
    107         }
    108     }
    109 }
    110