简体   繁体   English

如何使用listview实现一个简单的待办事项列表?

[英]How to implement a simple to do list using a listview?

I'm trying to implement a simple to do list using this solution Add user input into a ListView on button click , but when I implement the onCreate of the solution I get the following errors http://pastebin.com/9CBCMrjv I'm using fragments in this application and I'm wondering could this be causing the errors as maybe the constructors don't apply to fragments. 我正在尝试使用此解决方案实现一个简单的待办事项列表在按钮单击将用户输入添加到ListView中 ,但是当我实现解决方案的onCreate时,我得到以下错误http://pastebin.com/9CBCMrjv我是在这个应用程序中使用片段,我想知道这可能导致错误,因为构造函数可能不适用于片段。

Can someone make sense of the errors or where I'm going wrong with this implementation? 有人能够理解错误或我在这个实现中出错了吗? My understanding of the errors is that this solution can't be applied to my class as it is linked to a fragment instead of a plain activity. 我对错误的理解是这个解决方案不能应用于我的类,因为它链接到片段而不是普通活动。

This is the complete class to give you a better understanding of the problem: 这是让您更好地理解问题的完整课程:

    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 errors 错误

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

setContentView(R.layout.fragment_top_rated);

wrong 错误

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

You need to inflate a view in onCreateView and return the view 您需要在onCreateView中膨胀视图并返回视图

Also you use the inflated view object to initialize your views 您还可以使用膨胀的视图对象来初始化视图

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

So change to 所以改为

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

get rid of the codes in onCreate 摆脱onCreate中的代码

I am working on the same problem. 我正在研究同样的问题。 I am slowly overcoming this problem. 我正在慢慢克服这个问题。 This can help you with your TodoListAdapter problems https://gitorious.org/acal/acal/source/a6ba5fbe340ae96c1078e1e8e330e1e317ba5620:src/com/morphoss/acal/activity/TodoListAdapter.java#L70 这可以帮助您解决TodoListAdapter问题https://gitorious.org/acal/acal/source/a6ba5fbe340ae96c1078e1e8e330e1e317ba5620:src/com/morphoss/acal/activity/TodoListAdapter.java#L70

and this with your ToDoManagerActivity problems. 这与您的ToDoManagerActivity问题有关。 Crash after adding footer to ListView 向FootView添加页脚后崩溃

I am currently enrolled in the same course you took or is taking. 我目前正在参加您参加或正在参加的相同课程。 It is a free course and anyone can sign up. 这是一个免费的课程,任何人都可以注册。 No one gets a grade for this course it is offered to increase peoples general knowledge on topics, in this case Android development. 没有人获得该课程的成绩,它可以增加人们对主题的一般知识,在这种情况下是Android开发。 Just look on coursea.org. 请查看coursea.org。 I feel your pain when it comes to trying to get good help and I also understand where the helper is coming from when he said he did not want to do your work for you. 在尝试获得良好帮助时,我感到很痛苦,而且当他说他不想为你工作时,我也明白帮助者的来源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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