【问题标题】:How to implement a simple to do list using a listview?如何使用列表视图实现一个简单的待办事项列表?
【发布时间】:2014-03-05 07:22:58
【问题描述】:

我正在尝试使用此解决方案 Add user input into a ListView on button click 实现一个简单的待办事项列表,但是当我实现解决方案的 onCreate 时,我收到以下错误 http://pastebin.com/9CBCMrjv 我在此应用程序中使用片段,我正在想知道这是否会导致错误,因为构造函数可能不适用于片段。

有人能理解错误或我在这个实现中出错的地方吗?我对错误的理解是,此解决方案无法应用于我的课程,因为它链接到片段而不是普通活动。

这是让您更好地理解问题的完整课程:

    public class TopRatedFragment extends Fragment implements OnClickListener {

ListView mListView;
EditText mValue;
Button mAdd;
ArrayList<String> list = new ArrayList<String>();
ArrayAdapter<String> adapter;

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

    View rootView = inflater.inflate(R.layout.fragment_top_rated, container, false);
    return rootView;    
}

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fragment_top_rated);

    mAdd = (Button)findViewById(R.id.newList);
    mAdd.setOnClickListener(this);
    mValue = (EditText)findViewById(R.id.listData);
    adapter = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, list);

    // set the lv variable to your list in the xml
    mListView=(ListView)findViewById(R.id.listView);  
    mListView.setAdapter(adapter);
}
public void onClick(View v)
{
    String input = mValue.getText().toString();
    if(input.length() > 0)
    {
        // add string to the adapter, not the listview
        adapter.add(input);
        // no need to call adapter.notifyDataSetChanged(); as it is done by the adapter.add() method
    }
}   

 }

错误

The method setContentView(int) is undefined for the type TopRatedFragment

The method findViewById(int) is undefined for the type TopRatedFragment

The method setOnClickListener(View.OnClickListener) in the type View is not applicable for the arguments (TopRatedFragment)

The method findViewById(int) is undefined for the type TopRatedFragment

The constructor ArrayAdapter<String>(TopRatedFragment, int, ArrayList<String>) is undefined

The method findViewById(int) is undefined for the type TopRatedFragment

【问题讨论】:

    标签: android android-listview android-fragments


    【解决方案1】:

    setContentView(R.layout.fragment_top_rated);

    错了

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
    
        View rootView = inflater.inflate(R.layout.fragment_top_rated, container, false);
         mAdd = (Button)rootView.findViewById(R.id.newList);
         mAdd.setOnClickListener(this);
         mValue = (EditText)rootView.findViewById(R.id.listData);
         adapter = new ArrayAdapter<String>(getActivity, android.R.layout.simple_expandable_list_item_1, list);
    
    // set the lv variable to your list in the xml
         mListView=(ListView)rootView.findViewById(R.id.listView);  
         mListView.setAdapter(adapter);
        return rootView;    
    }
    

    你需要在 onCreateView 中给视图充气并返回视图

    你也可以使用膨胀的视图对象来初始化你的视图

    The constructor ArrayAdapter<String>(TopRatedFragment, int, ArrayList<String>) is undefined
    

    所以改成

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

    去掉onCreate中的代码

    【讨论】:

    • @BrianJ 这个建议是对的。还要确保你有正确的进口。这就是我没有提到的。休息应该没问题
    • 是的,这行得通,现在可能会添加更多功能,比如如果我选择一个行项目,我可以删除它或输入一个数量..
    猜你喜欢
    • 1970-01-01
    • 2020-02-20
    • 1970-01-01
    • 1970-01-01
    • 2022-12-03
    • 1970-01-01
    • 2019-06-02
    • 2012-02-01
    • 2015-11-15
    相关资源
    最近更新 更多