【问题标题】:Move an ArrayList of objects from one activity to another. And adapter problems too将对象的 ArrayList 从一个活动移动到另一个活动。还有适配器问题
【发布时间】:2018-11-04 18:30:43
【问题描述】:

我对所有这些东西真的很陌生,所以我希望你能帮助我解决我的情况。 我希望我的应用程序在 mainactivity 中获取两个 EditText 的信息,将其保存(在我的情况下使用 json)并将其传递给另一个活动并在 ListView 上显示。 但我的适配器和我的意图有问题。

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;
}
}

修正错字后,我得到了这个新的 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)

【问题讨论】:

  • 这只是您的问题中的一个错字,还是您真的在 MainActivity 中有“bundle”,在 ListViewActivity 中有“bundle”?
  • 是的,我有,listviewactivity 中的捆绑包应该从 mainactivity 获取信息,或者至少是我正在尝试做的事情
  • 好的,但至少在您的问题中,第一个是“B”,而第二个是“b”(这就是我想在第一条评论中指出的,但是,好吧,发生拼写错误:/)
  • 第二个 NPE 发生在 convertView 为 null 时(这通常是您开始显示列表时的情况 - 只有经过一些滚动后,您才会有视图可以回收)。您可以通过写view.findViewById() 来避免这种情况,因为 view 是您刚刚膨胀并要返回的视图。或者(推荐的方法)你可以试试 ViewHolder 模式,比如here
  • 好的,我已经修正了错字,但现在我的 logcat 中有这个错误。原因:java.lang.ClassCastException: com.example.alexj.ejercicio6.JournalList 无法在 com.example.alexj.ejercicio6.ListViewActivity.onCreate(ListViewActivity.java:26) 在 android.app 中转换为 java.util.ArrayList .Activity.performCreate(Activity.java:7136) 在 android.app.Activity.performCreate(Activity.java:7127) 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271) 在 android.app.ActivityThread.performLaunchActivity( ActivityThread.java:2893)

标签: java android


【解决方案1】:

也许你应该改变

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

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

【讨论】:

  • 是的,密钥区分大小写。
【解决方案2】:

要解决您的问题,正如mychemicalro 在他的回答中所说,您应该确保您的密钥没有改变大小写。使用静态值确保不会发生这种情况。

我还建议在这里可能不需要将 Bundle 捆绑到 Intent 中。您可以通过让您的 Journal 类实现 Parcelable 来使事情变得更快、更容易,然后可以直接将 parcelableArrayListExtra 添加到意图。

【讨论】:

    猜你喜欢
    • 2015-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-11
    • 2013-01-12
    • 1970-01-01
    • 2021-03-15
    • 2012-08-04
    相关资源
    最近更新 更多