我正在尝试使用此解决方案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
答案 0 :(得分:2)
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
答案 1 :(得分:0)
我正在研究同样的问题。我正在慢慢克服这个问题。这可以帮助您解决TodoListAdapter问题https://gitorious.org/acal/acal/source/a6ba5fbe340ae96c1078e1e8e330e1e317ba5620:src/com/morphoss/acal/activity/TodoListAdapter.java#L70
这与您的ToDoManagerActivity问题有关。 Crash after adding footer to ListView
我目前正在参加您参加或正在参加的相同课程。这是一个免费的课程,任何人都可以注册。没有人获得该课程的成绩,它可以增加人们对主题的一般知识,在这种情况下是Android开发。请查看coursea.org。在尝试获得良好帮助时,我感到很痛苦,而且当他说他不想为你工作时,我也明白帮助者的来源。