List of usage examples for android.text.format DateUtils getRelativeTimeSpanString
public static CharSequence getRelativeTimeSpanString(long startTime)
From source file:gxu.software_engineering.market.android.activity.ItemActivity.java
@Override protected void onCreate(Bundle arg0) { super.onCreate(arg0); // setContentView(cn.longkai.android.R.layout.fragment_container); setContentView(R.layout.item);/* w w w .j a v a 2s.co m*/ app = MarketApp.marketApp(); c = getContentResolver().query(Uri.parse(C.BASE_URI + C.ITEMS + "/" + getIntent().getLongExtra(C.ID, -1)), null, null, null, null); if (!c.moveToNext()) { Toast.makeText(this, R.string.item_not_found, Toast.LENGTH_SHORT).show(); return; } Button category = (Button) findViewById(R.id.category); category.setText(c.getString(c.getColumnIndex(C.item.CATEGORY))); Button clickTimes = (Button) findViewById(R.id.click_times); clickTimes.setText(c.getString(c.getColumnIndex(C.item.CLICK_TIMES)) + " "); Button name = (Button) findViewById(R.id.name); name.setText(c.getString(c.getColumnIndex(C.item.NAME))); Button seller = (Button) findViewById(R.id.seller); seller.setText("?" + c.getString(c.getColumnIndex(C.item.SELLER))); Button price = (Button) findViewById(R.id.price); price.setText("" + c.getString(c.getColumnIndex(C.item.PRICE))); long millis1 = c.getLong(c.getColumnIndex(C.item.ADDED_TIME)); Button publicAt = (Button) findViewById(R.id.publish_at); publicAt.setText("?" + DateUtils.getRelativeTimeSpanString(millis1)); Button desc = (Button) findViewById(R.id.desc); desc.setText(c.getString(c.getColumnIndex(C.item.DESCRIPTION))); String _extra = c.getString(c.getColumnIndex(C.item.EXTRA)); Button extra = (Button) findViewById(R.id.extra); if (TextUtils.isEmpty(_extra) || _extra.equals("null")) { extra.setText(R.string.no_extra); } else { extra.setText(_extra); } long mills2 = c.getLong(c.getColumnIndex(C.item.LAST_MODIFIED_TIME)); Button lastModifiedDate = (Button) findViewById(R.id.last_modified_time); lastModifiedDate.setText("" + DateUtils.getRelativeTimeSpanString(mills2)); // FragmentManager fm = getSupportFragmentManager(); // if (fm.findFragmentByTag("item") == null) { // Fragment fragment = new ItemFragment(); // fm.beginTransaction() // .replace(cn.longkai.android.R.id.fragment_container, fragment, "item") // .commit(); // } this.uid = c.getLong(c.getColumnIndex(C.item.SELLER_ID)); // this.cid = getCallingActivity(). this.cid = c.getLong(c.getColumnIndex(C.item.CID)); this.cname = c.getString(c.getColumnIndex(C.item.CATEGORY)); seller.setOnClickListener(this); category.setOnClickListener(this); getSupportActionBar().setTitle("??"); getSupportActionBar().setSubtitle(R.string.hello_world); getSupportActionBar().setDisplayHomeAsUpEnabled(true); }
From source file:com.barak.pix.CommentsFragment.java
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View rootView = inflater.inflate(R.layout.fragment_comments, container, false); RecyclerView mCommentsView = (RecyclerView) rootView.findViewById(R.id.comment_list); mEditText = (EditText) rootView.findViewById(R.id.editText); final Button sendButton = (Button) rootView.findViewById(R.id.send_comment); final DatabaseReference commentsRef = FirebaseUtil.getCommentsRef().child(mPostRef); mAdapter = new FirebaseRecyclerAdapter<Comment, CommentViewHolder>(Comment.class, R.layout.comment_item, CommentViewHolder.class, commentsRef) { @Override//from ww w. j ava2 s . c o m protected void populateViewHolder(final CommentViewHolder viewHolder, Comment comment, int position) { Author author = comment.getAuthor(); viewHolder.commentAuthor.setText(author.getFull_name()); GlideUtil.loadProfileIcon(author.getProfile_picture(), viewHolder.commentPhoto); viewHolder.authorRef = author.getUid(); viewHolder.commentTime.setText(DateUtils.getRelativeTimeSpanString((long) comment.getTimestamp())); viewHolder.commentText.setText(comment.getText()); } }; sendButton.setEnabled(false); mEditText.setHint(R.string.new_comment_hint); mEditText.setFilters(new InputFilter[] { new InputFilter.LengthFilter(DEFAULT_MSG_LENGTH_LIMIT) }); mEditText.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { } @Override public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { } @Override public void afterTextChanged(Editable editable) { if (editable.length() > 0) { sendButton.setEnabled(true); } else { sendButton.setEnabled(false); } } }); sendButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // Clear input box and hide keyboard. final Editable commentText = mEditText.getText(); mEditText.setText(""); InputMethodManager inputManager = (InputMethodManager) getActivity() .getSystemService(Context.INPUT_METHOD_SERVICE); inputManager.hideSoftInputFromWindow(mEditText.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser(); if (user == null) { Toast.makeText(getActivity(), R.string.user_logged_out_error, Toast.LENGTH_SHORT).show(); } Author author = new Author(user.getDisplayName(), "", user.getUid()); Comment comment = new Comment(author, commentText.toString(), ServerValue.TIMESTAMP); commentsRef.push().setValue(comment, new DatabaseReference.CompletionListener() { @Override public void onComplete(DatabaseError error, DatabaseReference firebase) { if (error != null) { Log.w(TAG, "Error posting comment: " + error.getMessage()); Toast.makeText(getActivity(), "Error posting comment.", Toast.LENGTH_SHORT).show(); mEditText.setText(commentText); } } }); } }); mCommentsView.setLayoutManager(new LinearLayoutManager(getActivity())); mCommentsView.setAdapter(mAdapter); return rootView; }
From source file:Rangoli.testapp.Feed.CommentsFragment.java
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View rootView = inflater.inflate(R.layout.fragment_comments, container, false); RecyclerView mCommentsView = (RecyclerView) rootView.findViewById(R.id.comment_list); mEditText = (EditText) rootView.findViewById(R.id.editText); final Button sendButton = (Button) rootView.findViewById(R.id.send_comment); final DatabaseReference commentsRef = FirebaseUtil.getCommentsRef().child(mPostRef); mAdapter = new FirebaseRecyclerAdapter<Comment, CommentViewHolder>(Comment.class, R.layout.comment_item, CommentViewHolder.class, commentsRef) { @Override//from ww w.ja v a2s.c om protected void populateViewHolder(final CommentViewHolder viewHolder, Comment comment, int position) { Author author = comment.getAuthor(); viewHolder.commentAuthor.setText(author.getFull_name()); PicassoUtil.loadProfileIcon(author.getProfile_picture(), viewHolder.commentPhoto); viewHolder.authorRef = author.getUid(); viewHolder.commentTime.setText(DateUtils.getRelativeTimeSpanString((long) comment.getTimestamp())); viewHolder.commentText.setText(comment.getText()); } }; sendButton.setEnabled(false); mEditText.setHint("add text here"); mEditText.setFilters(new InputFilter[] { new InputFilter.LengthFilter(DEFAULT_MSG_LENGTH_LIMIT) }); mEditText.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { } @Override public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { } @Override public void afterTextChanged(Editable editable) { if (editable.length() > 0) { sendButton.setEnabled(true); } else { sendButton.setEnabled(false); } } }); sendButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // Clear input box and hide keyboard. final Editable commentText = mEditText.getText(); mEditText.setText(""); InputMethodManager inputManager = (InputMethodManager) getActivity() .getSystemService(Context.INPUT_METHOD_SERVICE); inputManager.hideSoftInputFromWindow(mEditText.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser(); if (user == null) { Toast.makeText(getActivity(), "user_logged_out_error", Toast.LENGTH_SHORT).show(); } Author author = new Author(user.getDisplayName(), user.getPhotoUrl().toString(), user.getUid()); Comment comment = new Comment(author, commentText.toString(), ServerValue.TIMESTAMP); commentsRef.push().setValue(comment, new DatabaseReference.CompletionListener() { @Override public void onComplete(DatabaseError error, DatabaseReference firebase) { if (error != null) { Log.w(TAG, "Error posting comment: " + error.getMessage()); Toast.makeText(getActivity(), "Error posting comment.", Toast.LENGTH_SHORT).show(); mEditText.setText(commentText); } } }); } }); mCommentsView.setLayoutManager(new LinearLayoutManager(getActivity())); mCommentsView.setAdapter(mAdapter); return rootView; }
From source file:org.catnut.adapter.ConversationAdapter.java
@Override public void bindView(View view, final Context context, Cursor cursor) { final ViewHolder holder = (ViewHolder) view.getTag(); Picasso.with(context).load(cursor.getString(holder.avatarIndex)).error(R.drawable.error) .placeholder(R.drawable.error).into(holder.avatar); String replyString = cursor.getString(holder.replyCommentIndex); String statusString = cursor.getString(holder.statusIndex); if (TextUtils.isEmpty(replyString)) { // ta?... JSONObject status = null;//from w w w.j av a2 s .c o m try { status = new JSONObject(statusString); } catch (JSONException e) { Toast.makeText(context, R.string.malformed_json, Toast.LENGTH_LONG).show(); } if (status != null) { holder.myText.setText(status.optString(Status.text)); CatnutUtils.vividTweet(holder.myText, mTweetImageSpan); } } else { // ta? JSONObject reply = null; try { reply = new JSONObject(replyString); } catch (JSONException e) { Toast.makeText(context, R.string.malformed_json, Toast.LENGTH_LONG).show(); } if (reply != null) { holder.myText.setText(reply.optString(Comment.text)); CatnutUtils.vividTweet(holder.myText, mTweetImageSpan); } } holder.hisText.setText(cursor.getString(holder.hisTextIndex)); CatnutUtils.vividTweet(holder.hisText, mTweetImageSpan); final String screenName = cursor.getString(holder.screenNameIndex); String remark = cursor.getString(holder.remarkIndex); holder.screenName.setText(TextUtils.isEmpty(remark) ? screenName : remark); holder.hisCreateAt.setText(DateUtils .getRelativeTimeSpanString(DateTime.getTimeMills(cursor.getString(holder.hisCreateAtIndex)))); holder.myTextCreateAt.setText(DateUtils .getRelativeTimeSpanString(DateTime.getTimeMills(cursor.getString(holder.myTextCreateAtIndex)))); // bind tag for click listener final long uid = cursor.getLong(holder.uidIndex); holder.avatar.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { return CatnutUtils.imageOverlay(v, event); } }); holder.avatar.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { holder.avatar.getDrawable().clearColorFilter(); holder.avatar.invalidate(); Intent intent = new Intent(context, ProfileActivity.class); intent.putExtra(Constants.ID, uid); intent.putExtra(User.screen_name, screenName); context.startActivity(intent); } }); }
From source file:com.akalizakeza.apps.ishusho.activity.CommentsFragment.java
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View rootView = inflater.inflate(R.layout.fragment_comments, container, false); RecyclerView mCommentsView = (RecyclerView) rootView.findViewById(R.id.comment_list); mEditText = (EditText) rootView.findViewById(R.id.editText); final Button sendButton = (Button) rootView.findViewById(R.id.send_comment); final DatabaseReference commentsRef = FirebaseUtil.getCommentsRef().child(mPostRef); mAdapter = new FirebaseRecyclerAdapter<Comment, CommentViewHolder>(Comment.class, R.layout.activity_comments_item, CommentViewHolder.class, commentsRef) { @Override//from w ww. jav a2 s . c om protected void populateViewHolder(final CommentViewHolder viewHolder, Comment comment, int position) { Artist artist = comment.getArtist(); viewHolder.commentAuthor.setText(artist.getFull_name()); GlideUtil.loadProfileIcon(artist.getProfile_picture(), viewHolder.commentPhoto); viewHolder.authorRef = artist.getUid(); viewHolder.commentTime.setText(DateUtils.getRelativeTimeSpanString((long) comment.getTimestamp())); viewHolder.commentText.setText(comment.getText()); } }; sendButton.setEnabled(false); mEditText.setHint(R.string.new_comment_hint); mEditText.setFilters(new InputFilter[] { new InputFilter.LengthFilter(DEFAULT_MSG_LENGTH_LIMIT) }); mEditText.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { } @Override public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { } @Override public void afterTextChanged(Editable editable) { if (editable.length() > 0) { sendButton.setEnabled(true); } else { sendButton.setEnabled(false); } } }); sendButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // Clear input box and hide keyboard. final Editable commentText = mEditText.getText(); mEditText.setText(""); InputMethodManager inputManager = (InputMethodManager) getActivity() .getSystemService(Context.INPUT_METHOD_SERVICE); inputManager.hideSoftInputFromWindow(mEditText.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser(); if (user == null) { Toast.makeText(getActivity(), R.string.user_logged_out_error, Toast.LENGTH_SHORT).show(); } Artist artist = new Artist(user.getDisplayName(), user.getPhotoUrl().toString(), user.getUid()); Comment comment = new Comment(artist, commentText.toString(), ServerValue.TIMESTAMP); commentsRef.push().setValue(comment, new DatabaseReference.CompletionListener() { @Override public void onComplete(DatabaseError error, DatabaseReference firebase) { if (error != null) { Log.w(TAG, "Error posting comment: " + error.getMessage()); Toast.makeText(getActivity(), "Error posting comment.", Toast.LENGTH_SHORT).show(); mEditText.setText(commentText); } } }); } }); mCommentsView.setLayoutManager(new LinearLayoutManager(getActivity())); mCommentsView.setAdapter(mAdapter); return rootView; }
From source file:com.edicon.firebase.devs.firepix.CommentsFragment.java
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View rootView = inflater.inflate(R.layout.fragment_comments, container, false); RecyclerView mCommentsView = (RecyclerView) rootView.findViewById(R.id.comment_list); mEditText = (EditText) rootView.findViewById(R.id.editText); final Button sendButton = (Button) rootView.findViewById(R.id.send_comment); final DatabaseReference commentsRef = FirebaseUtil.getCommentsRef().child(mPostRef); mAdapter = new FirebaseRecyclerAdapter<Comment, CommentViewHolder>(Comment.class, R.layout.comment_item, CommentViewHolder.class, commentsRef) { @Override//from w w w. j a va2 s . c om protected void populateViewHolder(final CommentViewHolder viewHolder, Comment comment, int position) { Author author = comment.getAuthor(); viewHolder.commentAuthor.setText(author.getFull_name()); GlideUtil.loadProfileIcon(author.getProfile_picture(), viewHolder.commentPhoto); viewHolder.authorRef = author.getUid(); viewHolder.commentTime.setText(DateUtils.getRelativeTimeSpanString((long) comment.getTimestamp())); viewHolder.commentText.setText(comment.getText()); } }; sendButton.setEnabled(false); mEditText.setHint(R.string.new_comment_hint); mEditText.setFilters(new InputFilter[] { new InputFilter.LengthFilter(DEFAULT_MSG_LENGTH_LIMIT) }); mEditText.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { } @Override public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { } @Override public void afterTextChanged(Editable editable) { if (editable.length() > 0) { sendButton.setEnabled(true); } else { sendButton.setEnabled(false); } } }); sendButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // Clear input box and hide keyboard. final Editable commentText = mEditText.getText(); mEditText.setText(""); InputMethodManager inputManager = (InputMethodManager) getActivity() .getSystemService(Context.INPUT_METHOD_SERVICE); inputManager.hideSoftInputFromWindow(mEditText.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser(); if (user == null) { Toast.makeText(getActivity(), R.string.user_logged_out_error, Toast.LENGTH_SHORT).show(); } Author author = new Author(user.getDisplayName(), user.getPhotoUrl().toString(), user.getUid()); Comment comment = new Comment(author, commentText.toString(), ServerValue.TIMESTAMP); commentsRef.push().setValue(comment, new DatabaseReference.CompletionListener() { @Override public void onComplete(DatabaseError error, DatabaseReference firebase) { if (error != null) { Log.w(TAG, "Error posting comment: " + error.getMessage()); Toast.makeText(getActivity(), "Error posting comment.", Toast.LENGTH_SHORT).show(); mEditText.setText(commentText); } } }); } }); mCommentsView.setLayoutManager(new LinearLayoutManager(getActivity())); mCommentsView.setAdapter(mAdapter); return rootView; }
From source file:com.polito.dion.CommentsFragment.java
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View rootView = inflater.inflate(R.layout.fragment_comments, container, false); RecyclerView mCommentsView = (RecyclerView) rootView.findViewById(R.id.comment_list); mEditText = (EditText) rootView.findViewById(R.id.editText); mETAddType = (EditText) rootView.findViewById(R.id.etAddType); mETAddCost = (EditText) rootView.findViewById(R.id.etAddCost); final Button sendButton = (Button) rootView.findViewById(R.id.send_comment); final DatabaseReference commentsRef = FirebaseUtil.getCommentsRef().child(mPostRef); mAdapter = new FirebaseRecyclerAdapter<Comment, CommentViewHolder>(Comment.class, R.layout.comment_item, CommentViewHolder.class, commentsRef) { @Override// w w w. java 2 s.co m protected void populateViewHolder(final CommentViewHolder viewHolder, Comment comment, int position) { Author author = comment.getAuthor(); viewHolder.commentAuthor.setText(author.getFull_name()); GlideUtil.loadProfileIcon(author.getProfile_picture(), viewHolder.commentPhoto); viewHolder.authorRef = author.getUid(); viewHolder.commentTime.setText(DateUtils.getRelativeTimeSpanString((long) comment.getTimestamp())); viewHolder.commentText.setText(comment.getText()); viewHolder.commentAddType.setText(comment.getAddType()); viewHolder.commentAddCost.setText(comment.getAddCost()); } }; sendButton.setEnabled(false); mEditText.setHint(R.string.new_comment_hint); mEditText.setFilters(new InputFilter[] { new InputFilter.LengthFilter(DEFAULT_MSG_LENGTH_LIMIT) }); mEditText.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { } @Override public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { } @Override public void afterTextChanged(Editable editable) { if (editable.length() > 0) { sendButton.setEnabled(true); } else { sendButton.setEnabled(false); } } }); sendButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // Clear input box and hide keyboard. final Editable commentText = mEditText.getText(); final Editable commentAddType = mETAddType.getText(); final Editable commentAddCost = mETAddCost.getText(); mEditText.setText(""); mETAddType.setText(""); mETAddCost.setText(""); InputMethodManager inputManager = (InputMethodManager) getActivity() .getSystemService(Context.INPUT_METHOD_SERVICE); inputManager.hideSoftInputFromWindow(mEditText.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser(); if (user == null) { Toast.makeText(getActivity(), R.string.user_logged_out_error, Toast.LENGTH_SHORT).show(); } Author author = new Author(user.getDisplayName(), user.getPhotoUrl().toString(), user.getUid()); Comment comment = new Comment(author, commentAddCost.toString(), commentAddType.toString(), commentText.toString(), ServerValue.TIMESTAMP); commentsRef.push().setValue(comment, new DatabaseReference.CompletionListener() { @Override public void onComplete(DatabaseError error, DatabaseReference firebase) { if (error != null) { Log.w(TAG, "Error posting comment: " + error.getMessage()); Toast.makeText(getActivity(), "Error posting comment.", Toast.LENGTH_SHORT).show(); mEditText.setText(commentText); } } }); } }); mCommentsView.setLayoutManager(new LinearLayoutManager(getActivity())); mCommentsView.setAdapter(mAdapter); return rootView; }
From source file:com.bloodtolife.bloodapp.CommentsFragment.java
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View rootView = inflater.inflate(R.layout.fragment_comments, container, false); db = new SQLiteHandler(getActivity()); RecyclerView mCommentsView = (RecyclerView) rootView.findViewById(R.id.comment_list); mEditText = (EditText) rootView.findViewById(R.id.editText); final Button sendButton = (Button) rootView.findViewById(R.id.send_comment); final DatabaseReference commentsRef = FirebaseUtil.getCommentsRef().child(mPostRef); mAdapter = new FirebaseRecyclerAdapter<Comment, CommentViewHolder>(Comment.class, R.layout.comment_item, CommentViewHolder.class, commentsRef) { @Override/*from ww w . j av a 2 s . co m*/ protected void populateViewHolder(final CommentViewHolder viewHolder, Comment comment, int position) { user_ = db.getUserDetails(); viewHolder.commentAuthor.setText(user_.get("name")); GlideUtil.loadProfileIcon(user_.get("profile_pic_url"), viewHolder.commentPhoto); viewHolder.authorRef = user_.get("uid"); viewHolder.commentTime.setText(DateUtils.getRelativeTimeSpanString((long) comment.getTimestamp())); viewHolder.commentText.setText(comment.getText()); } }; sendButton.setEnabled(false); mEditText.setHint(R.string.new_comment_hint); mEditText.setFilters(new InputFilter[] { new InputFilter.LengthFilter(DEFAULT_MSG_LENGTH_LIMIT) }); mEditText.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { } @Override public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { } @Override public void afterTextChanged(Editable editable) { if (editable.length() > 0) { sendButton.setEnabled(true); } else { sendButton.setEnabled(false); } } }); sendButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // Clear input box and hide keyboard. final Editable commentText = mEditText.getText(); mEditText.setText(""); InputMethodManager inputManager = (InputMethodManager) getActivity() .getSystemService(Context.INPUT_METHOD_SERVICE); inputManager.hideSoftInputFromWindow(mEditText.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser(); if (user == null) { Toast.makeText(getActivity(), R.string.user_logged_out_error, Toast.LENGTH_SHORT).show(); } Comment comment = new Comment(user.getDisplayName(), commentText.toString(), ServerValue.TIMESTAMP); commentsRef.push().setValue(comment, new DatabaseReference.CompletionListener() { @Override public void onComplete(DatabaseError error, DatabaseReference firebase) { if (error != null) { Log.w(TAG, "Error posting comment: " + error.getMessage()); Toast.makeText(getActivity(), "Error posting comment.", Toast.LENGTH_SHORT).show(); mEditText.setText(commentText); } } }); } }); mCommentsView.setLayoutManager(new LinearLayoutManager(getActivity())); mCommentsView.setAdapter(mAdapter); return rootView; }
From source file:com.janardhan.blood2life.CommentsFragment.java
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View rootView = inflater.inflate(R.layout.fragment_comments, container, false); Log.d("mPostRef", mPostRef); db = new SQLiteHandler(getActivity()); user_ = db.getUserDetails();/* ww w. j av a2 s. c o m*/ RecyclerView mCommentsView = (RecyclerView) rootView.findViewById(R.id.comment_list); mEditText = (EditText) rootView.findViewById(R.id.editText); final Button sendButton = (Button) rootView.findViewById(R.id.send_comment); final DatabaseReference commentsRef = FirebaseUtil.getCommentsRef().child(mPostRef); mAdapter = new FirebaseRecyclerAdapter<Comment, CommentViewHolder>(Comment.class, R.layout.comment_item, CommentViewHolder.class, commentsRef) { @Override protected void populateViewHolder(final CommentViewHolder viewHolder, Comment comment, int position) { String aurth = comment.getAuthor(); viewHolder.commentAuthor.setText(comment.getAuthor()); GlideUtil.loadProfileIcon(comment.getUser_propic(), viewHolder.commentPhoto); viewHolder.authorRef = comment.getUser_id(); viewHolder.commentTime.setText(DateUtils.getRelativeTimeSpanString((long) comment.getTimestamp())); viewHolder.commentText.setText(comment.getText()); } }; sendButton.setEnabled(false); mEditText.setHint(R.string.new_comment_hint); mEditText.setFilters(new InputFilter[] { new InputFilter.LengthFilter(DEFAULT_MSG_LENGTH_LIMIT) }); mEditText.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { } @Override public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { } @Override public void afterTextChanged(Editable editable) { if (editable.length() > 0) { sendButton.setEnabled(true); } else { sendButton.setEnabled(false); } } }); sendButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // Clear input box and hide keyboard. final Editable commentText = mEditText.getText(); mEditText.setText(""); InputMethodManager inputManager = (InputMethodManager) getActivity() .getSystemService(Context.INPUT_METHOD_SERVICE); inputManager.hideSoftInputFromWindow(mEditText.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser(); if (user == null) { Toast.makeText(getActivity(), R.string.user_logged_out_error, Toast.LENGTH_SHORT).show(); } Comment comment = new Comment(user.getDisplayName(), commentText.toString(), ServerValue.TIMESTAMP, user_.get("uid"), user_.get("profile_pic_url")); commentsRef.push().setValue(comment, new DatabaseReference.CompletionListener() { @Override public void onComplete(DatabaseError error, DatabaseReference firebase) { if (error != null) { Log.w(TAG, getString(R.string.error_posting) + error.getMessage()); Toast.makeText(getActivity(), R.string.error_posting, Toast.LENGTH_SHORT).show(); mEditText.setText(commentText); } } }); } }); mCommentsView.setLayoutManager(new LinearLayoutManager(getActivity())); mCommentsView.setAdapter(mAdapter); return rootView; }
From source file:gxu.software_engineering.market.android.service.SyncService.java
@Override protected void onHandleIntent(Intent intent) { // ??/*w w w. ja v a2s . c o m*/ if (!NetworkUtils.connected(getApplicationContext())) { return; } // ?? long lastSyncMills = app.getPrefs().getLong(C.LAST_SYNC, 0); if (lastSyncMills == 0L) { Calendar c = Calendar.getInstance(); c.set(Calendar.YEAR, 1980); lastSyncMills = c.getTimeInMillis(); } String uri = C.DOMAIN + String.format("/sync?count=%d&last=%s", C.DEFAULT_LIST_SIZE, lastSyncMills); JSONObject result = null; try { result = RESTMethod.get(uri); } catch (IOException e) { e.printStackTrace(); } catch (JSONException e) { e.printStackTrace(); } boolean nice = false; ContentValues[] categories = null; ContentValues[] users = null; ContentValues[] items = null; try { if (result.getInt(C.STATUS) == C.OK) { nice = true; categories = Processor.toCategories(result.getJSONArray(C.CATEGORIES)); users = Processor.toUsers(result.getJSONArray(C.USERS)); items = Processor.toItems(result.getJSONArray(C.ITEMS)); } else { throw new RuntimeException(result.getString(C.MSG)); } } catch (Exception e) { Log.wtf("synv json result error!", e); final String msg = e.getMessage(); handler.post(new Runnable() { @Override public void run() { Toast.makeText(getApplicationContext(), getResources().getString(R.string.sync_error, msg), Toast.LENGTH_SHORT).show(); } }); } if (nice) { getContentResolver().bulkInsert(Uri.parse(C.BASE_URI + C.CATEGORIES), categories); getContentResolver().bulkInsert(Uri.parse(C.BASE_URI + C.USERS), users); getContentResolver().bulkInsert(Uri.parse(C.BASE_URI + C.ITEMS), items); final long now = System.currentTimeMillis(); // app.getPrefs().edit().putLong(C.LAST_SYNC, now).commit(); // ???? handler.post(new Runnable() { @Override public void run() { Toast.makeText(getApplicationContext(), getResources().getString(R.string.sync_nice, DateUtils.getRelativeTimeSpanString(now)), Toast.LENGTH_SHORT).show(); } }); } }