如何在片段中实现动态列表?

时间:2018-11-20 20:04:37

标签: java android android-fragments android-listview

我有一个片段,我想在其中实现一个动态列表,以便可以单击一个按钮以在列表中添加字段。

我发现一些代码可以做到这一点(也在下面显示)

Dynamically add elements to a listView Android

我正在尝试在我的片段中实现此操作,但未成功。一个解决方案将不胜感激。

我发现要创建动态列表的代码

import java.util.ArrayList;
import java.util.List;
import android.app.ListActivity;
import android.widget.ArrayAdapter;
import android.os.Bundle;
import android.view.View;

public class OrdersDynamicList extends ListActivity {
    //LIST OF ARRAY STRINGS WHICH WILL SERVE AS LIST ITEMS
    ArrayList<String> listItems=new ArrayList<String>();

    //DEFINING A STRING ADAPTER WHICH WILL HANDLE THE DATA OF THE LISTVIEW
    ArrayAdapter<String> adapter;

    //RECORDING HOW MANY TIMES THE BUTTON HAS BEEN CLICKED
    int clickCounter=0;

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.activity_server);
        adapter=new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1,
                listItems);
        setListAdapter(adapter);
    }

    //METHOD WHICH WILL HANDLE DYNAMIC INSERTION
    public void addItems(View v) {
        listItems.add("Clicked : "+clickCounter++);
        adapter.notifyDataSetChanged();
    }
}

我的片段

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;


public class FragmentNewTable extends Fragment {
    private FragmentNewTableListener listener;
    private OrdersDynamicList ordersDynamicList;

    public interface FragmentNewTableListener {
        void onInputFragmentNewTableSent(CharSequence input);
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_new_table, container, false);

        return v;
    }

    public void updateEditText(CharSequence newText) {
        //editText.setText(newText);
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof FragmentNewTableListener) {
            listener = (FragmentNewTableListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement FragmentAListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        listener = null;
    }

片段XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <Button
        android:id="@+id/addBtn"
        android:text="Add New Order"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:onClick="addItems"/>
    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:drawSelectorOnTop="false"
        />
</LinearLayout>

1 个答案:

答案 0 :(得分:0)

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import java.util.ArrayList;


public class FragmentNewTable extends Fragment {
    private FragmentNewTableListener listener;
    private Button addToList;
    private ArrayList<String> listItems;
    private ArrayAdapter<String> listViewAdapter;

    public interface FragmentNewTableListener {
        void onInputFragmentNewTableSent(CharSequence input);
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_new_table, container, false);

        this.listItems = new ArrayList<String>();

        this.listViewAdapter = new ArrayAdapter<String>(
                getActivity(),
                android.R.layout.simple_list_item_1,
                this.listItems
        );


        ListView listView = (ListView) v.findViewById(R.id.list);
        listView.setAdapter(listViewAdapter);

        this.addToList = v.findViewById(R.id.addBtn);
        this.addToList.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listItems.add("hi");
                listViewAdapter.notifyDataSetChanged();
            }
        });

        return v;
    }

    public void updateEditText(CharSequence newText) {
        //editText.setText(newText);
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof FragmentNewTableListener) {
            listener = (FragmentNewTableListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement FragmentAListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        listener = null;
    }
}