Listview 替换第一个项目而不是创建新项目

Listview replaces first item instead of creating new items

我正在尝试使用 FragmentsString 项添加到我的 ListView

我有四个 Buttons。每次单击 ButtonListView 应添加一个 String 项:"Button 1,2,3, or 4 clicked"

问题是它一直用单击的 Button String 替换第一个 String 项目。它不会创建新项目。

这是我的代码:

ListViewFragment.java:

public class ListViewFragment1 extends Fragment {

    protected ArrayAdapter<String> adapter1;

    @Override
    public View onCreateView(LayoutInflater inflater,
            @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {       
        return inflater.inflate(R.layout.list_view_fragment1, container, false);
    }

    public void changeText(String data){

        //Build Adapter
        adapter1 = new ArrayAdapter<String>(getActivity(), R.layout.items);

        //Configure the list view
        ListView list1 = (ListView) getActivity().findViewById(R.id.listview1);

        list1.setAdapter(adapter1);

        adapter1.add(data);
        adapter1.notifyDataSetChanged();
    }
}

ButtonLayoutFragment.java:

public class ButtonLayoutFragment extends Fragment implements View.OnClickListener {

    Communicator comm;

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

        return inflater.inflate(R.layout.buttons_layout, container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        comm = (Communicator) getActivity();

        Button[] bttn_arr = new Button[] {b0, b1, b2, b3}; 

        button_list.addAll(Arrays.asList(bttn_arr));

        button_list.set(0,(Button) getActivity().findViewById(R.id.button0));
        button_list.set(1,(Button) getActivity().findViewById(R.id.button1));
        button_list.set(2,(Button) getActivity().findViewById(R.id.button2));
        button_list.set(3,(Button) getActivity().findViewById(R.id.button3));

        for(Button b:button_list){
            b.setOnClickListener(this);
        }
    }

    @Override
    public void onClick(View v) {
          switch(v.getId()){
              case R.id.button0:
                  comm.respond("item0 added");
                  break;
              case R.id.button1:
                  comm.respond("item1 added");
                  break;
              case R.id.button2:
                  comm.respond("item2 added"); 
                break;
              case R.id.button3:
                  comm.respond("item3 added"); 
                break;
      }     
    }
}

MainActivity.java:

public class MainActivity extends Activity implements Communicator {

    FragmentManager manager;

    ListViewFragment1 frag2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.game_activity);

        ButtonLayoutFragment frag = new ButtonLayoutFragment();
        frag2 = new ListViewFragment1();
        ListViewFragment2 frag3 = new ListViewFragment2();
        manager = getFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.add(R.id.FragmentLayout, frag, "Fragment");
        transaction.add(R.id.fragment2, frag2, "Fragment2");
        transaction.commit();
    }

    @Override
    public void respond(String data) {
        frag2.changeText(data);
    }

}

Communicator.java:

public interface Communicator {
    public void respond(String data);    
}

这是所有相关代码。我想我正确地连接了片段。我认为问题出在 ListViewFragment1 - 我可能没有正确使用 adapter。我尝试了许多不同的方法,但没有任何效果。有人可以帮忙吗?

*提醒 - 我只是希望 ListView 在每次单击 Button 时创建一个新项目。目前它只替换第一项。

每次调用 changeText() 时,您的适配器都会重置,因此只会显示一个项目

像这样更改您的片段

public class ListViewFragment1 extends Fragment {

protected ArrayAdapter<String> adapter1;
ListView list1;

@Override
public View onCreateView(LayoutInflater inflater,
        @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.list_view_fragment1, container, false);       
    adapter1 = new ArrayAdapter<String>(getActivity(), R.layout.items);
    list1 = (ListView) view.findViewById(R.id.listview1);
    list1.setAdapter(adapter1);
    return view;
}

public void changeText(String data){
    adapter1.add(data);
    adapter1.notifyDataSetChanged();
}
}