如何使用每个新参数在ArrayList中创建新对象

时间:2018-06-19 08:51:58

标签: java arraylist

public class AlarmsFragment extends Fragment {

javax.net.ssl.SSLHandshakeException

Caused by: javax.net.ssl.SSLHandshakeException: Received fatal alert: bad_certificate
at sun.security.ssl.Alerts.getSSLException(Alerts.java:192)
at sun.security.ssl.Alerts.getSSLException(Alerts.java:154)
at sun.security.ssl.SSLSocketImpl.recvAlert(SSLSocketImpl.java:2038)
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1135)
at sun.security.ssl.SSLSocketImpl.waitForClose(SSLSocketImpl.java:1779)
at sun.security.ssl.HandshakeOutStream.flush(HandshakeOutStream.java:124)
at sun.security.ssl.Handshaker.sendChangeCipherSpec(Handshaker.java:1156)
at sun.security.ssl.ClientHandshaker.sendChangeCipherAndFinish(ClientHandshaker.java:1266)
at sun.security.ssl.ClientHandshaker.serverHelloDone(ClientHandshaker.java:1178)
at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:348)
at sun.security.ssl.Handshaker.processLoop(Handshaker.java:1052)
at sun.security.ssl.Handshaker.process_record(Handshaker.java:987)
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1072)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1385)
at sun.security.ssl.SSLSocketImpl.writeRecord(SSLSocketImpl.java:757)
at sun.security.ssl.AppOutputStream.write(AppOutputStream.java:123)
at sun.nio.cs.StreamEncoder.writeBytes(StreamEncoder.java:221)
at sun.nio.cs.StreamEncoder.implFlushBuffer(StreamEncoder.java:291)
at sun.nio.cs.StreamEncoder.implFlush(StreamEncoder.java:295)
at sun.nio.cs.StreamEncoder.flush(StreamEncoder.java:141)
at java.io.OutputStreamWriter.flush(OutputStreamWriter.java:229)
at java.io.BufferedWriter.flush(BufferedWriter.java:254)
at de.sourcepark.security.cryptolib.ssl.DefaultSSLContextProviderTest.testMutualAuthFail(DefaultSSLContextProviderTest.java:204)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.ExpectException.evaluate(ExpectException.java:21)
... 23 more

它不断更新它创建的同一个对象,但是没有创建新的对象,你可以帮我解决这个问题。

FloatingActionButton floatingActionButton;
RecyclerView recyclerView;
TextView checkText;
ArrayList<AlarmObjects> alarmObjects;


public AlarmsFragment() {
    // Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {


    View v = inflater.inflate(R.layout.fragment_alarms, container, false);

    floatingActionButton = v.findViewById(R.id.fab_button);
    recyclerView = v.findViewById(R.id.rv_alarms);

     alarmObjects = new ArrayList<>();
    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
    RecyclerView.LayoutManager rvLayoutManager = linearLayoutManager;
    recyclerView.setLayoutManager(rvLayoutManager);
    AlarmAdapter alarmAdapter = new AlarmAdapter(getContext(),alarmObjects);
    recyclerView.setAdapter(alarmAdapter);

    Bundle arguments = getArguments();

    if (arguments != null){
       String newTime = getArguments().getString("newTimm");
        String newNotes = getArguments().getString("newNott");
        checkText.setText(newTime + newNotes);
        alarmObjects.add(new AlarmObjects(newNotes,newTime));
       }

    floatingActionButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            ((MainActivity)getActivity()).showPopupmeth();
        }
    });


    return  v;

    }

   }

使用这种方法我回到我的alarmFragment。当我从对话框片段回到它时,它每次都按照我的理解创建新的ArrayList。如何使用getArguments

显示现有对象并添加新对象

3 个答案:

答案 0 :(得分:0)

很抱歉,但我无法得到你想做的事。

在这里初始化一个ArrayList:

alarmObjects = new ArrayList<>();

将第一个对象放入数组:

 alarmObjects.add(new AlarmObjects(newNotes,newTime)); }

您期望什么行为?

编辑:

试试这个:

FloatingActionButton floatingActionButton;
RecyclerView recyclerView;
TextView checkText;
ArrayList<AlarmObjects> alarmObjects = new ArrayList<>();

public AlarmsFragment() {
    // Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {


    View v = inflater.inflate(R.layout.fragment_alarms, container, false);

    floatingActionButton = v.findViewById(R.id.fab_button);
    recyclerView = v.findViewById(R.id.rv_alarms);

    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
    RecyclerView.LayoutManager rvLayoutManager = linearLayoutManager;
    recyclerView.setLayoutManager(rvLayoutManager);
    AlarmAdapter alarmAdapter = new AlarmAdapter(getContext(),alarmObjects);
    recyclerView.setAdapter(alarmAdapter);

    Bundle arguments = getArguments();

    if (arguments != null){
       String newTime = getArguments().getString("newTimm");
        String newNotes = getArguments().getString("newNott");
        checkText.setText(newTime + newNotes);
        alarmObjects.add(new AlarmObjects(newNotes,newTime));
       }

    floatingActionButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            ((MainActivity)getActivity()).showPopupmeth();
        }
    });


    return  v;
    enter code here
    }

   }

答案 1 :(得分:0)

这只是代码的一部分。我假设每个新参数都会调用上面的代码。如果是这样,那么以下是发生的事情: 在第一行,您正在执行新的ArrayList(),它始终创建一个新的(和空)数组列表。 在最后一行,您将向列表中添加一个新的AlarmObject。 因此,当您尝试从列表中检索元素时,您将获得最近添加的元素。这就是您认为它正在修改现有元素的原因。 实际上,您正在添加一个新元素。但是你每次都在重新创建列表。所以你没有看到前面的元素

不是每次都创建一个新的arraylist,而是在parent方法中创建一个并将该引用传递给此方法。

public void parentMethod(){
    List<AlarmObjects> alarmObjects = new ArrayList<>();
    childMethod(alarmObjects);
}
public void childMethod(List<AlarmObjects> alarmObjects){
    //all of your code except the new ArrayList<>() goes here
}

答案 2 :(得分:0)

你只是参与并且不够清楚,所以我假设你想创建Immutable ArrayList,你可以通过使用Collections框架Java 8的unmodifiableList方法实现如下

即Collections unmodifiableList(list);

示例:

List<String> list = new ArrayList<String>(Arrays.asList("one", "two", "three"));
List<String> unmodifiableList = Collections.unmodifiableList(list);
unmodifiableList.add("four");