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

时间:2015-01-29 02:04:13

标签: android listview fragment adapter

我正在尝试使用StringListView项添加到Fragments

我有四个Buttons。每次点击Button时,ListView都应添加String项:“按钮1,2,3或4点击”

问题在于它一直用被点击的String Button替换第一个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时都会创建一个新项目。目前它只替换第一项。

1 个答案:

答案 0 :(得分:2)

每次调用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();
}
}