我想實現一個簡單的使用此解決方案Add user input into a ListView on button click待辦事項清單,但是當我實施解決方案我收到以下錯誤http://pastebin.com/9CBCMrjv我使用的片段在此應用程序的OnCreate,我想知道這可能會導致錯誤也許構造函數不適用於片段。如何使用listview實現一個簡單的待辦事項清單?
有人可以做出錯誤的感覺或在那裏我有這個實現的問題呢?我對錯誤的理解是,這個解決方案不能應用於我的課程,因爲它鏈接到片段而不是普通的活動。
這是完整的類來給你一個更好的理解這一問題:
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
只是現在要進行測試,希望這能解決問題.. –
@BrianJ的建議是正確的。還要確保你有正確的進口。這是我沒有提到的。休息一切應該沒問題 – Raghunandan
是的,它可以添加一些更多的功能,如果我選擇一個行項目,我可以刪除它或輸入數量.. –