store multiple views in recylerview using sharedpreferences on runtime - java

Hi am using a "Add" button in ActionBar of my app on clicking it a ListView gets open and the user can tap on any list item to add that item to the RecyclerView which is the parent activity(MainActivity holding the Recyclerview) but on exiting the app the last added item stays on MainActivity's view. I guess SharedPreferences' Editor is getting overwritten everytime. Can you please help in providing some snippet as i can't call editor.put() method multiple times as the click happens on listView. My RecyclerView contains an image and a text.
Here is my MainActivity's Code
package com.example.mohitmehndiratta.customlistadap;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Icon;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
import static android.support.v7.widget.LinearLayoutManager.*;
public class MainActivity extends AppCompatActivity {
public static ArrayList<DataSet> alist;
int i;
RecyclerView rv;
static RecycledAdap adap;
static String args;
static int rid;
static SharedPreferences sharedPreferences;
public static void addnow(String arg)
{
args=arg;
rid=R.drawable.i;
alist.add(new DataSet(args,rid));
adap.notifyDataSetChanged();
SharedPreferences.Editor editor=sharedPreferences.edit();
editor.putInt("Iconid",rid);
editor.putString("AppName",args);
editor.apply();
if (sharedPreferences!=null)
{
int x=sharedPreferences.getInt("Iconid",rid);
String y=sharedPreferences.getString("AppName",args);
addnow(y);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater=getMenuInflater();
inflater.inflate(R.menu.new_menu,menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId()==R.id.addbtn)
{
Intent intent=new Intent(this,AppList.class);
startActivity(intent);
}
return super.onOptionsItemSelected(item);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sharedPreferences = getSharedPreferences("MyPrefs",getApplicationContext().MODE_PRIVATE);
rv=(RecyclerView)findViewById(R.id.rv);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
rv.setLayoutManager(mLayoutManager);
rv.setItemAnimator(new DefaultItemAnimator());
rv.addItemDecoration(new ItemDecoration(this, LinearLayoutManager.VERTICAL));
for(i=0;i<DataModel.name.length;i++) {
alist = new ArrayList<DataSet>();
alist.add(new DataSet("Paytm",R.drawable.i));
alist.add(new DataSet("Facebook",R.drawable.i1));
alist.add(new DataSet("ShareIt",R.drawable.i2));
alist.add(new DataSet("Instagram",R.drawable.i3));
alist.add(new DataSet("BookMyShow",R.drawable.i4));
}
adap=new RecycledAdap(getApplicationContext(),alist);
rv.setAdapter(adap);
MyListener mlistener=new MyListener(getApplicationContext(),rv, new MyListener.ReClickListener() {
#Override
public void onClick(int position) {
Toast.makeText(getApplicationContext(),"Launching item"+position,Toast.LENGTH_SHORT).show();
LauncherHandler lh=new LauncherHandler(position);
String pkname=lh.getpack();
AppDialog adialog=new AppDialog();
adialog.packagenameset(pkname);
adialog.show(getFragmentManager(),"AppDialogFrag");
}
#Override
public void onLongClick(int position) {
LauncherHandler lh=new LauncherHandler(position);
String pkname=lh.getpack();
Toast.makeText(getApplicationContext(),"Launching item"+position,Toast.LENGTH_SHORT).show();
AppDialog adialog=new AppDialog();
adialog.packagenameset(pkname);
adialog.show(getFragmentManager(),"AppDialogFrag");
}
});
rv.addOnItemTouchListener(mlistener);
if (sharedPreferences!=null)
{
int x=sharedPreferences.getInt("Iconid",rid);
String y=sharedPreferences.getString("AppName",args);
addnow(y);
}
else
{
Toast.makeText(getApplication(),"There is nothing in app's cache",Toast.LENGTH_SHORT).show();
}
}
}
ListView code :
package com.example.mohitmehndiratta.customlistadap;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class AppList extends AppCompatActivity {
ListView lv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_app_list);
PackageManager pm=getPackageManager();
ArrayList<ApplicationInfo> li= (ArrayList<ApplicationInfo>) pm.getInstalledApplications(0);
ArrayList al=new ArrayList<String>();
String str;
String strpk;
ApplicationInfo info;
for(int i=0;i<li.size();i++)
{
info=li.get(i);
str=info.loadLabel(pm).toString();
Drawable appicon=info.loadIcon(pm);
al.add(str);
}
lv=(ListView)findViewById(R.id.listView);
final ArrayAdapter adap=new ArrayAdapter<String>(this,R.layout.support_simple_spinner_dropdown_item,al);
lv.setAdapter(adap);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String text=lv.getItemAtPosition(i).toString();
Toast.makeText(getApplicationContext(),text+i,Toast.LENGTH_SHORT).show();
MainActivity.addnow(text);
}
});
}
}

Related

Notify adapter on item inserted in RoomDatabase and update the recyclerView

I am creating a notes app with java, i added room database to my app and when user saves a notes it adds it to database but doesn't shows in recyclerView immediately, when i reloads the app then it shows up, how and where should i insert notifyiteminserted so that recyclerView changes immediately
I have tried onResume Method but that results in app crash.
This is my MainActivity.
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
static ArrayList<Notes> arrNotes;
#SuppressLint("StaticFieldLeak")
RecyclerViewAdapter adapter;
RecyclerView.LayoutManager layoutManager;
notesModelView modelView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DatabaseHelper dbHelper = DatabaseHelper.getDatabase(this);
arrNotes = (ArrayList<Notes>) dbHelper.notesDao().getAllNotes();
RecyclerView recyclerView = findViewById(R.id.recycler_view);
adapter = new RecyclerViewAdapter(this, arrNotes);
recyclerView.setAdapter(adapter);
//setting up recycler view
layoutManager = new StaggeredGridLayoutManager(2, LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(layoutManager);
//Setting custom Toolbar
Toolbar toolbar1 = findViewById(R.id.toolbar);
setSupportActionBar(toolbar1);
//Moving from MainActivity to add_Notes Activity
Button button = findViewById(R.id.floatingActionButton);
button.setOnClickListener(view -> {
Intent intent = new Intent(MainActivity.this, addActivity.class);
startActivity(intent);
});
}
}
This is my add_Activity.
package com.example.keepnotes;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import java.util.Objects;
public class addActivity extends AppCompatActivity {
MainActivity mainActivity;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
Toolbar toolbar_add = findViewById(R.id.toolbar_add_activity);
setSupportActionBar(toolbar_add);
Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);
toolbar_add.setNavigationIcon(R.drawable.back_button);
toolbar_add.setNavigationOnClickListener(view -> onBackPressed());
EditText titleText = findViewById(R.id.add_activity_title);
EditText bodyText = findViewById(R.id.add_activity_text);
Button saveBtn = findViewById(R.id.button);
DatabaseHelper database = DatabaseHelper.getDatabase(this);
saveBtn.setOnClickListener(view -> {
String titleBody = titleText.getText().toString();
String textBody = bodyText.getText().toString();
if (titleBody.equals("") && textBody.equals("")) {
Toast.makeText(addActivity.this, "Fields can't be empty",
Toast.LENGTH_LONG).show();
} else {
database.notesDao().addNotes(new Notes(titleBody, textBody));
finish();
}
});
}
}
How can i notify adapter the changes on each item add in database.
Here is my MainActivity after update to liveData.
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
static ArrayList<Notes> arrNotes;
#SuppressLint("StaticFieldLeak")
RecyclerViewAdapter adapter;
RecyclerView.LayoutManager layoutManager;
notesModelView modelView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DatabaseHelper dbHelper = DatabaseHelper.getDatabase(this);
modelView = new ViewModelProvider(this).get(notesModelView.class);
modelView.getAllNotes().observe(this, new Observer<List<Notes>>() {
#Override
public void onChanged(List<Notes> notes) {
arrNotes = (ArrayList<Notes>) notes;
}
});
arrNotes = (ArrayList<Notes>) dbHelper.notesDao().getAllNotes();
RecyclerView recyclerView = findViewById(R.id.recycler_view);
adapter = new RecyclerViewAdapter(this, arrNotes);
recyclerView.setAdapter(adapter);
//setting up recycler view
layoutManager = new StaggeredGridLayoutManager(2, LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(layoutManager);
//Setting custom Toolbar
Toolbar toolbar1 = findViewById(R.id.toolbar);
setSupportActionBar(toolbar1);
//Moving from MainActivity to add_Notes Activity
Button button = findViewById(R.id.floatingActionButton);
button.setOnClickListener(view -> {
Intent intent = new Intent(MainActivity.this, addActivity.class);
startActivity(intent);
});
}
}
this is Dao.
import androidx.lifecycle.LiveData;
import androidx.room.Dao;
import androidx.room.Delete;
import androidx.room.Insert;
import androidx.room.Query;
import androidx.room.Update;
import java.util.List;
#Dao
public interface NotesDao {
#Query("SELECT * FROM notesTable")
List<Notes> getAllNotes();
#Query("SELECT * FROM notesTable")
LiveData<List<Notes>> findAllNotes();
#Insert
void addNotes(Notes note);
#Update
void updateNotes(Notes note);
#Delete
void deleteNotes(Notes note);
}
And here is my ViewModel
package com.example.keepnotes;
import android.app.Application;
import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import java.util.List;
public class notesModelView extends AndroidViewModel {
DatabaseHelper databaseHelper;
public notesModelView(#NonNull Application application) {
super(application);
databaseHelper = DatabaseHelper.getDatabase(application.getApplicationContext());
}
public LiveData<List<Notes>> getAllNotes() {
return databaseHelper.notesDao().findAllNotes();
}
}
Here is my RecyclerView adapter
package com.example.keepnotes;
import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
Context context;
ArrayList<Notes> arrNotes;
DatabaseHelper databaseHelper;
RecyclerViewAdapter(Context context, ArrayList<Notes> arrNotes, DatabaseHelper databaseHelper) {
this.context = context;
this.arrNotes = arrNotes;
this.databaseHelper = databaseHelper;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.single_view, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, #SuppressLint("RecyclerView") int position) {
holder.title.setText(arrNotes.get(position).title);
holder.body.setText(arrNotes.get(position).text);
holder.index.setText(String.valueOf(position + 1));
holder.llView.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View view) {
AlertDialog.Builder alert = new AlertDialog.Builder(context)
.setTitle("Delete view")
.setMessage("Are you sure to delete")
.setIcon(R.drawable.ic_baseline_delete_24)
.setPositiveButton("yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
databaseHelper.notesDao().deleteNotes(new Notes(arrNotes.get(position).id,arrNotes.get(position).title,arrNotes.get(position).text));
notifyItemRemoved(position);
notifyItemRangeChanged(position, arrNotes.size());
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
alert.show();
return true;
}
});
}
#Override
public int getItemCount() {
return arrNotes.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView title, body, index;
CardView llView;
public ViewHolder(View itemView) {
super(itemView);
title = itemView.findViewById(R.id.text_title_view);
body = itemView.findViewById(R.id.text_text_view);
index = itemView.findViewById(R.id.index);
llView = itemView.findViewById(R.id.card_View);
databaseHelper = DatabaseHelper.getDatabase(context);
}
}
}
It deletes the selected notes but also crashes immediately after confirming delete.
and it throws following error
java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionViewHolder{535e3b3 position=5 id=-1, oldPos=4, pLpos:4 scrap [attachedScrap] tmpDetached not recyclable(1) no parent} androidx.recyclerview.widget.RecyclerView{11f4816 VFED..... ......ID 31,171-689,1048 #7f090165 app:id/recycler_view}, adapter:com.example.keepnotes.RecyclerViewAdapter#fd652a0, layout:androidx.recyclerview.widget.StaggeredGridLayoutManager#32e1e59, context:com.example.keepnotes.MainActivity#286bccd
Remove this two line
notifyItemRemoved(position);
notifyItemRangeChanged(position, arrNotes.size());
OLD ANSWER
First, if you use liveData you don't need to call the method
arrNotes = (ArrayList<Notes>) dbHelper.notesDao().getAllNotes();
just keep a reference to the adapter instance and whenever there is a change from liveData call the notifyDataSetChanged method.
adapter = new RecyclerViewAdapter(this,new List<Notes>());
RecyclerView recyclerView = findViewById(R.id.recycler_view);
recyclerView.setAdapter(adapter);
modelView.getAllNotes().observe(this, new Observer<List<Notes>>() {
#Override
public void onChanged(List<Notes> notes) {
arrNotes.clear();
arrNotes.addAll(notes);
adapter.notifyDataSetChanged()
}
});
observe a LiveData object as it is not a LifecycleOwner
Use observeForever() on the LiveData, manually unregistering via removeObserver() when appropriate (onDestroy() of the service, if not sooner).
Bear in mind that standard service limitations apply here (e.g., services run for ~1 minute on Android 8.0+ unless they are foreground services), so it may be that you need to consider other approaches anyway. 🤞
Also you can read more in original post in here -> Link

RecyclerView don't show items even if I call notifyDataSetChanged()

I'm using a RecyclerView to show datas but even if I call notifyDataSetChanged() after I got the list from Room db I can't see the items on the first creation of the fragment, but only on second one.
MainActivity.class
package it.bastoner.taboom;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.view.MenuItem;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.room.Room;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.navigation.NavigationBarView;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import it.bastoner.taboom.database.CardDAO;
import it.bastoner.taboom.database.CardEntity;
import it.bastoner.taboom.database.DatabaseTaboom;
import it.bastoner.taboom.fragments.AddFragment;
import it.bastoner.taboom.fragments.BaseCardFragment;
import it.bastoner.taboom.fragments.PlayFragment;
import it.bastoner.taboom.fragments.UpdateFragment;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private List<CardEntity> cardList;
private boolean cardListIsUpdated;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(TAG, ">>MainActivity created");
cardList = new ArrayList<CardEntity>();
// Setting bottomNavigation
BottomNavigationView bottomNav = findViewById(R.id.bottom_nav);
bottomNav.setOnItemSelectedListener(new NavigationBarView.OnItemSelectedListener() {
//TODO ANIMATION
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
// By using switch we can easily get the selected fragment by using there id.
Fragment selectedFragment = null;
switch (item.getItemId()) {
case R.id.add_nav:
selectedFragment = new AddFragment(cardList);
break;
case R.id.play_nav:
selectedFragment = new PlayFragment(cardList);
break;
case R.id.update_nav:
selectedFragment = new UpdateFragment(cardList);
break;
}
// It will help to replace the
// one fragment to other.
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragment_container, selectedFragment)
.commit();
return true;
}
});
// As soon as the application opens the play fragment should be shown to the user
bottomNav.setSelectedItemId(R.id.play_nav);
loadCardList();
}
private void loadCardList() {
CardDAO cardDAO = DatabaseTaboom.getDatabase(getApplicationContext()).cardDao();
// Setting cardsList
ExecutorService executor = Executors.newSingleThreadExecutor();
Runnable runnable = new Runnable() {
#Override
public void run() {
cardList = cardDAO.getAll();
Log.d(TAG, ">>Cardlist: " + cardList);
BaseCardFragment actualFragment = (BaseCardFragment) getSelectedFragment();
if (actualFragment != null)
actualFragment.updateUI();
else
Log.d(TAG, ">>Null fragment");
}
};
executor.execute(runnable);
}
private Fragment getSelectedFragment() {
FragmentManager fragmentManager = getSupportFragmentManager();
List<Fragment> fragmentList = fragmentManager.getFragments();
for (Fragment f: fragmentList) {
if (f != null && f.isVisible())
return f;
}
return null;
}
}
PlayFragment.class
package it.bastoner.taboom.fragments;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.media.MediaPlayer;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.PagerSnapHelper;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.SnapHelper;
import android.os.CountDownTimer;
import android.text.InputFilter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.util.List;
import java.util.Locale;
import it.bastoner.taboom.R;
import it.bastoner.taboom.adapter.RecyclerViewAdapter;
import it.bastoner.taboom.database.CardEntity;
import it.bastoner.taboom.filters.MinMaxFilter;
import it.bastoner.taboom.listeners.MyDialogListener;
public class PlayFragment extends BaseCardFragment implements MyDialogListener {
private static final String TAG = "PlayFragment";
private RecyclerView recyclerView;
private TextView timerTextView;
private Button playPauseButton;
private Button resetButton;
private CountDownTimer countDownTimer;
private long startTimeInMillis;
private boolean timerIsRunning;
private long timeLeftInMillis = startTimeInMillis;
private MediaPlayer endTimerSound;
public PlayFragment(List<CardEntity> cardList) {
super(cardList);
//Log.d(TAG, "PlayFragment created");
}
#Override
public void updateUI() {
recyclerView.getAdapter().notifyDataSetChanged();
Log.d(TAG, ">>Update, number of items: " + recyclerView.getAdapter().getItemCount());
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.d(TAG, ">>OnCreateView");
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_play, container, false);
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Log.d(TAG, ">>OnViewCreated");
setRecyclerView();
setTimerSound();
setTimer();
setDialogModifyTimer();
updateUI();
}
// other methods...
}
As you can see in the console the cardList has received elements before the call of UpdateUI() but in update the first time is called no elements are seen. Can you help me? I don't understand why, thank you.
I think you need to set the Data in the RecyclerAdapter. Then call it inside your fragment with a ViewModel. I'm using Kotlin sorry.
This works only if u are using RoomDB with viewModel and repositories
In your RecyclerAdapter, you first need to set an empty list
private var timerList = emptyList<EntityClassName>()
// Now use a method to set the Data
fun setData(timer : List<EntityClassName>){
this.timerList = timer
notifyDataSetChanged()
}
Then you call this on your Fragment inside OnViewCreated.
sharedViewModel.readAllData.observe(viewLifecycleOwner,Observer {
data -> myAdapter.setData(data)})

Struggling with Shared Preferences and Lists

I'm busy with a very basic Note Taking Android App and having trouble with shared preferences.
In MainActivity is(should be) a list of the notes that were previously taken. To make a note you press menu and select make a note, that will take you to the second activity(writeANote.java).From there you make your note press menu and select add note, the it should be in in the listView(noteList). But its not... i think my problem is in MainActivity where I try to get the ArrayList from shared preferences.
I also have the ObjectSerializer class.
Please help.
Here is my code for the MainActivity.
package com.example.makeanote;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
static SharedPreferences sp;
static ListView noteList;
//static ArrayList<String> noteArray;
//static ArrayAdapter<String> adapter;
static ArrayAdapter<String> spListAdapter;
ArrayList<String> newSpList;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater=getMenuInflater();
menuInflater.inflate(R.menu.main_menu,menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(#NonNull MenuItem item) {
super.onOptionsItemSelected(item);
switch(item.getItemId()){
case(R.id.makeNewNote):
Intent i=new Intent(getApplicationContext(),writeANote.class);
startActivity(i);
Log.i("SELECTED","make New Note");
return true;
default:
return false;
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
noteList=(ListView)findViewById(R.id.noteList);
//noteArray= new ArrayList<String>();
//adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,noteArray);
try {
newSpList=(ArrayList<String>)ObjectSerializer.deserialize((sp.getString("spList",ObjectSerializer.serialize(new ArrayList<String>()))));
Log.i("new SP List", newSpList.get(0));
}catch(Exception e){
e.printStackTrace();
}
spListAdapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,newSpList);
sp= (SharedPreferences) getSharedPreferences("com.example.makeanote", Context.MODE_PRIVATE);
//String test=sp.getString("test","nothing");
newSpList.add("test");
//noteArray.add(test);
if (newSpList.size()!=0) {
noteList.setAdapter(spListAdapter);
}else{
Toast.makeText(this, "No Notes So Far", Toast.LENGTH_SHORT).show();
}
}
}
Here is my code for the second activity(writeANote.java).
package com.example.makeanote;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.EditText;
import java.util.ArrayList;
import static com.example.makeanote.MainActivity.sp;
public class writeANote extends AppCompatActivity {
EditText note;
ArrayList<String> spList;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater=getMenuInflater();
menuInflater.inflate(R.menu.menu_2,menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(#NonNull MenuItem item) {
super.onOptionsItemSelected(item);
switch(item.getItemId()){
case(R.id.addNote):
//sp.edit().putString("test",).apply();
spList.add(note.getText().toString());
try {
MainActivity.sp.edit().putString("spList",ObjectSerializer.serialize(spList)).apply();
}catch(Exception e){
e.printStackTrace();
}
Intent i=new Intent(getApplicationContext(),MainActivity.class);
startActivity(i);
Log.i("SELECTED","add New Note");
return true;
default:
return false;
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_write_a_note);
spList=new ArrayList<>();
note=findViewById(R.id.note);
}
}

using bitmap with custom spinner

I used this tutorial to make a custom spinner but now I'm having a java.lang.OutOfMemoryError: Failed to allocate a 95976012 byte allocation with 16777120 free bytes and 31MB until OOMerror. I am aware that I should use bitmaps I just don't know how to include the bitmapFactory in my CustomAdapter. I have already compressed my images to sizes less than 16kb and put them in the mipmap folder because they are icons. I have also already tried
<application
android:largeHeap="true"
</application>
Here's my code :
MainActivity.java
package com.kathure.flags;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.Toast;
import static android.R.attr.id;
import static android.graphics.BitmapFactory.*;
import static java.security.AccessController.getContext;
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
String[] countryNames= {
"Kenya" , "Malawi", "Nigeria", "Rwanda", "Tanzania", "Uganda"
};
int [] flags = {
R.mipmap.kenyamin,R.mipmap.malawimin, R.mipmap.nigeriamin, R.mipmap.rwandamin, R.mipmap.tanzaniamin, R.mipmap.ugandamin
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//Getting the instance of Spinner and applying OnItemSelectedListener on it
Spinner spin = (Spinner) findViewById(R.id.simpleSpinner);
spin.setOnItemSelectedListener(this);
CustomAdapter customAdapter=new CustomAdapter(getApplicationContext(),flags,countryNames);
spin.setAdapter(customAdapter);
}
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(getApplicationContext(), countryNames[i], Toast.LENGTH_LONG).show();
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
}
CustomAdapter.java
package com.kathure.flags;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
/**
* Created by kathure on 28/09/16.
*/
public class CustomAdapter extends BaseAdapter {
Context context;
int flags[];
String[] countryNames;
LayoutInflater inflter;
public CustomAdapter(Context applicationContext, int[] flags, String[] countryNames) {
this.context = applicationContext;
this.flags = flags;
this.countryNames = countryNames;
inflter = (LayoutInflater.from(applicationContext));
}
#Override
public int getCount() {
return flags.length;
}
#Override
public Object getItem(int i) {
return null;
}
#Override
public long getItemId(int i) {
return 0;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
view = inflter.inflate(R.layout.custom_spinner_items, null);
ImageView icon = (ImageView) view.findViewById(R.id.imageView);
TextView names = (TextView) view.findViewById(R.id.textView);
icon.setImageResource(flags[i]);
names.setText(countryNames[i]);
return view;
}
}

Data not being displayed when switching between tabs

I am using the https://github.com/astuetz/PagerSlidingTabStrip/ library. My activity consists of 3 "tabs" and each of these tabs has it's own fragment.
The problem is that whenever I load tab 3, all the data in tab 1 is lost and will not be refreshed.
All 3 fragments are filled with a listview and have similar code.
I can't figure out why it keeps happening, but it seems as if whenever I load a tab that is not the direct "neighbour" of the currently selected tab, the currently selected tab gets unloaded.
Basically, I have 3 tabs. If I load tab 3, tab 1 unloads and doesn't reload. Tab 2 always remains filled (I assume because tab 2 is always either a] the neighbour of tab 1 or b] the neighbour of tab 3).
OrdersActivity.java
package nl.*.android.order;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import com.astuetz.PagerSlidingTabStrip;
import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;
import nl.*.android.DrawerActivity;
import nl.*.android.home.HomeActivity_;
import nl.*.android.R;
import nl.*.android.utility.IFragmentSetter;
import nl.*.android.utility.TabsPagerAdapter;
#EActivity(R.layout.activity_orders)
public class OrdersActivity extends DrawerActivity implements IFragmentSetter {
#ViewById
public ViewPager viewpager;
#ViewById
public PagerSlidingTabStrip strip;
private TabsPagerAdapter adapter;
#AfterViews
public void init() {
buildDrawer();
adapter = new TabsPagerAdapter(getSupportFragmentManager(), this);
addFragments();
viewpager.setAdapter(adapter);
strip.setViewPager(viewpager);
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle("Bestellingen");
getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_menu_white);
}
}
private void addFragments() {
adapter.addFragment(new OrdersNewFragment_(), "Nieuw");
adapter.addFragment(new OrdersDeliveryFragment_(), "Afleveren");
adapter.addFragment(new OrdersPickupFragment_(), "Ophalen");
}
#Override
public void setFragment(Fragment fragment) {
//geen implementatie nodig
}
#Override
public void onBackPressed() {
super.onBackPressed();
final Intent i = HomeActivity_.intent(getApplicationContext()).get();
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
finish();
}
}
OrdersNewFragment.java [Tab 1]
package nl.*.android.order;
import android.support.v4.app.Fragment;
import android.widget.ListView;
import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import nl.*.android.R;
import nl.*.android.backend.API;
import nl.*.android.backend.APIFactory;
import nl.*.android.model.MOrderItem;
import nl.*.android.model.MOrders;
#EFragment(R.layout.fragment_orders_overview)
public class OrdersNewFragment extends Fragment {
#Bean
public APIFactory apiFactory;
private API api;
private List<MOrderItem> items = new ArrayList<>();
private OrderListAdapter adapter;
#ViewById
public ListView ordersList;
#AfterViews
public void init() {
api = apiFactory.get();
getNewOrders();
adapter = new OrderListAdapter(getActivity(), R.id.ordersList, items);
ordersList.setAdapter(adapter);
dataChanged();
}
#UiThread
public void dataChanged() {
adapter.notifyDataSetChanged();
}
#Background
public void getNewOrders() {
items.clear();
final MOrders result = api.getOrdersForNew();
if (result != null) {
Collections.addAll(items, result.result.orderList);
}
}
}
OrdersDeliveryFragment.java [Tab 2]
package nl.*.android.order;
import android.support.v4.app.Fragment;
import android.widget.ListView;
import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import nl.*.android.R;
import nl.*.android.backend.API;
import nl.*.android.backend.APIFactory;
import nl.*.android.model.MOrderItem;
import nl.*.android.model.MOrders;
#EFragment(R.layout.fragment_orders_delivery)
public class OrdersDeliveryFragment extends Fragment {
#Bean
public APIFactory apiFactory;
private API api;
private List<MOrderItem> items = new ArrayList<>();
private OrderListAdapter adapter;
#ViewById
public ListView ordersList;
#AfterViews
public void init() {
api = apiFactory.get();
getDeliveryOrders();
adapter = new OrderListAdapter(getActivity(), R.id.ordersList, items);
ordersList.setAdapter(adapter);
dataChanged();
}
#UiThread
public void dataChanged() {
adapter.notifyDataSetChanged();
}
#Background
public void getDeliveryOrders() {
items.clear();
final MOrders result = api.getOrdersForDelivery();
if (result != null) {
Collections.addAll(items, result.result.orderList);
}
}
}
OrdersPickupFragment.java [Tab 3]
package nl.*.android.order;
import android.support.v4.app.Fragment;
import android.widget.ListView;
import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import nl.*.android.R;
import nl.*.android.backend.API;
import nl.*.android.backend.APIFactory;
import nl.*.android.model.MOrderItem;
import nl.*.android.model.MOrders;
#EFragment(R.layout.fragment_orders_pickup)
public class OrdersPickupFragment extends Fragment {
#Bean
public APIFactory apiFactory;
private API api;
private List<MOrderItem> items = new ArrayList<>();
private OrderListAdapter adapter;
#ViewById
public ListView ordersList;
#AfterViews
public void init() {
api = apiFactory.get();
getPickupOrders();
adapter = new OrderListAdapter(getActivity(), R.id.ordersList, items);
ordersList.setAdapter(adapter);
dataChanged();
}
#UiThread
public void dataChanged() {
adapter.notifyDataSetChanged();
}
#Background
public void getPickupOrders() {
items.clear();
final MOrders result = api.getOrdersForPickup();
if (result != null) {
Collections.addAll(items, result.result.orderList);
}
}
}
OrderListAdapter.java
package nl.****.android.order;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import nl.*.android.R;
import nl.*.android.model.MOrderItem;
/**
* #author Niels
* #version 1.0
* #since 4-9-2015
*/
public class OrderListAdapter extends ArrayAdapter<MOrderItem> {
private List<MOrderItem> itemList = new ArrayList<>();
private Context context;
private OrdersActivity activity;
private static final SimpleDateFormat FORMATTER = new SimpleDateFormat("dd-MM-yyyy HH:mm", Locale.FRENCH);
public OrderListAdapter(Context context, int resource, List<MOrderItem> orderList) {
super(context, resource, orderList);
this.itemList = orderList;
this.context = context;
}
#Override
public View getView(final int position, View view, ViewGroup parent) {
view = LayoutInflater.from(getContext()).inflate(R.layout.list_single, parent, false);
MOrderItem item = itemList.get(position);
TextView label = (TextView) view.findViewById(R.id.txtKlant);
TextView txt = (TextView) view.findViewById(R.id.txtDate);
label.setText(item.name);
txt.setText(FORMATTER.format(item.placeDate));
return view;
}
}
Thanks for any and all help.
Turns out I was notifying my listview that it's data set had changed before I actually finished getting the results.
Solution: move the line "dataChanged()" in the #AfterViews annotated method #init() to the #Background annotated method #getOrders().

Categories

Resources