2014-01-13 38 views
0

我想實現一個簡單的使用此解決方案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 

回答

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

+0

只是現在要進行測試,希望這能解決問題.. –

+0

@BrianJ的建議是正確的。還要確保你有正確的進口。這是我沒有提到的。休息一切應該沒問題 – Raghunandan

+0

是的,它可以添加一些更多的功能,如果我選擇一個行項目,我可以刪除它或輸入數量.. –

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。當我想要獲得良好的幫助時,我感到你的痛苦,同時我也明白當他說他不想爲你做你的工作時,這個幫手是從哪裏來的。

相關問題