Related
Hello, I have two problems which I can't solve at all, but which I think are ultimately related.
I am triyng to do the following:
I have an activity (Nota) which serves as an add/edit note java class. I want to display these notes in the recyclerview in the ListFragment. And this I can do. However, when I tried to complicate things a bit more, I arrived at a dead end. For one, I want to sort the items according to 2 parameters, primarily by location and then by number of stars (in the rating bar), when the location is the same. Secondly, I also want to have a toggle button whose job is to switch these parameters, meaning firstly to filter according to the number of stars, and then according to the location. Any help would be appreciated.
Now, moving on to what I have tried. The repository fetches the data using LiveData, which I can't seem to use for comparisons. So, I have tried to transform this to a list, I have tried to use MediatorLiveData and many other ways. Either way, I am still stuck pretty much where I started. So, this is my code:
NoteDao
public interface NoteDao {
#Insert
void insert(Notev2 note);
#Update
void update(Notev2 note);
#Delete
void delete(Notev2 note);
#Query("DELETE FROM note_table")
void deleteAllNotes();
#Query("SELECT note_table.*, counter.count FROM note_table LEFT JOIN (SELECT note_table.location, count(note_table.location) as count FROM note_table GROUP BY note_table.location) counter ON counter.location = note_table.location ORDER BY counter.count DESC")
LiveData<List<Notev2>> getAllNotes();
}
This is my ListFragment
public class ListFragment extends Fragment {
public static final int ADD_NOTE_REQUEST = 1;
public static final int EDIT_NOTE_REQUEST = 2;
private NoteViewModel noteViewModel;
private ListViewModel listViewModel;
RecyclerView recyclerView_1;
public View onCreateView(#NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
listViewModel =
ViewModelProviders.of(this).get(ListViewModel.class);
View root = inflater.inflate(R.layout.fragment_list, container, false);
final TextView textView = root.findViewById(R.id.text_list);
listViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
#Override
public void onChanged(#Nullable String s) {
textView.setText(s);
}
});
recyclerView_1 = root.findViewById(R.id.recycler_view_list_1);
recyclerView_1.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView_1.setHasFixedSize(true);
final NoteAdapter nadapter = new NoteAdapter();
recyclerView_1.setAdapter(nadapter);
noteViewModel = ViewModelProviders.of(this).get(NoteViewModel.class);
noteViewModel.getAllNotes().observe(getActivity(), new Observer<List<Notev2>>() {
#Override
public void onChanged(List<Notev2> notev2s) {
nadapter.submitList(notev2s);
}
});
nadapter.setOnItemClickListener(new NoteAdapter.OnItemClickListener() {
#Override
public void onItemClick(Notev2 note) {
Intent intent = new Intent(getActivity(), Nota.class);
intent.putExtra(Nota.EXTRA_ID, note.getId());
intent.putExtra(Nota.EXTRA_TITLE, note.getTitle_v2());
intent.putExtra(Nota.EXTRA_LOCATION, note.getLocation_v2());
intent.putExtra(Nota.EXTRA_STARS, note.getStars_v2());
intent.putExtra(Nota.EXTRA_OPEN_1, note.getOpening_hours_1_v2());
intent.putExtra(Nota.EXTRA_OPEN_2, note.getOpening_hours_2_v2());
intent.putExtra(Nota.EXTRA_OPEN_3, note.getOpening_hours_3_v2());
intent.putExtra(Nota.EXTRA_NOTAS, note.getNotas_v2());
startActivityForResult(intent, EDIT_NOTE_REQUEST);
}
});
final ToggleButton swap = (ToggleButton) root.findViewById(R.id.toggle_stars_location);
swap.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (swap.isChecked()) {
swap.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.colorPrimary));
swap.setButtonDrawable(R.drawable.iswap_v2);
//noteViewModel.sortByStars();
nadapter.notifyDataSetChanged();
}
}
});
return root;
}
#Override
public void onViewCreated(View view , #Nullable Bundle savedInstanceState) {
Button button_add_nota = (Button) getView().findViewById(R.id.button_nota);
button_add_nota.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), Nota.class);
startActivityForResult(intent, ADD_NOTE_REQUEST);
}
});
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == ADD_NOTE_REQUEST && resultCode == MainActivity.RESULT_OK) {
String title = data.getStringExtra(Nota.EXTRA_TITLE);
String location = data.getStringExtra(Nota.EXTRA_LOCATION);
String stars = data.getStringExtra(Nota.EXTRA_STARS);
String open_1 = data.getStringExtra(Nota.EXTRA_OPEN_1);
String open_2 = data.getStringExtra(Nota.EXTRA_OPEN_2);
String open_3 = data.getStringExtra(Nota.EXTRA_OPEN_3);
String notas = data.getStringExtra(Nota.EXTRA_NOTAS);
Notev2 note = new Notev2(title, location, stars, open_1, open_2, open_3, notas);
noteViewModel.insert(note);
Toast.makeText(getActivity(), "Note saved", Toast.LENGTH_SHORT).show();
} else if (requestCode == EDIT_NOTE_REQUEST && resultCode == MainActivity.RESULT_OK) {
int id = data.getIntExtra(Nota.EXTRA_ID, -1);
if (id == -1) {
Toast.makeText(getActivity(), "Note can't be updated", Toast.LENGTH_SHORT).show();
return;
}
String title = data.getStringExtra(Nota.EXTRA_TITLE);
String location = data.getStringExtra(Nota.EXTRA_LOCATION);
String stars = data.getStringExtra(Nota.EXTRA_STARS);
String open_1 = data.getStringExtra(Nota.EXTRA_OPEN_1);
String open_2 = data.getStringExtra(Nota.EXTRA_OPEN_2);
String open_3 = data.getStringExtra(Nota.EXTRA_OPEN_3);
String notas = data.getStringExtra(Nota.EXTRA_NOTAS);
Notev2 note = new Notev2(title, location, stars, open_1, open_2, open_3, notas);
note.setId(id);
noteViewModel.update(note);
Toast.makeText(getActivity(), "Note updated", Toast.LENGTH_SHORT).show();
} else if (requestCode == EDIT_NOTE_REQUEST && resultCode == MainActivity.RESULT_OK +10) {
int id = data.getIntExtra(Nota.EXTRA_ID, -1);
String title = data.getStringExtra(Nota.EXTRA_TITLE);
String location = data.getStringExtra(Nota.EXTRA_LOCATION);
String stars = data.getStringExtra(Nota.EXTRA_STARS);
String open_1 = data.getStringExtra(Nota.EXTRA_OPEN_1);
String open_2 = data.getStringExtra(Nota.EXTRA_OPEN_2);
String open_3 = data.getStringExtra(Nota.EXTRA_OPEN_3);
String notas = data.getStringExtra(Nota.EXTRA_NOTAS);
Notev2 note = new Notev2(title, location, stars, open_1, open_2, open_3, notas);
note.setId(id);
noteViewModel.delete(note);
} else {
Toast.makeText(getActivity(), "Note not saved", Toast.LENGTH_SHORT).show();
}
}
}
This is my Nota Activity (which I use to add/edit notes)
button_save = (Button) findViewById(R.id.button_save);
button_save.setEnabled(true);
button_save.setClickable(true);
button_save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
saveNote();
}
});
EditText edit_new_sight = (EditText) findViewById(R.id.new_sight);
TextView opened_1 = findViewById(R.id.text_opened_1);
TextView opened_2 = findViewById(R.id.text_opened_2);
TextView opened_3 = findViewById(R.id.text_opened_3);
RatingBar priority_bar = (RatingBar) findViewById(R.id.priority_stars);
if (intent.hasExtra(EXTRA_ID)) {
delete_nota.setVisibility(View.VISIBLE);
toolbar_text.setText("Edit Sight");
edit_new_sight.setText(intent.getStringExtra(EXTRA_TITLE));
String slocation = intent.getStringExtra(EXTRA_LOCATION);
if (slocation.contains("zioiu"))
slocation = "";
editLocation.setText(slocation);
priority_bar.setRating(Float.parseFloat(intent.getStringExtra(EXTRA_STARS)));
opened_1.setText(intent.getStringExtra(EXTRA_OPEN_1));
opened_2.setText(intent.getStringExtra(EXTRA_OPEN_2));
opened_3.setText(intent.getStringExtra(EXTRA_OPEN_3));
notas.setText(intent.getStringExtra(EXTRA_NOTAS));
} else {
toolbar_text.setText("New Sight");
}
delete_nota.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openDialog();
}
});
}
private void saveNote() {
EditText noteTitlep = (EditText) findViewById(R.id.new_sight);
EditText noteLocationp = (EditText) findViewById(R.id.new_location);
RatingBar ratingBar = (RatingBar) findViewById(R.id.priority_stars);
String noteStars = String.valueOf(ratingBar.getRating());
String noteTitle = (String) "" + noteTitlep.getText();
if (noteTitle.equals("")) {
Toast toast = Toast.makeText(Nota.this, "Sorry, you need to input new sight!", Toast.LENGTH_LONG);
toast.show();
} else if (noteStars.equals("0.0")) {
Toast toast = Toast.makeText(Nota.this, "Sorry, you need to input stars!", Toast.LENGTH_LONG);
toast.show();
} else {
TextView noteOpen_1p = (TextView) findViewById(R.id.text_opened_1);
TextView noteOpen_2p = (TextView) findViewById(R.id.text_opened_2);
TextView noteOpen_3p = (TextView) findViewById(R.id.text_opened_3);
EditText noteNotasp = (EditText) findViewById(R.id.edit_notes);
String noteLocation = (String) "" + noteLocationp.getText();
String noteOpen_1 = (String) "" + noteOpen_1p.getText();
String noteOpen_2 = (String) "" + noteOpen_2p.getText();
String noteOpen_3 = (String) "" + noteOpen_3p.getText();
String noteNotas = (String) "" + noteNotasp.getText();
if (noteLocation.equals("")) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd G 'at' HH:mm:ss z");
String currentDateandTime = sdf.format(new Date());
noteLocation = "zioiu" + currentDateandTime;
}
Intent data = new Intent();
data.putExtra(EXTRA_TITLE, noteTitle);
data.putExtra(EXTRA_LOCATION, noteLocation);
data.putExtra(EXTRA_STARS, noteStars);
data.putExtra(EXTRA_OPEN_1, noteOpen_1);
data.putExtra(EXTRA_OPEN_2, noteOpen_2);
data.putExtra(EXTRA_OPEN_3, noteOpen_3);
data.putExtra(EXTRA_NOTAS, noteNotas);
int id = getIntent().getIntExtra(EXTRA_ID, -1);
if (id != -1) {
data.putExtra(EXTRA_ID, id);
}
setResult(RESULT_OK, data);
Toast.makeText(Nota.this, "Sight Saved", Toast.LENGTH_SHORT).show();
finish();
}
This is my NoteDatabase(v2)
public abstract class NoteDatabase_v2 extends RoomDatabase {
private static NoteDatabase_v2 instance;
public abstract NoteDao noteDao();
public static synchronized NoteDatabase_v2 getInstance(Context context) {
if (instance == null) {
instance = Room.databaseBuilder(context.getApplicationContext(),
NoteDatabase_v2.class, "note_database")
.fallbackToDestructiveMigration()
.addCallback(roomCallback)
.build();
}
return instance;
}
private static RoomDatabase.Callback roomCallback = new RoomDatabase.Callback() {
#Override
public void onCreate(#NonNull SupportSQLiteDatabase db) {
super.onCreate(db);
new PopulateDbAsyncTask(instance).execute();
}
};
#NonNull
#Override
protected SupportSQLiteOpenHelper createOpenHelper(DatabaseConfiguration config) {
return null;
}
#NonNull
#Override
protected InvalidationTracker createInvalidationTracker() {
return null;
}
#Override
public void clearAllTables() {
}
private static class PopulateDbAsyncTask extends AsyncTask<Void, Void, Void> {
private NoteDao noteDao;
private PopulateDbAsyncTask(NoteDatabase_v2 db) {
noteDao = db.noteDao();
}
#Override
protected Void doInBackground(Void... voids) {
noteDao.insert(new Notev2("Title 1", "Location 1", "1", "Open_1_1", "Open_2_1", "Open_3_1", "Notas_1"));
noteDao.insert(new Notev2("Title 2", "Location 2", "2", "Open_1_2", "Open_2_2", "Open_3_2", "Notas_2"));
noteDao.insert(new Notev2("Title 3", "Location 3", "3", "Open_1_3", "Open_2_3", "Open_3_3", "Notas_3"));
return null;
}
}
Sorry about so many javas, but there's still more
This is my NoteRepository
private NoteDao noteDao;
private LiveData<List<Notev2>> allNotes;
private MediatorLiveData<List<Notev2>> medallnotes;
public NoteRepository(Application application) {
NoteDatabase_v2 database = NoteDatabase_v2.getInstance(application);
noteDao = database.noteDao();
allNotes = noteDao.getAllNotes();
}
public void insert(Notev2 note) {
new InsertNoteAsyncTask(noteDao).execute(note);
}
public void update(Notev2 note) {
new UpdateNoteAsyncTask(noteDao).execute(note);
}
public void delete(Notev2 note) {
new DeleteNoteAsyncTask(noteDao).execute(note);
}
public void deleteAllNotes() {
new DeleteAllNotesAsyncTask(noteDao).execute();
}
public MediatorLiveData<List<Notev2>> getAllNotes() {
return (MediatorLiveData<List<Notev2>>) allNotes;
}
private static class InsertNoteAsyncTask extends AsyncTask<Notev2, Void, Void> {
private NoteDao noteDao;
private InsertNoteAsyncTask(NoteDao noteDao) {
this.noteDao = noteDao;
}
#Override
protected Void doInBackground(Notev2... notes) {
noteDao.insert(notes[0]);
return null;
}
}
private static class UpdateNoteAsyncTask extends AsyncTask<Notev2, Void, Void> {
private NoteDao noteDao;
private UpdateNoteAsyncTask(NoteDao noteDao) {
this.noteDao = noteDao;
}
#Override
protected Void doInBackground(Notev2... notes) {
noteDao.update(notes[0]);
return null;
}
}
private static class DeleteNoteAsyncTask extends AsyncTask<Notev2, Void, Void> {
private NoteDao noteDao;
private DeleteNoteAsyncTask(NoteDao noteDao) {
this.noteDao = noteDao;
}
#Override
protected Void doInBackground(Notev2... notes) {
noteDao.delete(notes[0]);
return null;
}
}
private static class DeleteAllNotesAsyncTask extends AsyncTask<Void, Void, Void> {
private NoteDao noteDao;
private DeleteAllNotesAsyncTask(NoteDao noteDao) {
this.noteDao = noteDao;
}
#Override
protected Void doInBackground(Void... voids) {
noteDao.deleteAllNotes();
return null;
}
}
}
This is my Note(v2)
#PrimaryKey(autoGenerate = true)
protected int id;
String title;
String location;
String stars;
String opening_hours_1;
String opening_hours_2;
String opening_hours_3;
String notas;
public Notev2(String title, String location, String stars, String opening_hours_1, String opening_hours_2,
String opening_hours_3, String notas) {
this.title = title;
this.location = location;
this.stars = stars;
this.opening_hours_1 = opening_hours_1;
this.opening_hours_2 = opening_hours_2;
this.opening_hours_3 = opening_hours_3;
this.notas = notas;
}
public void setId(int id) {
this.id = id;
}
public int getId() {
return id;
}
public String getTitle_v2() {
return title;
}
public String getLocation_v2() {
return location;
}
public String getStars_v2() {
return stars;
}
public String getOpening_hours_1_v2() {
return opening_hours_1;
}
public String getOpening_hours_2_v2() {
return opening_hours_2;
}
public String getOpening_hours_3_v2() {
return opening_hours_3;
}
public String getNotas_v2() {
return notas;
}
}
This is my NoteViewModel
public class NoteViewModel extends AndroidViewModel {
private NoteRepository repository;
private LiveData<List<Notev2>> allNotes;
public NoteViewModel(#NonNull Application application) {
super(application);
repository = new NoteRepository(application);
allNotes = repository.getAllNotes();
}
public void insert(Notev2 note) {
repository.insert(note);
}
public void update(Notev2 note) {
repository.update(note);
}
public void delete(Notev2 note) {
repository.delete(note);
}
public LiveData<List<Notev2>> getAllNotes() {
return allNotes;
}
public void sortByStars() {
Collections.sort((List<Notev2>) allNotes, ByStars);
}
public Comparator<Notev2> ByStars = new Comparator<Notev2>() {
#Override
public int compare(Notev2 o1, Notev2 o2) {
return Integer.valueOf(o1.stars).compareTo(Integer.valueOf(o2.stars));
}
};
}
And finally, this is my NoteAdapter
public class NoteAdapter extends ListAdapter<Notev2, NoteAdapter.NoteHolder> {
private OnItemClickListener listener;
public NoteAdapter() {
super(DIFF_CALLBACK);
}
private static final DiffUtil.ItemCallback<Notev2> DIFF_CALLBACK = new DiffUtil.ItemCallback<Notev2>() {
#Override
public boolean areItemsTheSame(Notev2 oldItem, Notev2 newItem) {
return oldItem.getId() == newItem.getId();
}
#Override
public boolean areContentsTheSame(Notev2 oldItem, Notev2 newItem) {
return oldItem.getTitle_v2().equals(newItem.getTitle_v2()) &&
oldItem.getLocation_v2().equals(newItem.getLocation_v2()) &&
oldItem.getStars_v2().equals(newItem.getStars_v2()) &&
oldItem.getOpening_hours_1_v2().equals(newItem.getOpening_hours_1_v2()) &&
oldItem.getOpening_hours_2_v2().equals(newItem.getOpening_hours_2_v2()) &&
oldItem.getOpening_hours_3_v2().equals(newItem.getOpening_hours_3_v2()) &&
oldItem.getNotas_v2().equals(newItem.getNotas_v2());
}
};
#NonNull
#Override
public NoteHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.cardview_list_1, parent, false);
return new NoteHolder(itemView);
}
#Override
public void onBindViewHolder(#NonNull NoteHolder holder, int position) {
Notev2 currentNote = getItem(position);
holder.nTitle.setText(currentNote.getTitle_v2());
String slocation = "" + currentNote.getLocation_v2();
if (slocation.contains("zioiu"))
slocation = "";
holder.nLocation.setText(slocation);
Float numStars = Float.parseFloat(currentNote.getStars_v2());
holder.nStars.setRating(numStars);
holder.nOpen.setText(new StringBuilder().append(currentNote.getOpening_hours_1_v2()).append(currentNote.getOpening_hours_2_v2()).append(currentNote.getOpening_hours_3_v2()).toString());
holder.nNotas.setText(currentNote.getNotas_v2());
}
public Notev2 getNoteAt(int position) {
return getItem(position);
}
class NoteHolder extends RecyclerView.ViewHolder {
TextView nTitle,nLocation,nOpen,nNotas;
RatingBar nStars;
public NoteHolder(View itemView) {
super(itemView);
nTitle = itemView.findViewById(R.id.textview_title);
nLocation = itemView.findViewById(R.id.textview_location);
nStars = itemView.findViewById(R.id.ratingbar_priority);
nOpen = itemView.findViewById(R.id.textview_openinghours);
nNotas = itemView.findViewById(R.id.textview_notas);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int position = getAdapterPosition();
if (listener != null && position != RecyclerView.NO_POSITION) {
listener.onItemClick(getItem(position));
}
}
});
}
}
public interface OnItemClickListener {
void onItemClick(Notev2 note);
}
public void setOnItemClickListener(OnItemClickListener listener) {
this.listener = listener;
}
}
Thank you very much if you managed to read it this far.
All help is welcome.
After much investigation, I finally arrived at an answer to my question. The key is to create new dao queries to filter the information, according to what you need.
This is my update NoteDao
#Dao
public interface NoteDao {
#Insert
void insert(Notev2 note);
#Update
void update(Notev2 note);
#Delete
void delete(Notev2 note);
#Query("DELETE FROM note_table")
void deleteAllNotes();
#Query("SELECT note_table.*, counter.count FROM note_table LEFT JOIN (SELECT note_table.location, count(note_table.location) as count FROM note_table GROUP BY note_table.location) counter ON counter.location = note_table.location ORDER BY counter.count DESC, stars DESC")
LiveData<List<Notev2>> getAllNotes();
#Query("SELECT note_table.*, counter.count FROM note_table LEFT JOIN (SELECT note_table.location, count(note_table.location) as count FROM note_table GROUP BY note_table.location) counter ON counter.location = note_table.location ORDER BY stars DESC, counter.count DESC")
LiveData<List<Notev2>> getAllNotesStars();
//#Query("SELECT location, count(location) FROM note_table GROUP BY location ORDER BY count(location) DESC ")
//LiveData<List<String>> getAllLocations();
}
Observer watches only to the LiveData's sourse, not to the field value. We need to switch liveData sourse
ListFragment
ViewModel.YOUR_VALUE.observe(viewLifecycleOwner){ list ->
list.let {
YOUR_ADAPTER.submitList(it)
}
}
YOUR_BUTTON.setOnClickListener {
ViewModel.sortByYOU_FUNCTION()
}
ViewModel file
val _YOUR_VALUE: MutableLiveData<Boolean> by lazy{MutableLiveData(false)}
val YOUR_VALUE =_YOUR_VALUE.switchMap { _allAnimals ->
when(_allAnimals){
true -> repository.SORTBY_YOURTH.asLiveData()
else -> repository.SORTBY_YOURTH.asLiveData()
}
}
fun sortByYOU_FUNCTION(){
_YOUR_VALUE.value = false
}
I have a realm database object each with 4 properties. In the first activity I put a Listview to display only one of the property, i.e Event. On clicking any one of the displayed events, I want to go to Activity 2 (details view) which will display all of the other 3 properties associated with the clicked Event. How to I do this with intent and how do I retrieve all the properties to be displayed individually in Activity 2?
public class Events extends RealmObject {
public String fileRef;
public String event;
public String venue;
public String date;
#Override
public String toString() {
return " \n Events{" +
"fileRef='" + fileRef + '\'' +
", parties='" + event + '\'' +
", \n court='" + venue + '\'' +
", courtAt='" + date + '\'' +
'}';
}
public String getFileRef() {
return fileRef;
}
public void setFileRef(String fileRef) {
this.fileRef = fileRef;
}
public String getEvent() {
return event;
}
public void setEvent(String event) {
this.event = event;
}
public String getVenue() {
return venue;
}
public void setVenue(String venue) {
this.venue = venue;
}
public String getDate() {
return date;
}
public void setCourtAt(String date) {
this.date = date;
}
}
The code below is in the 1st Activity. All I got is to pass only one property i.e event. How do I get to pass and retrieve all the properties in Activity2?
public class AllEventActivity extends AppCompatActivity {
private ArrayAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_all_events);
ListView list = (ListView) findViewById(R.id.listView);
Realm realm;
realm = Realm.getDefaultInstance();
RealmResults<Events> results = realm.where(Events.class).findAll();
ArrayList<String> rows = new ArrayList<>();
for (Events events : results)
{
rows.add(Events.event);
}
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, rows);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String selectedEvent = parent.getItemAtPosition(position).toString();
Intent eventDetailIntent = new Intent(AllEventActivity.this, Activity2.class);
eventDetailIntent.putExtra("event", selectedEvent);
startActivity(eventDetailIntent);
}
});
}
Below is the code in Activity2. I only managed to make the toast display the event.
public class Activity2 extends AppCompatActivity {
private String selectedEvent;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.event_detail);
selectedEvent = getIntent().getExtras().get("event").toString();
Toast.makeText(Activity2.this, selectedCase, Toast.LENGTH_LONG).show();
}
}
Your answer is much appreciated. Thank you very much.
You can use the Gson library, you need to add an import inside your gradle file to use it
With Gson you can cast your Events to String
Gson gson = new Gson();
intent.putExtra("event", gson.toJson(selectedEvent));
And you can get your object in your next activity with Gson
Gson gson = new Gson();
Events yourEvent = gson.fromJson(getIntent().getStringExtra("event"), Events.class);
Use the bundle,
public class Activity2 extends AppCompatActivity {
private String selectedEvent;
Bundle bundle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.event_detail);
bundle = getIntent().getExtras();
if (bundle != null){
try {
String selectedEvent = bundle.getString("event");
}catch (Exception e){
e.printStackTrace();
}
}
}
}
Please use this below code:
I passes Event model 1st to 2nd activity
Model Class
public class Events extends RealmObject implements Serializable {
public String fileRef;
public String event;
public String venue;
public String date;
#Override
public String toString() {
return " \n Events{" +
"fileRef='" + fileRef + '\'' +
", parties='" + event + '\'' +
", \n court='" + venue + '\'' +
", courtAt='" + date + '\'' +
'}';
}
public String getFileRef() {
return fileRef;
}
public void setFileRef(String fileRef) {
this.fileRef = fileRef;
}
public String getEvent() {
return event;
}
public void setEvent(String event) {
this.event = event;
}
public String getVenue() {
return venue;
}
public void setVenue(String venue) {
this.venue = venue;
}
public String getDate() {
return date;
}
public void setCourtAt(String date) {
this.date = date;
}
}
Liveview Click Event:
Intent eventDetailIntent = new Intent(AllEventActivity.this, Activity2.class);
eventDetailIntent.putExtra("event", results.get(position)); // Selected postion item
startActivity(eventDetailIntent);
In your 2nd Activity, you can retrieve your passed model data.
Events eventModel = ( Events) getIntent().getSerializableExtra("event");
So you will have your data in eventModel Object. Now you can get data from that model and set into your TextViews.
Like
textview1.setText(eventModel.getEvent())
textview2.setText(eventModel.getVenue())
textview3.setText(eventModel.getDate())
Hope it helps.
I want the Validation for the mobile number ,Am enter the edit text value search with database i want get the error already register your number for the validation setError Or Toast message,Add the Main class, Model class and AppUtil class kindly help me , Am attached code given below. Please tell what i set validation.
public class CreateRyotManagerFragment extends Fragment {
EditText mRyotName, mRyotFatherName, mAddress, mIdNo, mMobile1;
Spinner mDivision;
List<RyotMasterDb> mRyotList;
List<DivitionMasterDb> mDistrickarray;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.layout_new_ryot, container, false);
mMobile1 = view.findViewById(R.id.mobile);
mDivision = view.findViewById(R.id.divition);
mRyotList = AppUtils.getRyotMasterList();
mDistrickarray = AppUtils.getDistrictMasterLIst();
SpinnerAfdapterDistric divisionAdapter = new SpinnerAfdapterDistric(getActivity().getApplicationContext(), mDistrickarray);
mDivision.setAdapter(divisionAdapter);
}
}
#Table(name = "ryotMasterDB", database = AppDatabase.class)
RyotMasterDb extends Model implements Serializable {
#PrimaryKey
public Long id;
#Column(name = "mobile_1")
public String mobile_1;
#Column(name = "mobile_2")
public String mobile_2;
#Column(name = "zone_group_id")
public String zone_group_id;
#Column(name = "bank_id")
public String bank_id;
#Column(name = "branch_id")
public String branch_id;
#Column(name = "sb_ac_no")
public String sb_ac_no;
#Column(name = "state")
public String state;
#Column(name = "remoteID")
public String remoteID;
public RyotMasterDb() {
}
public RyotMasterDb(String remoteId, String imageData, String ryotNo, String catId, String name, String fatherName, String divId, String secID, String villID, String address, String address1, String add2, String add3, String iiSourceID, String irrType, String totalArea, String suitableArea, String pCode, String disID, String talukID, String firID, String factID, String pontentialRyot, String idProof, String idNo, String mobile1, String mobil2m,String passbook_image, String id_proof_image, String id_proof_back_side_image) {
this.remoteID = remoteId;
this.id = Long.getLong(remoteId);
this.mobile_1 = mobile1;
this.mobile_2 = mobil2m;
this.zone_group_id = zoneGroup;
this.bank_id = bankID;
this.branch_id = branchID;
this.sb_ac_no = sbAccNO;
this.state = state;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getMobile_1() {
return mobile_1;
}
public void setMobile_1(String mobile_1) {
this.mobile_1 = mobile_1;
}
public String getMobile_2() {
return mobile_2;
}
public void setMobile_2(String mobile_2) {
this.mobile_2 = mobile_2;
}
public String getZone_group_id() {
return zone_group_id;
}
public void setZone_group_id(String zone_group_id) {
this.zone_group_id = zone_group_id;
}
public String getBank_id() {
return bank_id;
}
public void setBank_id(String bank_id) {
this.bank_id = bank_id;
}
public String getBranch_id() {
return branch_id;
}
public void setBranch_id(String branch_id) {
this.branch_id = branch_id;
}
public String getSb_ac_no() {
return sb_ac_no;
}
public void setSb_ac_no(String sb_ac_no) {
this.sb_ac_no = sb_ac_no;
}
}
public class AppUtils {
static Fragment mFragment;
public static List<RyotMasterDb> getRyotMasterList() {
return Select.from(RyotMasterDb.class).fetch();
}
}
I suggest you use AutoCompleteTextView. An editable text view that shows completion suggestions automatically while the user is typing. The list of suggestions is displayed in a drop down menu from which the user can choose an item to replace the content of the edit box with.
The drop down can be dismissed at any time by pressing the back key or, if no item is selected in the drop down, by pressing the enter/dpad center key.
The list of suggestions is obtained from a data adapter and appears only after a given number of characters defined by the threshold.
Code Sample:
public class CountriesActivity extends Activity {
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.countries);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, COUNTRIES);
AutoCompleteTextView textView = (AutoCompleteTextView)
findViewById(R.id.countries_list);
textView.setAdapter(adapter);
}
private static final String[] COUNTRIES = new String[] {
"Belgium", "France", "Italy", "Germany", "Spain"
};
}
You can refer to this example for a detailed explanation.
You can set a keyListener for your EditText
editText.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if(event.getAction() == KeyEvent.ACTION_DOWN && keycode == KeyEvent.KEYCODE_ENTER){
// do your search in here
String mobileno=mmobile.getText().toString();
//search it
boolean isInList = checkIsPhoneNumberInList(mRyotList,mobileno);
Log.i("CheckResult","is my phone number "+mobileno+" in mRyotList: "+isInList);
if(isInList){
Toast.makeText(getActivity(),"your phone number is already registered",Toast.Toast.LENGTH_LONG).show();
}
}
return false;
}
});
for check whether phone which you input in your edittext is in your database by this method
private boolean checkIsPhoneNumberInList(List<RyotMasterDb> mRyotList,String phoneNumber){
if (mRyotList == null || phoneNumber == null) {
return false;
}
boolean isIn = false;
for (RyotMasterDb rm: mRyotList) {
if (phoneNumber.equals(rm.getMobile_1())||phoneNumber.equals(rm.getMobile_2())){
isIn = true;
}
}
return isIn;
}
I will try to be brief and precise:
I have a user object that contains a chat, and this is updated every time I receive a message:
public class Usuarios implements Serializable {
private static final long serialVersionUID = 1113799434508676095L;
private int id;
private String nombre;
private String ip;
private boolean isSInicial;
//HERE WILL GO THE VARIABLE THAT STORMS THE CHAT, BUT I DO NOT KNOW HOW TO IMPLEMENT IT
public Usuarios(int id, String nombre, String ip, boolean isSInicial){
this.id = id;
this.nombre = nombre;
this.ip = ip;
this.isSInicial = isSInicial;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getNombre() {
return nombre;
}
public void setNombre(String nombre) {
this.nombre = nombre;
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public boolean isSInicial() {
return isSInicial;
}
public void setSInicial(boolean SInicial) {
isSInicial = SInicial;
}}
I have an activity, that by clicking on a list, loads a new activity for the specific user:
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int pos, long id) {
Usuarios e = (Usuarios) listview.getItemAtPosition(pos);
Intent visorDetalles = new Intent(view.getContext(),Chat.class);
if(Contactos.listaDeContactos.indexOf(e) >= 0) {
visorDetalles.putExtra("indice", Contactos.listaDeContactos.indexOf(e));
startActivity(visorDetalles);
}
else{
Toast.makeText(contexto,"Usuario fuera de linea",Toast.LENGTH_LONG);
}
}
});
I need the EditText that contains the Intent to be updated automatically every time I click on a user of a ListView.
I do not know what kind of variable I should use in the user object and how to implement it.
In your Usuarios class add the variable:
public ArrayList<String> currentChat;
Instead of passing the index, pass the Object himself maybe.
if(Contactos.listaDeContactos.indexOf(e) >= 0) {
visorDetalles.putExtra("usuario",e);
startActivity(visorDetalles);
}
else{
Toast.makeText(contexto,"Usuario fuera de linea",Toast.LENGTH_LONG);
}
And in your onCreate Chat Activity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat);
Intent intent = getIntent();
Usuario usuario = (Usuario) intent.getSerializableExtra("usuario");
EditText myEditText = (EditText)findViewById(R.id.myEditText1));
String allChat;
for(String chatLine : usuario.currentChat) {
allChat += chatLine + '\n';
}
myEditText.setText(allChat);
Did I got your problem right ?
I have a public class with one public static String method. I have to getString() and I know that before that I have to extract String ressource and pass Context and I dont know how to pass it .
Another class uses public static String method
Code example:
public class StringUtil {
public static String sumUp(int nr1, int nr2) {
int sum = nr1 + nr2;
String result;
//all what is in ""
result =.getString(R.string.result) + sum;
return result;
}
}
ButtonListener class uses public static String method. Code:
public class ButtonListener extends MainActivity implements View.OnClickListener {
MainActivity activity;
EditText et1;
EditText et2;
public ButtonListener(MainActivity activity) {
this.activity = activity;
}
#Override
public void onClick(View view) {
switch (view.getId()){
case R.id.sum:
et1 = (EditText) activity.findViewById(R.id.nr1_edit_text);
et2 = (EditText)activity.findViewById(R.id.nr2_edit_text);
String nr1In = et1.getText().toString();
String nr2In = et1.getText().toString();
int nr1 = Integer.parseInt(nr1In);
int nr2 = Integer.parseInt(nr2In);
Intent intent = new Intent(activity, IntentActivity.class);
intent.putExtra("result", StringUtil.sumUp(nr1, nr2));
activity.startActivity(intent);
break;
}
}
}
IntentActivity class ´getIntent´ Code:
public class IntentActivity extends MainActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
TextView textView = (TextView)findViewById(R.id.result);
Intent intent = getIntent();
String fResult = intent.getStringExtra("result");
textView.setText(fResult);
}
}
And MainActivity Code:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButtonListener bl = new ButtonListener(this);
Button sumUp= (Button)findViewById(R.id.sum_button);
sumUp.setOnClickListener(bl);
}
}
public class StringUtil {
public static String sumUp(int nr1, int nr2, Context context){
int sum = nr1 +nr2;
String result;
result = context.getString(R.string.result) + sum;
return result;
}
}
And use it like:
StringUtil.sumUp(1,2, this); // from Activity
StringUtil.sumUp(1,2, getContext()); // from Fragment
You have 2 ways:
1.to pass it as a parameter for your method:
public static String sumUp(int nr1, int nr2 ,Context context){
int sum = nr1 +nr2;
String result;
//all what is in ""
result = .getString(R.string.result) + sum;
return result;
}
2.to add context to the class constructor and then you can use it inside your method:
public class StringUtil {
Context context;
public void StringUtil (Context context){
this.context = context;
}
public static String sumUp(int nr1, int nr2){
int sum = nr1 +nr2;
String result;
//all what is in ""
result = .getString(R.string.result) + sum;
return result;
}
}
Replace your StringUtil class with this.
public class StringUtil {
public static String sumUp(Context mContext, int nr1, int nr2){
int sum = nr1 +nr2;
String result;
//all what is in ""
result = mContext.getString(R.string.result) + sum;
return result;
}
}
Edit
And call this method from your ButtonListener activity.
intent.putExtra("result", StringUtil.sumUp(ButtonListener.this, nr1, nr2));