0

I am really new to all this stuff so i hope you can help me with my situation. I want that my app get the info of two EditText in the mainactivity , save it (with json on my case) and pass it to another activity and show it on a ListView. But I am having problems with my adapter and my intends.

MainActivity.java

public class MainActivity extends AppCompatActivity {

SharedPreferences sharedPreferences;
EditText place;
EditText description;
Button save;
JournalList journalList;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    place = findViewById(R.id.place);
    description = findViewById(R.id.description);
    save = findViewById(R.id.save);

    sharedPreferences = getSharedPreferences("Journal", MODE_PRIVATE);

    String json = sharedPreferences.getString("journal", "");
    if(json != null){
        journalList = new JournalList();
        journalList = journalList.fromJson(json);
        for (Journal p:journalList.journals
             ) {
            Log.d("Journal", "Place :" + p.place + "Description :" + p.description);

        }
    }else{
        journalList = new JournalList();
    }

}

public void clickOnSave (View view) {
    save();
    openList();

}
/*
Con este metodo pasamos a la actividad donde tenemos guardados los diarios al pulsar el boton
save.
 */
public void openList (){
    Intent intent = new Intent(this, ListViewActivity.class);
    Bundle bundle = new Bundle();
    bundle.putSerializable("JournalList", journalList);
    intent.putExtra("Bundle", bundle);
    startActivity(intent);

}
/*
Con este metodo guardamos de manera persistente la informacion introducida en los editText
del layout al hacer click en el boton save.
 */
public void save (){
    String newPlace = place.getText().toString();
    String newDescription = description.getText().toString();

    Journal journal = new Journal();
    journal.place = newPlace;
    journal.description = newDescription;

    journalList.journals.add(journal);


    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putString("journal", journalList.toJson());
    editor.apply();
    Toast.makeText(this, "Data saved", Toast.LENGTH_SHORT).show();
}
}

ListviewActivity.java

public class ListViewActivity extends AppCompatActivity {

ListView list;
ArrayList<JournalList> journalLists;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_list_view);

    list = findViewById(R.id.list);
    Bundle bundle = getIntent().getBundleExtra("Bundle");
    ArrayList<JournalList> journalLists = (ArrayList<JournalList>) bundle.getSerializable("JournalList");
    //Adaptamos el array a nuestra lista
    MyAdapter adapter = new MyAdapter(this, android.R.layout.simple_list_item_1, journalLists);
    list.setAdapter(adapter);
}
}

MyAdapter.java

public class MyAdapter extends ArrayAdapter<JournalList> {
private Context context;
int itemLayout;
ArrayList<JournalList> JournalLists;

public MyAdapter(@NonNull Context context, int resource, @NonNull List<JournalList> JournalLists) {
    super(context, resource, JournalLists);
    this.context = context;
    itemLayout = resource;
    this.JournalLists = (ArrayList<JournalList>) JournalLists;
}

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
    LayoutInflater layoutInflater = LayoutInflater.from(context);
    View view= layoutInflater.inflate(itemLayout, parent, false);
    TextView placeTV = convertView.findViewById(R.id.placeTV);
    TextView descriptionTV = convertView.findViewById(R.id.descriptionTV);
    placeTV.setText(JournalLists.indexOf(position));
    descriptionTV.setText(JournalLists.indexOf(position));


    return view;
}
}

Journal.java

public class Journal implements Serializable {
String place;
String description;}

JournalList.java

public class JournalList implements Serializable {
public ArrayList<Journal> journals;

public JournalList(){
    journals = new ArrayList<>();
}

public String toJson (){
    Gson gson = new Gson();
    String json = gson.toJson(this);
    return json;
}
public JournalList fromJson (String json){
    Gson gson = new Gson();
    JournalList journalList = gson.fromJson(json, JournalList.class);
    return journalList;
}
}

After fixing the typo i got this new logcat:

Caused by: java.lang.ClassCastException: com.example.alexj.ejercicio6.JournalList cannot be cast to java.util.ArrayList
    at com.example.alexj.ejercicio6.ListViewActivity.onCreate(ListViewActivity.java:26)
    at android.app.Activity.performCreate(Activity.java:7136)
    at android.app.Activity.performCreate(Activity.java:7127)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
  • Is this just a typo in your question or do you really have "bundle" in MainActivity and "bundle" in ListViewActivity? – Bö macht Blau Nov 04 '18 at 18:42
  • Yes I have, the bundle in listviewactivity it's supposed to get the info from mainactivity or at least is what I'm trying to do – Alex Hernandez Nov 04 '18 at 18:51
  • OK, but at least in your question here the first is with a "B" whereas the second one starts with a "b" (That's what I wanted to indicate with my first comment but, well, typos happen :/) – Bö macht Blau Nov 04 '18 at 18:55
  • The second NPE you're getting happens when *convertView* is null (which is usually the case when you start to show a list - only after some scrolling you will have Views which can be recycled). You can avoid this by writing `view.findViewById()` since *view* is the View you just inflated and are going to return. Or (recommended approach) you can try the ViewHolder pattern like [here](https://stackoverflow.com/a/52451406/5015207) – Bö macht Blau Nov 04 '18 at 19:05
  • Ok, i have fixed the typo but now i have this error in my logcat. Caused by: java.lang.ClassCastException: com.example.alexj.ejercicio6.JournalList cannot be cast to java.util.ArrayList at com.example.alexj.ejercicio6.ListViewActivity.onCreate(ListViewActivity.java:26) at android.app.Activity.performCreate(Activity.java:7136) at android.app.Activity.performCreate(Activity.java:7127) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893) – Alex Hernandez Nov 04 '18 at 22:52

2 Answers2

1

Maybe you should change

Bundle bundle = getIntent().getBundleExtra("bundle");

in

Bundle bundle = getIntent().getBundleExtra("Bundle");

mychemicalro
  • 232
  • 3
  • 21
0

To fix your issue, as mychemicalro said in his answer, you should make sure your keys are not changing case. Use a static value to make sure this doesn't happen.

I would also suggest that maybe bundling a Bundle into an Intent isn't necessary here. You can make things a little faster and easier by having your Journal class implement Parcelable, then the intent can have a parcelableArrayListExtra added to it directly.

WoogieNoogie
  • 1,258
  • 1
  • 11
  • 21