I am implementing a local database using ROOM in android studio. I am having a problem. I am successful in inserting and deleting the data entries into the database. But when I am getting all the data objects from the database then each time I am getting one object less.
When that activity starts then there is no problem but when I am trying to update the recycler view after inserting a new data object into the database then that object is being updated into the recycler view one step after creating a new data object entry and the same problem happens with this also.
below is my complete code:-
package com.example.eventus.DashBoard;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.DialogFragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Toast;
import com.example.eventus.ApplicationDatabase.Events.Event;
import com.example.eventus.ApplicationDatabase.Events.EventsAdapter;
import com.example.eventus.ApplicationDatabase.Events.EventsDao;
import com.example.eventus.R;
import com.example.eventus.Singletons.DatabaseSingleton;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
public class Dashboard extends AppCompatActivity implements View.OnClickListener, DatePickerDialog.OnDateSetListener {
//Declaring the views
private FloatingActionButton fbAddEvent;
private Button btnOKNewEvent, btnCancelNewEvent;
private EditText etDeadlineNewEvent, etTitleNewEvent, etInfoNewEvent;
//For recycler view
private RecyclerView rvEvents;
private RecyclerView.LayoutManager layoutManager;
private List<Event> events;
private EventsAdapter eventsAdapter;
//For alert box
private AlertDialog.Builder alertDialogBuilder;
private AlertDialog alertDialog;
private View alertDialogView;
//For database
private DatabaseSingleton databaseSingleton;
private EventsDao eventsDao;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dashboard);
//Initializing and setting onclicklisteners on views
init();
//RecyclerView stuff
recyclerViewStuff();
//new event stuff
newEventDialog();
//database stuff
database();
//loadEvents
loadEvents();
}
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.fb_add_event_dashboard:
alertDialog.show();
}
}
#Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
Calendar mCalendar = Calendar.getInstance();
mCalendar.set(Calendar.YEAR, year);
mCalendar.set(Calendar.MONTH, month);
mCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
String selectedDate = DateFormat.getDateInstance(DateFormat.FULL).format(mCalendar.getTime());
etDeadlineNewEvent.setText(selectedDate);
}
private void init(){
//Initializing the views
rvEvents = findViewById(R.id.rv_events_dashboard);
fbAddEvent = findViewById(R.id.fb_add_event_dashboard);
//Setting onclicklisteners on views
fbAddEvent.setOnClickListener(this);
}
private void recyclerViewStuff() {
//Initializing the recycler view stuffs
layoutManager = new GridLayoutManager(this, 2);
rvEvents.setLayoutManager(layoutManager);
}
private void newEventDialog() {
alertDialogBuilder = new AlertDialog.Builder(Dashboard.this);
alertDialogView = getLayoutInflater().inflate(R.layout.new_event, null);
alertDialogBuilder.setView(alertDialogView);
alertDialog = alertDialogBuilder.create();
alertDialog.setCanceledOnTouchOutside(false);
//Instantiating the view
btnOKNewEvent = alertDialogView.findViewById(R.id.btn_ok_new_event);
etDeadlineNewEvent = alertDialogView.findViewById(R.id.et_deadline_new_event);
btnCancelNewEvent = alertDialogView.findViewById(R.id.btn_cancel_new_event);
etTitleNewEvent = alertDialogView.findViewById(R.id.et_title_new_event);
etInfoNewEvent = alertDialogView.findViewById(R.id.et_info_new_event);
//setting on click listeners for the views in dialog box
btnOKNewEvent.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
addEvent();
//loadEvents();
refreshEvents();
alertDialog.dismiss();
}
});
btnCancelNewEvent.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
alertDialog.dismiss();
}
});
etDeadlineNewEvent.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
DialogFragment datePicker = new com.example.eventus.DateAndTime.DatePicker();
datePicker.show(getSupportFragmentManager(), null);
}
});
}
private void database(){
//getting the database instance
databaseSingleton = DatabaseSingleton.getInstance();
//getting the events dao
eventsDao = databaseSingleton.getEventsDao(getApplicationContext());
//deleting all previously saved events
eventsDao.deleteAllEvent();
}
private void addEvent() {
String eventTitle, eventInfo, eventDeadline;
eventTitle = etTitleNewEvent.getText().toString();
eventInfo = etInfoNewEvent.getText().toString();
eventDeadline = etDeadlineNewEvent.getText().toString();
//creating an event
Event event = new Event(eventTitle, eventInfo, eventDeadline);
Handler handler = new Handler();
Runnable runnable = new Runnable() {
#Override
public void run() {
eventsDao.createEvent(event);
Toast.makeText(Dashboard.this, "Event Created", Toast.LENGTH_SHORT).show();
}
};
handler.post(runnable);
}
private void loadEvents(){
events = eventsDao.getEvents();
eventsAdapter = new EventsAdapter(events);
rvEvents.setAdapter(eventsAdapter);
}
private void refreshEvents(){
events.clear();
List<Event> tempEventsList = eventsDao.getEvents();
for(int i=0;i<tempEventsList.size();i++){
events.add(tempEventsList.get(i));
}
eventsAdapter.notifyDataSetChanged();
}
}
The problem you are having is realted to concurrency. If you check your code you first call the add event method and place the insertion inside a Runnable, this tells the application to execute at a later time when possible, but it does not execute it inmediately; therefor the code continues its execution, you fetch the events and then, at a later time, the new event is added.
To solve your problem, try to place the call of the method refreshEvents inside the runnable.
Runnable runnable = new Runnable() {
#Override
public void run() {
eventsDao.createEvent(event);
Toast.makeText(Dashboard.this, "Event Created", Toast.LENGTH_SHORT).show();
refreshEvents();
}
};
handler.post(runnable);
EDIT: As correctly said in the comments is not a mult-thread issue, it's the execution time of code fragments inside a single thread.
Related
I'm sure it's going to be a something very obvious that my stupid self couldn't find even after staring at the code for an hour.
I am trying to call the updateImages method in FragmentHome from HomeActivity. I have also created an instance of the class in onCreate but for reasons of my primitive coding knowledge, I am not able to call it. Any help would be greatly appreciated :)
Class that contains said method:
package com.example.youtubethumbnailapp;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.squareup.picasso.Picasso;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import static android.app.Activity.RESULT_OK;
public class FragmentHome extends Fragment {
private FragmentHomeListener listener;
public interface FragmentHomeListener{
void onInputHomeSent(ArrayList<Uri> input);
}
private ImageView video1;
private ImageView video2;
private ImageView video3;
private ImageView video4;
private ImageView video5;
private ImageView video6;
private ImageView video7;
private ImageView video8;
private ImageView video9;
private ImageView video10;
private ImageView addImageButton;
public Uri imageUri;
ArrayList<Uri> uriArrayList = new ArrayList<Uri>();
ArrayList<ImageView> imageViews = new ArrayList<ImageView>();
private int imagesSelected = 0;
#Nullable
#org.jetbrains.annotations.Nullable
#Override
public View onCreateView(#NonNull #org.jetbrains.annotations.NotNull LayoutInflater inflater, #Nullable #org.jetbrains.annotations.Nullable ViewGroup container, #Nullable #org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_home,container,false);
//initialize variables
video1 = v.findViewById(R.id.video1);
imageViews.add(video1);
video2 = v.findViewById(R.id.video2);
imageViews.add(video2);
video3 = v.findViewById(R.id.video3);
imageViews.add(video3);
video4 = v.findViewById(R.id.video4);
imageViews.add(video4);
video5 = v.findViewById(R.id.video5);
imageViews.add(video5);
video6 = v.findViewById(R.id.video6);
imageViews.add(video6);
video7 = v.findViewById(R.id.video7);
imageViews.add(video7);
video8 = v.findViewById(R.id.video8);
imageViews.add(video8);
video9 = v.findViewById(R.id.video9);
imageViews.add(video9);
video10 = v.findViewById(R.id.video10);
imageViews.add(video10);
addImageButton = v.findViewById(R.id.changeViewButton);
addImageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
choosePicture();
}
});
return v;
}//end of onCreate
private void choosePicture() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent, 1);
}
#Override
public void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode==1 && resultCode==RESULT_OK && null != data && data.getData()!=null){
imageUri = data.getData();
addPicture();
uriArrayList.add(imageUri);
imagesSelected++;
}
}
#Override
public void onAttach(#NonNull #NotNull Context context) {
super.onAttach(context);
if (context instanceof FragmentHomeListener){
listener = (FragmentHomeListener) context;
} else {
throw new RuntimeException(context.toString()
+ "must implement FragmentHomeListener");
}
}
#Override
public void onDetach() {
super.onDetach();
listener = null;
}
//this is the method I want to call
public void updateImages(ArrayList<Uri> imagesArrayList){
for (int i = 0; i < imagesArrayList.size(); i++){
Picasso.get()
.load(imagesArrayList.get(i))
.fit().centerCrop()
.into(imageViews.get(i));
}
}
private void addPicture(){
Picasso.get()
.load(imageUri)
.fit().centerCrop()
.into(imageViews.get(imagesSelected));
}
private void dataSent(){
listener.onInputHomeSent(uriArrayList);
}
}
Class where I want to call the method from:
package com.example.youtubethumbnailapp;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;
public class HomeActivity extends AppCompatActivity implements FragmentHome.FragmentHomeListener, FragmentHistory.FragmentHistoryListener, FragmentSuggested.FragmentSuggestedListener {
private ImageView homeButton;
private ImageView historyButton;
private ImageView playButton;
private Fragment fragmentHome;
private Fragment fragmentHistory;
private Fragment fragmentSuggested;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
homeButton = findViewById(R.id.homeButton);
historyButton = findViewById(R.id.historyButton);
playButton = findViewById(R.id.playButton);
fragmentHome = new FragmentHome();
fragmentHistory = new FragmentHistory();
fragmentSuggested = new FragmentSuggested();
switchToFragmentHome();
homeButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
switchToFragmentHome();
}
});
historyButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
switchToFragmentHistory();
}
});
playButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
switchToFragmentSuggested();
}
});
}//end of onCreate
public void switchToFragmentHome(){
FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction().replace(R.id.flFragment, fragmentHome).commit();
}
public void switchToFragmentSuggested(){
FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction().replace(R.id.flFragment, fragmentSuggested).commit();
}
public void switchToFragmentHistory(){
FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction().replace(R.id.flFragment, fragmentHistory).commit();
}
#Override
public void onInputHistorySent(ArrayList<Uri> input) {
//I want to call it from here
}
#Override
public void onInputHomeSent(ArrayList<Uri> input) {
}
#Override
public void onInputSuggestedSent(ArrayList<Uri> input) {
}
}
Since I have instantiated the class, I tried fragmentHome.update but it doesn't show up.
you are creating new instance of fragment. Please check below code:
FragmentHome fragmentHome = new FragmentHome();
You should use it like this:
fragmentHome = new FragmentHome();
Currently, your updateImages method is part of an instance of the class FragmentHome. Since the method doesn't seem to depend on anything, e.g., state, you can make the method static as follows:
public static void updateImages(//args...){
// Your method here
}
Within HomeActivity, you can access this static inner method directly as:
FragmentHome.updateImages(//args...);
P.S.: At the moment, you are likely getting a Non-static method 'updateImages()' cannot be referenced from a static context error.
Note that creating and using updateImages() in this manner is not good practice. It is not "wrong", but it is not efficient in the long run and can cause bugs. (E.g., even if you just declared the method static, you'd likely get a NullPointerException because updateImages() contains a reference to imageViews, an object inside FragmentHome, which may not have been initialized when the method is called in HomeActivity.)
If possible, you should put the independent method updateImages() in a separate utilities class, such that your UI code is as separate from your logic code as possible. This is called "separation of concerns".
I see that you need to use Picasso to set a source image to an ImageView. To do this, you could pass in the ImageView as an argument into the method (disclaimer: if it makes sense for your use case; if it is not going to leak context; etc.), so that the updateImages() method doesn't contain a reference to the imageViews ArrayList that sits in your FragmentHome. Also note that imageViews is currently private, since you've not declared it public.
If you employ this method, your updateImages() or other methods can be reused with higher generality. But of course, there are cases where you just want a wrapper for a method you just can't make very generalized, and that's okay too.
after fixing the null pointer exception of a previous question, (Not sure if posting a different question for the same code is okay, do let me know if it is not) I've come across a new problem. When I try passing the date variable from this first activity to another, it is always empty. I've also tried just setting a public getter or the variable and it is also empty. However, using a toast to check within the class shows that the variable does indeed contain the date. I am trying to pass the date class to be added into a database by the other classes in the application package. Any help would be much appreciated.
CalendarActivity.java
package com.example.zaphk.studenthelperapplication3.calendar;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.CalendarView;
import android.widget.Toast;
import com.example.zaphk.studenthelperapplication3.R;
public class CalendarActivity extends AppCompatActivity {
public static final String EXTRA_TEXT = "com.example.zaphk.studenthelperapplication3";
private static final String TAG = "CalendarActivity";
private CalendarView mCalendarView;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.calendar_layout);
mCalendarView = (CalendarView) findViewById(R.id.calendarView);
mCalendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
#Override
public void onSelectedDayChange(CalendarView CalendarView, int year, int month, int dayOfMonth) {
String date = year + "/" + month + "/"+ dayOfMonth ;
Log.d(TAG, "onSelectedDayChange: yyyy/mm/dd:" + date);
Intent intent = new Intent(CalendarActivity.this,CalendarEvent.class);
intent.putExtra(Intent.EXTRA_TEXT,date);
startActivity(intent);
Toast.makeText(CalendarActivity.this,date,Toast.LENGTH_SHORT).show();
}
});
}
}
The class I am trying to receive it from : CalendarEvent.java
package com.example.zaphk.studenthelperapplication3.calendar;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
import com.example.zaphk.studenthelperapplication3.calendar.database.Calendar;
import com.example.zaphk.studenthelperapplication3.calendar.database.CalendarAdapter;
import com.example.zaphk.studenthelperapplication3.calendar.database.Calendar_DbHelper;
import com.example.zaphk.studenthelperapplication3.utils.MyDividerItemDecoration;
import com.example.zaphk.studenthelperapplication3.utils.RecyclerTouchListener;
import com.example.zaphk.studenthelperapplication3.R;
public class CalendarEvent extends AppCompatActivity {
private CalendarAdapter mAdapter;
private List<Calendar> notesList = new ArrayList<>();
private CoordinatorLayout coordinatorLayout;
private RecyclerView recyclerView;
private TextView noNotesView;
Intent intent = getIntent();
public String date;
private Calendar_DbHelper db;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
intent = getIntent();
date = intent.getStringExtra(CalendarActivity.EXTRA_TEXT);
setContentView(R.layout.activity_notes);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
coordinatorLayout = findViewById(R.id.coordinator_layout);
recyclerView = findViewById(R.id.recycler_view);
noNotesView = findViewById(R.id.empty_notes_view);
db = new Calendar_DbHelper(this);
notesList.addAll(db.getAllNotes());
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
showNoteDialog(false, null, -1);
}
});
mAdapter = new CalendarAdapter(this, notesList);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.addItemDecoration(new MyDividerItemDecoration(this, LinearLayoutManager.VERTICAL, 16));
recyclerView.setAdapter(mAdapter);
toggleEmptyNotes();
/**
* On long press on RecyclerView item, open alert dialog
* with options to choose
* Edit and Delete
* */
recyclerView.addOnItemTouchListener(new RecyclerTouchListener(this,
recyclerView, new RecyclerTouchListener.ClickListener() {
#Override
public void onClick(View view, final int position) {
}
#Override
public void onLongClick(View view, int position) {
showActionsDialog(position);
}
}));
}
/**
* Inserting new note in db
* and refreshing the list
*/
private void createNote(String note) {
// inserting note in db and getting
// newly inserted note id
long id = db.insertNote(note);
// get the newly inserted note from db
Calendar n = db.getNote(id);
if (n != null) {
// adding new note to array list at 0 position
notesList.add(0, n);
// refreshing the list
mAdapter.notifyDataSetChanged();
toggleEmptyNotes();
}
}
/**
* Updating note in db and updating
* item in the list by its position
*/
private void updateNote(String note, int position) {
Calendar n = notesList.get(position);
// updating note text
n.setNote(note);
// updating note in db
db.updateNote(n);
// refreshing the list
notesList.set(position, n);
mAdapter.notifyItemChanged(position);
toggleEmptyNotes();
}
/**
* Deleting note from SQLite and removing the
* item from the list by its position
*/
private void deleteNote(int position) {
// deleting the note from db
db.deleteNote(notesList.get(position));
// removing the note from the list
notesList.remove(position);
mAdapter.notifyItemRemoved(position);
toggleEmptyNotes();
}
/**
* Opens dialog with Edit - Delete options
* Edit - 0
* Delete - 0
*/
private void showActionsDialog(final int position) {
CharSequence colors[] = new CharSequence[]{"Edit", "Delete"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Choose option");
builder.setItems(colors, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
if (which == 0) {
showNoteDialog(true, notesList.get(position), position);
} else {
deleteNote(position);
}
}
});
builder.show();
}
/**
* Shows alert dialog with EditText options to enter / edit
* a note.
* when shouldUpdate=true, it automatically displays old note and changes the
* button text to UPDATE
*/
private void showNoteDialog(final boolean shouldUpdate, final Calendar note, final int position) {
LayoutInflater layoutInflaterAndroid = LayoutInflater.from(getApplicationContext());
View view = layoutInflaterAndroid.inflate(R.layout.note_dialog, null);
AlertDialog.Builder alertDialogBuilderUserInput = new AlertDialog.Builder(CalendarEvent.this);
alertDialogBuilderUserInput.setView(view);
final EditText inputNote = view.findViewById(R.id.note);
TextView dialogTitle = view.findViewById(R.id.dialog_title);
dialogTitle.setText(!shouldUpdate ? getString(R.string.lbl_new_note_title) : getString(R.string.lbl_edit_note_title));
if (shouldUpdate && note != null) {
inputNote.setText(note.getNote());
}
alertDialogBuilderUserInput
.setCancelable(false)
.setPositiveButton(shouldUpdate ? "update" : "save", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogBox, int id) {
}
})
.setNegativeButton("cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogBox, int id) {
dialogBox.cancel();
}
});
final AlertDialog alertDialog = alertDialogBuilderUserInput.create();
alertDialog.show();
alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Show toast message when no text is entered
if (TextUtils.isEmpty(inputNote.getText().toString())) {
Toast.makeText(CalendarEvent.this, "Enter note!", Toast.LENGTH_SHORT).show();
return;
} else {
alertDialog.dismiss();
}
// check if user updating note
if (shouldUpdate && note != null) {
// update note by it's id
updateNote(inputNote.getText().toString(), position);
} else {
// create new note
createNote(inputNote.getText().toString());
}
}
});
}
/**
* Toggling list and empty notes view
*/
private void toggleEmptyNotes() {
// you can check notesList.size() > 0
if (db.getNotesCount() > 0) {
noNotesView.setVisibility(View.GONE);
} else {
noNotesView.setVisibility(View.VISIBLE);
}
}
public String getDate(){
return date;
}
}
You are using wrong key while passing data between activity.
Replace below line,
intent.putExtra(Intent.EXTRA_TEXT,date);
With this one,
intent.putExtra(CalendarActivity.EXTRA_TEXT,date);
You use the key of Intent.EXTRA_TEXT.
intent.putExtra(Intent.EXTRA_TEXT,date)
But you use the other key to receive. It's not the same key.
intent.getStringExtra(CalendarActivity.EXTRA_TEXT);
As per I know (Not pretty sure)
Intent.EXTRA_TEXT
is used for implicit intents.
For explicit (i.e.Activity to Activity)
Do as below
Intent intent = new Intent(CalendarActivity.this,CalendarEvent.class);
intent.putExtra("DATE",date);
startActivity(intent);
To receive
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_notes);
intent = getIntent();
String date = intent.getStringExtra("DATE");
}
Where "DATE" is String KEY.
I would say that CalendarActivity.EXTRA_TEXT is probably empty because you have not imported that class in to CalendarEvent.java
import com.example.zaphk.studenthelperapplication3.calendar.CalendarActivity; in CalendarEvent.java
and as other people have said the put and get on the Intent need to be the the same String value.
I'm trying to do a chat application with two activities. On the first plan is a short list containing the last sent message in each row. A second activity contain all conversation. I use socket.io and my problem is that, when I click back button and then I come back to my app notifyDataSetChanged() stops working. My app recevie a messages from dialog box from a website which a cooperates with android app. In console log I see that a messages from a website are received and onTaskComplete() method is called but a listview is not refreshing. I read that an adapter loses a reference to a listview after restart an activity but how to see in my code I create a new adapter in onResume() method. I don't understand what I do wrong?
While if I will throw out beyond the condition "mSocket.on("message", onMessage);" then a listview is refresh after come back to app but a messages are multiple as many times as there were returns.
below is my code, please help!
MainActivity:
package com.example.seadog.fb_dialog;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import io.socket.client.Socket;
import io.socket.emitter.Emitter;
public class MainActivity extends Activity implements MyListener {
public ListView listView;
public MyBaseAdapter adapter;
public TextView textView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*
* Get Socket.io Object
*/
SocketIO socketIo = new SocketIO();
Socket mSocket = socketIo.getSocket(); // get socket
Integer id = socketIo.getId(); // get Website ID
if(mSocket == null) {
socketIo.Connection();
mSocket = socketIo.getSocket();
mSocket.on("message", onMessage);
}
listView = (ListView) findViewById(R.id.listView);
/*
* OnItemClickListener
*/
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(MainActivity.this, Conversation.class);
intent.putExtra("item", position);
startActivity(intent);
TextView textView = (TextView) view.findViewById(R.id.descitem);
textView.setTypeface(null, Typeface.NORMAL);
}
});
textView = (TextView) findViewById(R.id.count);
}
private Emitter.Listener onMessage = new Emitter.Listener() {
/*
* Message Listener
*/
#Override
public void call(Object... args) {
Boolean isset = false;
try {
JSONObject object = (JSONObject) args[0];
String _id = object.getString("_id");
String message = object.getString("message");
JSONObject obj = new JSONObject();
obj.put("direction", "fb-lt");
obj.put("message", message);
obj.put("date", "2017-05-29T12:15:49.245Z");
for(int i = 0; i < arrayList.size(); i++){
ListData ld = (ListData) arrayList.get(i);
String id = ld.getId();
if(_id.equals(id)){
JSONArray Data = ld.getData();
Data.put(obj);
ld.setDescription(message);
arrayList.set(i, ld);
isset = true;
Log.d("LOG", message);
}
}
if(!isset) {
JSONArray jsonArray = new JSONArray();
jsonArray.put(obj);
ListData ld = new ListData();
ld.set_id(_id);
ld.setID(1);
ld.setTitle("Klient:");
ld.setDescription(message);
ld.setData(jsonArray);
arrayList.add(ld);
}
onTaskComplete();
} catch (JSONException e) {
e.printStackTrace();
}
}
};
public void onResume() {
super.onResume();
ArrayList clone = (ArrayList) arrayList.clone();
arrayList.clear();
arrayList.addAll(clone);
adapter = new MyBaseAdapter(this, arrayList);
listView.setAdapter(adapter);
}
#Override
public void onTaskComplete() {
runOnUiThread(new Runnable() {
#Override
public void run () {
Log.d("LOG:","refresh");
adapter.notifyDataSetChanged();
}
});
}
}
Interface MyListener:
package com.example.seadog.fb_dialog;
import java.util.ArrayList;
public interface MyListener {
ArrayList arrayList = new ArrayList();
void onTaskComplete();
}
If I remember well, when you open a new activity in android, that's something separate from the previous one.
When you press the back button, it just kill the activity you were in and show the previous activity.
Maybe you can try to do what you need when back is pressed in this :
#Override
public void onBackPressed()
{
//Do your things here
}
Hope I helped.
I am having an issue in setting up my Chronometer. The Java file shows an error at "updateTimerText" stating Cannot resolve method "updateTimerText (java.lang.string) How do I solve this? Everything is Fine on the fragment I am trying to cast this task to it is just the Chronometer.java file. If you need any other information or code, let me know. Thanks in advance.
package com.example.platinumirish.runassistwithdrawer;
import android.content.Context;
import runassist.demo.fragments.MainFragment;
/**
* Created by Platinum Irish on 22/06/2016.
*/
public class Chronometer implements Runnable {
public static final long MILLIS_TO_MINUTES = 60000;
public static final long MILLIS_TO_HOURS = 3600000;
private Context mContext;
private long mStartTime;
private boolean mIsRunning;
public Chronometer(Context context){
mContext = context;
}
public void start() {
mStartTime = System.currentTimeMillis();
mIsRunning = true;
}
public void stop() {
mIsRunning = false;
}
#Override
public void run() {
while(mIsRunning) {
long since = System.currentTimeMillis() - mStartTime;
int seconds = (int) (since / 1000 % 60);
int minutes = (int)(((since / MILLIS_TO_MINUTES)) % 60);
int hours = (int)((since / (MILLIS_TO_HOURS)) % 24);
int millis = (int) since % 1000;
MainFragment mainFragment = (MainFragment) mContext.updateTimerText(String.format(
"%02d;%02d;%02d;%03d", hours, minutes, seconds, millis
));
}
}
}
Here is my Main Fragment too if it will help in any way, shape or form.
package runassist.demo.fragments;
import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Chronometer;
import android.widget.TextView;
import com.example.platinumirish.runassistwithdrawer.R;
public class MainFragment extends Fragment{
private TextView mTvTime;
private Button mBtnStart;
private Button mBtnStop;
private Context mContext;
private Chronometer mChronometer;
private Thread mThreadChrono;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
mTvTime = (TextView) rootView.findViewById(R.id.chronometer);
mBtnStart = (Button) rootView.findViewById(R.id.start_button);
mBtnStop = (Button) rootView.findViewById(R.id.stop_button);
mBtnStart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(mChronometer == null) {
mChronometer = new Chronometer(mContext);
mThreadChrono = new Thread((Runnable) mChronometer);
mThreadChrono.start();
mChronometer.start();
}
}
});
mBtnStop.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(mChronometer !=null) {
mChronometer.stop();
mThreadChrono.interrupt();
mThreadChrono = null;
mChronometer = null;
}
}
});
return rootView;
}
public void updateTimerText(final String time) {
runOnUiThread(new Runnable() {
#Override
public void run () {
mTvTime.setText(time);
}
});
}
private void runOnUiThread(Runnable runnable) {
}
}
((MainFragment) mContext).updateTimerText(...) this is the way you want to call it. Check the squares around mContext.
Also, your method updateTimerText returns void so this line:
MainFragment mainFragment = (MainFragment) mContext.updateTimerText(String.format(
"%02d;%02d;%02d;%03d", hours, minutes, seconds, millis))
will raise another error. It should simply be:
((MainFragment) mContext).updateTimerText(...)
I can't help it, I have to suggest to use a different approach, first of all it's a bad idea to keep a reference to the context in a Runnable class.
Second of all, it's bad to cast Context to either Activity or Fragment, it will reduce the code reusability and cause unexpected crashes if the dev is not aware of this casts.
I'll do my best to explain my issue without a video
I have a login activity where upon successful login, the EditText and Button fields fade out and a "logging in" TextView fades in (using Facebook Shimmer). This works great!! However, upon successfully login we are greeted by a blank activity (still fine). Now, I overwrote the back button so that when the back button is pressed, the user is forced to login again. My problem arises with the user hits the login button at this time. The Edit Text and Button elements fade out nicely but the "logging in" TextView never fades in.
Below are some picture examples. I will also post the source code for LoginActivity.class and if you want to download the project it is available at: git#github.com:fbgrecojr/Android-Application-Login-Activity-Template.git
If you download the project, username: testuser and password: testpass will work.
Images:
Initial Login (working)
Login Attempt after pressing the back button and then logging in again (which I overwrote to restart the intent)
LoginActivity.class
package com.projects.fbgrecojr.logintemplate.UI;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Handler;
import android.support.design.widget.TextInputLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.DecelerateInterpolator;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;
import com.facebook.shimmer.ShimmerFrameLayout;
import com.projects.fbgrecojr.logintemplate.HTTPManager.HttpManager;
import com.projects.fbgrecojr.logintemplate.HTTPManager.RequestPackage;
import com.projects.fbgrecojr.logintemplate.Parser.JSONParser;
import com.projects.fbgrecojr.logintemplate.R;
import com.projects.fbgrecojr.logintemplate.Session.Session;
import com.projects.fbgrecojr.logintemplate.Structures.User;
import com.projects.fbgrecojr.logintemplate.Utility.UTILITY;
/**
* An example full-screen activity that shows and hides the system UI (i.e.
* status bar and navigation/system bar) with user interaction.
*/
public class LoginActivity extends AppCompatActivity implements View.OnClickListener{
private EditText userName, password;
private Button login;
private RelativeLayout image;
private LinearLayout button, belowPic;
private Animation fadeInImage, fadeInButton, bottomUp, fadeOut;
private TextInputLayout inputLayoutName,inputLayoutPassword;
private ViewGroup hiddenPanel;
private ShimmerFrameLayout container, loggingIn;
private static final int SECOND = 1000;
private static final int HALF_SECOND = 500;
private static final int QUARTER_SECOND = 250;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
//INITIALIZE ANIMATION ITEMS
fadeInImage = new AlphaAnimation(0, 1);
fadeInButton = new AlphaAnimation(0, 1);
fadeOut = new AlphaAnimation(1.0f,0.0f);
bottomUp = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.bottom_up_animation);
fadeInImage.setInterpolator(new AccelerateInterpolator()); //and this
bottomUp.setInterpolator(new DecelerateInterpolator());
//GET UI ELEMENTS
userName = (EditText) findViewById(R.id.userName);
password = (EditText) findViewById(R.id.password);
login = (Button) findViewById(R.id.login);
image = (RelativeLayout) findViewById(R.id.image);
button = (LinearLayout) findViewById(R.id.button);
container = (ShimmerFrameLayout) findViewById(R.id.shimmer);
belowPic = (LinearLayout) findViewById(R.id.below_picture);
loggingIn = (com.facebook.shimmer.ShimmerFrameLayout) findViewById(R.id.login_shimmer);
hiddenPanel = (ViewGroup)findViewById(R.id.input);
inputLayoutName = (TextInputLayout) findViewById(R.id.text_input_username);
inputLayoutPassword = (TextInputLayout) findViewById(R.id.text_input_password);
//SET UI PROPERTIES
loggingIn.setVisibility(View.INVISIBLE);
userName.setCursorVisible(false);
password.setCursorVisible(false);
password.setHint("Password");
userName.setHint("Username");
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
userName.setCursorVisible(true);
password.setCursorVisible(true);
userName.requestFocus();
}
}, LoginActivity.SECOND * 3);
//ANIMATIONS
fadeInImage.setDuration(SECOND * 3);
fadeOut.setStartOffset(SECOND);
fadeOut.setDuration(SECOND);
image.setAnimation(fadeInImage);
fadeInButton.setStartOffset(SECOND + HALF_SECOND + QUARTER_SECOND);
fadeInButton.setDuration(SECOND * 2);
button.setAnimation(fadeInButton);
hiddenPanel.startAnimation(bottomUp);
hiddenPanel.setVisibility(View.VISIBLE);
container.setDuration(SECOND * 2 + QUARTER_SECOND);
container.setRepeatDelay(QUARTER_SECOND);
container.setIntensity((float) 0.15);
container.setBaseAlpha((float) 0.75);
container.setFadingEdgeLength(3);
container.setDropoff((float) 0.40);
container.startShimmerAnimation();
//ON CLICK LISTENERS
login.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.login:
if(getUserName().getText().toString().equals("") || getUserName().getText().toString().equals(" ")) {
inputLayoutName.setError("enter username");
}else if(getPassword().getText().toString().equals("") || getPassword().getText().toString().equals(" ")){
inputLayoutPassword.setError("enter password");
}else{
//webservice
if (UTILITY.isOnline(getApplicationContext())) {
RequestPackage p = new RequestPackage();
p.setMethod("GET");
p.setUri(UTILITY.UBUNTU_SERVER_URL);
p.setParam("query", "user");
p.setParam("username", getUserName().getText().toString());
new WebserviceCallOne().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, p);
} else {
Toast.makeText(getApplicationContext(), "you are not connected to the internet", Toast.LENGTH_LONG).show();
}
}
break;
}
}
private void animateExit() {
//fade out annimation
belowPic.startAnimation(fadeOut);
belowPic.setVisibility(View.INVISIBLE);
fadeInImage.setStartOffset(SECOND * 2);
fadeInImage.setDuration(HALF_SECOND);
loggingIn.startAnimation(fadeInImage);
loggingIn.setVisibility(View.VISIBLE);
loggingIn.setDuration(SECOND);
loggingIn.startShimmerAnimation();
}
public EditText getPassword() {
return password;
}
public EditText getUserName() {
return userName;
}
private class WebserviceCallOne extends AsyncTask<RequestPackage, String, User> {
#Override
protected User doInBackground(RequestPackage... params) {
String content = HttpManager.getData(params[0]);
return JSONParser.parseUserFeed(content);
}
#Override
protected void onPostExecute(User s) {
Session.setCurrentUser(s);
//if null, error stacktrace will print to the log. This is expected!!
if(Session.getCurrentUser() == null){ //username was incorrect
inputLayoutName.setError("username does not exist");
}else{ //check password
if(getPassword().getText().toString().equals(s.getPassword())){ //passwords match
animateExit();
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
startActivity(new Intent(getApplicationContext(), MainActivity.class));
}
},LoginActivity.SECOND * 4);
}else{
inputLayoutPassword.setError("password incorrect");
}
}
}
}
}
MainActivity.class
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/**
* Take care of popping the fragment back stack or finishing the activity
* as appropriate.
*/
#Override
public void onBackPressed() {
startActivity(new Intent(this, LoginActivity.class));
}
}
You have to call animateExit(); code in onResume methord
#Override
public void onResume() {
super.onResume();
animateExit();
}
override onResume() in your Activity.
try starting your animation in onresume.
override onPause() in your Activity
try stopping your animation in onPause.
Hope this Helps :)