2017-03-25 54 views
0

我知道很多人都問過這個問題,或者那些非常相似,但所有響應我已閱讀並嘗試我的應用程序仍然不能正常工作。它不會崩潰,但循環加載符號無限期地繼續。安卓:用此按鈕來添加項目的ListView在片段

什麼我現在已經是一個包含一個按鈕和一個ListView片段。我希望能夠在點擊按鈕時添加一個新項目。

這裏是我的MainActivity:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    if(findViewById(R.id.container) != null) { 
    if (savedInstanceState == null) { 
     getSupportFragmentManager().beginTransaction() 
       .add(R.id.container, new ListFragment()) 
       .commit(); 
      } 
     } 
    }  
    } 

我的片段:

public class ListFragment extends Fragment implements View.OnClickListener { 

private ArrayList<String> listItems = new ArrayList<String>(); 
private ArrayAdapter<String> myListAdapter; 
private ListView listView; 

public ListFragment() { 

} 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
} 

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

    // The ArrayAdapter will take data from a source and 
    // use it to populate the ListView it's attached to. 
    myListAdapter = new ArrayAdapter<String>(
      getActivity(), 
      R.layout.fragment_element_list, 
      R.id.list_item_textview, 
      listItems); 

    View rootView = inflater.inflate(R.layout.fragment_list, container, false); 

    listView = (ListView) rootView.findViewById(
      R.id.listview_list); 
    listView.setAdapter(myListAdapter); 

    Button b = (Button) getView().findViewById(R.id.button); 
    b.setOnClickListener(this); 

    return rootView; 

} 

@Override 
public void onClick(View v) { 
    switch (v.getId()) { 
     case R.id.button: 
      listView.setAdapter(myListAdapter); //Adds to the List View 
      listItems.add("New Item"); 
      break; 
    } 

} 
} 

ActivityMain.xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/container" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" > 

FragmentList.xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context=".ListFragment"> 

<Button 
    android:id="@+id/button" 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content" 
    android:text="Add" /> 

<ListView 
    android:id="@+id/listview_list" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    /> 

</FrameLayout> 

Fragment_element_list.xml:

<?xml version="1.0" encoding="utf-8"?> 
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:minHeight="?android:attr/listPreferredItemHeight" 
android:gravity="center_vertical" 
android:id="@+id/list_item_textview"> 
</TextView> 

我爲我重複的問題表示歉意。我仍然在學習Android,非常感謝這方面的幫助。

編輯:這是我迄今所做的更改。唯一出現的是頁面加載器。

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

    // The ArrayAdapter will take data from a source and 
    // use it to populate the ListView it's attached to. 
    myListAdapter = new ArrayAdapter<String>(
      getActivity(), 
      R.layout.fragment_element_list, 
      R.id.list_item_textview, 
      listItems); 

    View rootView = inflater.inflate(R.layout.fragment_list, container, false); 

    listView = (ListView) rootView.findViewById(
      R.id.listview_list); 
    listView.setAdapter(myListAdapter); 

    Button b = (Button) rootView.findViewById(R.id.button); 
    b.setOnClickListener(this); 

    return rootView; 

} 

@Override 
public void onClick(View v) { 
    switch (v.getId()) { 
     case R.id.button: 
      listItems.add("New Item"); 
      myListAdapter.notifyDataSetChanged(); 
      break; 
    } 

} 

而且我在logcat中發現: 03-25 10:22:25.343 5617-5617/com.example.kristindiep.appmanager E/ViewRootImpl: sendUserActionEvent() mView == null

+0

如果調用myListAdapter.notifyDataSetChanged()你把你的項目到時listItems列表後,項目添加到ListView。另外,您不要多次調用listView.setAdapter(myListAdapter)。試試吧,讓我知道我 –

+0

從拿出listView.setAdapter(myListAdapter)內onCreateView和listtems.add後myListAdapter.notifyDataSetChanged()加(「新項目」),但頁面仍然將不會加載。 – kristiyip

回答

0

想通了!這是因爲ListFragment是Android中的一個內置類,所以它不喜歡我'重新定義'它。

0

嘗試清除從適配器的項目,然後調用notifyDataSetChanged()方法:

listAdapter.clear(); 
listitems.add("item"); 
listAdapter.addAll(listitems); 
listAdapter.notifyDataSetChanged(); 
+0

它仍然不會加載頁面。但是我發現,如果我拿出開關盒,只留下那裏的東西,那麼應用程序就會崩潰。 – kristiyip

+0

嘗試用rootView替換getView(),如Button b =(Button)rootView.findViewById(R.id.button); b.setOnClickListener(this); – Akshay

0

刪除。 listview.setadapter()從的onclick ,並將其添加到CreateView的

,並添加的onclick() -

listItems.add(); adapter notifyDataSetChanged();

0

兩件事情,我注意到:

1.Don't您onCreateView方法使用getView() ,使用剛剛膨脹的觀點:

Button b = (Button) rootView.findViewById(R.id.button); 
b.setOnClickListener(this); 

2.您的onClick方法應該是這樣的:

@Override 
public void onClick(View v) { 
    switch (v.getId()) { 
     case R.id.button: 
      listItems.add("New Item"); 
      myListAdapter.notifyDataSetChanged(); 
      break; 
    } 
} 
+0

我也試過這個,但頁面加載器是唯一出現的東西。 – kristiyip

+0

對不起,但是「頁面加載器」是什麼意思? –