简体   繁体   中英

Add Item to ListView

I'm trying to add items at runtime to ListView placed on Tab. But when I do so, I get an error. There are 3 tabs and I'm not using ListFragments, because I have different content in all tabs

my code below

SwipeTabFragment.java

public class SwipeTabFragment extends Fragment {

    private String tab;
    private int color;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        tab = bundle.getString("tab");
        color = bundle.getInt("color");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.bookshelf, null);
        TextView tv = (TextView) view.findViewById(R.id.textView);
        tv.setText(tab);
        view.setBackgroundResource(color);
        return view;
    }
}

TabPagerAdapter.java

public class TabPagerAdapter extends FragmentPagerAdapter {

    public TabPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int index) {
        Bundle bundle = new Bundle();
        String tab = "";
        int colorResId = 0;
        SwipeTabFragment swipeTabFragment = new SwipeTabFragment();
        switch (index) {
            case 0:
                tab = "BookShelf";
                bundle.putString("tab",tab);
                swipeTabFragment.setArguments(bundle);
                return swipeTabFragment;


               // break;
            case 1:
                tab = "Now Listening";
                colorResId = R.color.color2;
                BookFragment bookFragment = new BookFragment();
                return bookFragment;
                //break;
            case 2:
                tab = "Library";
                colorResId = R.color.color3;
                TabLibrary tabLibrary = new TabLibrary();
                return tabLibrary;
                break;
        }

        return null;
    }

    @Override
    public int getCount() {
        return 3;
    }
}

TabLibrary.java

public class TabLibrary extends Fragment {

    final String LOG_TAG = "myLogs";
    ListView lvLibList;
    ImageButton btAddItem;
    ArrayList<String> list = new ArrayList<String>();
    ArrayAdapter<String> adapter;

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

        ListView lvLibList = (ListView)v.findViewById(R.id.lvLibList);
        ImageButton btn = (ImageButton)v.findViewById(R.id.btAddItem);
        adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, list);
        View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EditText edit = (EditText)v.findViewById(R.id.eAddItem);
                list.add(edit.getText().toString());
                edit.setText("");
                adapter.notifyDataSetChanged();
            }
        };
        btn.setOnClickListener(listener);
        lvLibList.setAdapter(adapter);
        return v;
    }

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
}

Posting my comment as answer, as I'm pretty sure that is the issue:

The variable edit seems to be null. That is because when you call v.findViewById , you are actually calling it on the view that was clicked and NOT the fragment's view.

So you got two options:

  1. Change the name of the fragment's view. eg. View v1 = inflater.inflate(R.layout.library, parent, false); and then change the line to v1.findViewById
  2. Change the name of the argument of onClick. eg. public void onClick(View v1) { .

Hope that helps.

@Gurusharan S is right, the problem is about the names of your variables. To avoid confusions I'd suggest to change your code to this:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.library, parent, false); //Change the name from v to view

    ListView lvLibList = (ListView)view.findViewById(R.id.lvLibList);
    ImageButton btn = (ImageButton)view.findViewById(R.id.btAddItem);
    EditText edit = (EditText)view.findViewById(R.id.eAddItem);   //Initialize the EditText outside of the click method

    adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, list);

    View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            list.add(edit.getText().toString());
            edit.setText("");
            adapter.notifyDataSetChanged();
        }
    };
    btn.setOnClickListener(listener);
    lvLibList.setAdapter(adapter);
    return view;
}

And in the future I'd advice to give your variables more meaningful names, sometimes it takes you a little more coding but it is worth it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM