将项添加到ListView

时间:2014-10-07 16:35:16

标签: java android listview

我正在尝试在运行时将项目添加到Tab上的ListView。 但是当我这样做时,我得到一个错误。有3个选项卡,我没有使用ListFragments,因为我在所有选项卡中都有不同的内容

我的代码

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);
    }
}

2 个答案:

答案 0 :(得分:0)

发表评论作为答案,因为我非常确定这是问题:

变量edit似乎为空。这是因为当你调用v.findViewById时,你实际上是在被点击的视图上调用它而不是片段的视图。

所以你有两个选择:

  1. 更改片段视图的名称。例如。 View v1 = inflater.inflate(R.layout.library, parent, false);然后将该行更改为v1.findViewById
  2. 更改onClick的参数名称。例如。 public void onClick(View v1) {
  3. 希望有所帮助。

答案 1 :(得分:0)

@Gurusharan S是对的,问题在于变量的名称。为避免混淆,我建议将代码更改为:

@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;
}

在未来,我建议给你的变量更有意义的名字,有时它需要你更多的编码,但它是值得的。