我知道很多人都問過這個問題,或者那些非常相似,但所有響應我已閱讀並嘗試我的應用程序仍然不能正常工作。它不會崩潰,但循環加載符號無限期地繼續。安卓:用此按鈕來添加項目的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
如果調用myListAdapter.notifyDataSetChanged()你把你的項目到時listItems列表後,項目添加到ListView。另外,您不要多次調用listView.setAdapter(myListAdapter)。試試吧,讓我知道我 –
從拿出listView.setAdapter(myListAdapter)內onCreateView和listtems.add後myListAdapter.notifyDataSetChanged()加(「新項目」),但頁面仍然將不會加載。 – kristiyip